Skip to content

Commit e1dbef5

Browse files
KSXGitHubanonrig
authored andcommitted
feat: fallible setters should return result
1 parent b31e5ed commit e1dbef5

File tree

1 file changed

+51
-40
lines changed

1 file changed

+51
-40
lines changed

src/lib.rs

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,17 @@ impl From<*mut ffi::ada_url> for Url {
141141
}
142142
}
143143

144+
type SetterResult = Result<(), ()>;
145+
146+
#[inline]
147+
fn setter_result(successful: bool) -> SetterResult {
148+
if successful {
149+
Ok(())
150+
} else {
151+
Err(())
152+
}
153+
}
154+
144155
impl Url {
145156
/// Parses the input with an optional base
146157
///
@@ -225,17 +236,17 @@ impl Url {
225236
}
226237

227238
/// Updates the href of the URL, and triggers the URL parser.
228-
/// Returns true if operation is successful.
229239
///
230240
/// ```
231241
/// use ada_url::Url;
232242
///
233243
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
234-
/// assert!(url.set_href("https://lemire.me"));
244+
/// url.set_href("https://lemire.me").unwrap();
235245
/// assert_eq!(url.href(), "https://lemire.me/");
236246
/// ```
237-
pub fn set_href(&mut self, input: &str) -> bool {
238-
unsafe { ffi::ada_set_href(self.0, input.as_ptr().cast(), input.len()) }
247+
#[allow(clippy::result_unit_err)]
248+
pub fn set_href(&mut self, input: &str) -> SetterResult {
249+
setter_result(unsafe { ffi::ada_set_href(self.0, input.as_ptr().cast(), input.len()) })
239250
}
240251

241252
/// Return the username for this URL as a percent-encoded ASCII string.
@@ -253,17 +264,17 @@ impl Url {
253264
}
254265

255266
/// Updates the `username` of the URL.
256-
/// Returns true if operation is successful.
257267
///
258268
/// ```
259269
/// use ada_url::Url;
260270
///
261271
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
262-
/// assert!(url.set_username("username"));
272+
/// url.set_username("username").unwrap();
263273
/// assert_eq!(url.href(), "https://[email protected]/");
264274
/// ```
265-
pub fn set_username(&mut self, input: &str) -> bool {
266-
unsafe { ffi::ada_set_username(self.0, input.as_ptr().cast(), input.len()) }
275+
#[allow(clippy::result_unit_err)]
276+
pub fn set_username(&mut self, input: &str) -> SetterResult {
277+
setter_result(unsafe { ffi::ada_set_username(self.0, input.as_ptr().cast(), input.len()) })
267278
}
268279

269280
/// Return the password for this URL, if any, as a percent-encoded ASCII string.
@@ -281,17 +292,17 @@ impl Url {
281292
}
282293

283294
/// Updates the `password` of the URL.
284-
/// Returns true if operation is successful.
285295
///
286296
/// ```
287297
/// use ada_url::Url;
288298
///
289299
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
290-
/// assert!(url.set_password("password"));
300+
/// url.set_password("password").unwrap();
291301
/// assert_eq!(url.href(), "https://:[email protected]/");
292302
/// ```
293-
pub fn set_password(&mut self, input: &str) -> bool {
294-
unsafe { ffi::ada_set_password(self.0, input.as_ptr().cast(), input.len()) }
303+
#[allow(clippy::result_unit_err)]
304+
pub fn set_password(&mut self, input: &str) -> SetterResult {
305+
setter_result(unsafe { ffi::ada_set_password(self.0, input.as_ptr().cast(), input.len()) })
295306
}
296307

297308
/// Return the port number for this URL, or an empty string.
@@ -312,17 +323,17 @@ impl Url {
312323
}
313324

314325
/// Updates the `port` of the URL.
315-
/// Returns true if operation is successful.
316326
///
317327
/// ```
318328
/// use ada_url::Url;
319329
///
320330
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
321-
/// assert!(url.set_port("8080"));
331+
/// url.set_port("8080").unwrap();
322332
/// assert_eq!(url.href(), "https://yagiz.co:8080/");
323333
/// ```
324-
pub fn set_port(&mut self, input: &str) -> bool {
325-
unsafe { ffi::ada_set_port(self.0, input.as_ptr().cast(), input.len()) }
334+
#[allow(clippy::result_unit_err)]
335+
pub fn set_port(&mut self, input: &str) -> SetterResult {
336+
setter_result(unsafe { ffi::ada_set_port(self.0, input.as_ptr().cast(), input.len()) })
326337
}
327338

328339
/// Return this URL’s fragment identifier, or an empty string.
@@ -374,17 +385,17 @@ impl Url {
374385
}
375386

376387
/// Updates the `host` of the URL.
377-
/// Returns true if operation is successful.
378388
///
379389
/// ```
380390
/// use ada_url::Url;
381391
///
382392
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
383-
/// assert!(url.set_host("localhost:3000"));
393+
/// url.set_host("localhost:3000").unwrap();
384394
/// assert_eq!(url.href(), "https://localhost:3000/");
385395
/// ```
386-
pub fn set_host(&mut self, input: &str) -> bool {
387-
unsafe { ffi::ada_set_host(self.0, input.as_ptr().cast(), input.len()) }
396+
#[allow(clippy::result_unit_err)]
397+
pub fn set_host(&mut self, input: &str) -> SetterResult {
398+
setter_result(unsafe { ffi::ada_set_host(self.0, input.as_ptr().cast(), input.len()) })
388399
}
389400

390401
/// Return the parsed representation of the host for this URL. Non-ASCII domain labels are
@@ -406,17 +417,17 @@ impl Url {
406417
}
407418

408419
/// Updates the `hostname` of the URL.
409-
/// Returns true if operation is successful.
410420
///
411421
/// ```
412422
/// use ada_url::Url;
413423
///
414424
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
415-
/// assert!(url.set_hostname("localhost"));
425+
/// url.set_hostname("localhost").unwrap();
416426
/// assert_eq!(url.href(), "https://localhost/");
417427
/// ```
418-
pub fn set_hostname(&mut self, input: &str) -> bool {
419-
unsafe { ffi::ada_set_hostname(self.0, input.as_ptr().cast(), input.len()) }
428+
#[allow(clippy::result_unit_err)]
429+
pub fn set_hostname(&mut self, input: &str) -> SetterResult {
430+
setter_result(unsafe { ffi::ada_set_hostname(self.0, input.as_ptr().cast(), input.len()) })
420431
}
421432

422433
/// Return the path for this URL, as a percent-encoded ASCII string.
@@ -434,17 +445,17 @@ impl Url {
434445
}
435446

436447
/// Updates the `pathname` of the URL.
437-
/// Returns true if operation is successful.
438448
///
439449
/// ```
440450
/// use ada_url::Url;
441451
///
442452
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
443-
/// assert!(url.set_pathname("/contact"));
453+
/// url.set_pathname("/contact").unwrap();
444454
/// assert_eq!(url.href(), "https://yagiz.co/contact");
445455
/// ```
446-
pub fn set_pathname(&mut self, input: &str) -> bool {
447-
unsafe { ffi::ada_set_pathname(self.0, input.as_ptr().cast(), input.len()) }
456+
#[allow(clippy::result_unit_err)]
457+
pub fn set_pathname(&mut self, input: &str) -> SetterResult {
458+
setter_result(unsafe { ffi::ada_set_pathname(self.0, input.as_ptr().cast(), input.len()) })
448459
}
449460

450461
/// Return this URL’s query string, if any, as a percent-encoded ASCII string.
@@ -492,17 +503,17 @@ impl Url {
492503
}
493504

494505
/// Updates the `protocol` of the URL.
495-
/// Returns true if operation is successful.
496506
///
497507
/// ```
498508
/// use ada_url::Url;
499509
///
500510
/// let mut url = Url::parse("http://yagiz.co", None).expect("Invalid URL");
501-
/// assert!(url.set_protocol("http"));
511+
/// url.set_protocol("http").unwrap();
502512
/// assert_eq!(url.href(), "http://yagiz.co/");
503513
/// ```
504-
pub fn set_protocol(&mut self, input: &str) -> bool {
505-
unsafe { ffi::ada_set_protocol(self.0, input.as_ptr().cast(), input.len()) }
514+
#[allow(clippy::result_unit_err)]
515+
pub fn set_protocol(&mut self, input: &str) -> SetterResult {
516+
setter_result(unsafe { ffi::ada_set_protocol(self.0, input.as_ptr().cast(), input.len()) })
506517
}
507518

508519
/// A URL includes credentials if its username or password is not the empty string.
@@ -827,31 +838,31 @@ mod test {
827838
"https://username:[email protected]:9090/search?query#hash"
828839
);
829840

830-
assert!(out.set_username("new-username"));
841+
out.set_username("new-username").unwrap();
831842
assert_eq!(out.username(), "new-username");
832843

833-
assert!(out.set_password("new-password"));
844+
out.set_password("new-password").unwrap();
834845
assert_eq!(out.password(), "new-password");
835846

836-
assert!(out.set_port("4242"));
847+
out.set_port("4242").unwrap();
837848
assert_eq!(out.port(), "4242");
838849

839850
out.set_hash("#new-hash");
840851
assert_eq!(out.hash(), "#new-hash");
841852

842-
assert!(out.set_host("yagiz.co:9999"));
853+
out.set_host("yagiz.co:9999").unwrap();
843854
assert_eq!(out.host(), "yagiz.co:9999");
844855

845-
assert!(out.set_hostname("domain.com"));
856+
out.set_hostname("domain.com").unwrap();
846857
assert_eq!(out.hostname(), "domain.com");
847858

848-
assert!(out.set_pathname("/new-search"));
859+
out.set_pathname("/new-search").unwrap();
849860
assert_eq!(out.pathname(), "/new-search");
850861

851862
out.set_search("updated-query");
852863
assert_eq!(out.search(), "?updated-query");
853864

854-
out.set_protocol("wss");
865+
out.set_protocol("wss").unwrap();
855866
assert_eq!(out.protocol(), "wss:");
856867

857868
assert!(out.has_credentials());
@@ -886,7 +897,7 @@ mod test {
886897
fn should_clone() {
887898
let first = Url::parse("https://lemire.me", None).unwrap();
888899
let mut second = first.clone();
889-
second.set_href("https://yagiz.co");
900+
second.set_href("https://yagiz.co").unwrap();
890901
assert_ne!(first.href(), second.href());
891902
assert_eq!(first.href(), "https://lemire.me/");
892903
assert_eq!(second.href(), "https://yagiz.co/");

0 commit comments

Comments
 (0)