@@ -141,6 +141,17 @@ impl From<*mut ffi::ada_url> for Url {
141
141
}
142
142
}
143
143
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
+
144
155
impl Url {
145
156
/// Parses the input with an optional base
146
157
///
@@ -225,17 +236,17 @@ impl Url {
225
236
}
226
237
227
238
/// Updates the href of the URL, and triggers the URL parser.
228
- /// Returns true if operation is successful.
229
239
///
230
240
/// ```
231
241
/// use ada_url::Url;
232
242
///
233
243
/// 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( );
235
245
/// assert_eq!(url.href(), "https://lemire.me/");
236
246
/// ```
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 ( ) ) } )
239
250
}
240
251
241
252
/// Return the username for this URL as a percent-encoded ASCII string.
@@ -253,17 +264,17 @@ impl Url {
253
264
}
254
265
255
266
/// Updates the `username` of the URL.
256
- /// Returns true if operation is successful.
257
267
///
258
268
/// ```
259
269
/// use ada_url::Url;
260
270
///
261
271
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
262
- /// assert!( url.set_username("username"));
272
+ /// url.set_username("username").unwrap( );
263
273
/// assert_eq!(url.href(), "https://[email protected] /");
264
274
/// ```
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 ( ) ) } )
267
278
}
268
279
269
280
/// Return the password for this URL, if any, as a percent-encoded ASCII string.
@@ -281,17 +292,17 @@ impl Url {
281
292
}
282
293
283
294
/// Updates the `password` of the URL.
284
- /// Returns true if operation is successful.
285
295
///
286
296
/// ```
287
297
/// use ada_url::Url;
288
298
///
289
299
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
290
- /// assert!( url.set_password("password"));
300
+ /// url.set_password("password").unwrap( );
291
301
/// assert_eq!(url.href(), "https://:[email protected] /");
292
302
/// ```
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 ( ) ) } )
295
306
}
296
307
297
308
/// Return the port number for this URL, or an empty string.
@@ -312,17 +323,17 @@ impl Url {
312
323
}
313
324
314
325
/// Updates the `port` of the URL.
315
- /// Returns true if operation is successful.
316
326
///
317
327
/// ```
318
328
/// use ada_url::Url;
319
329
///
320
330
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
321
- /// assert!( url.set_port("8080"));
331
+ /// url.set_port("8080").unwrap( );
322
332
/// assert_eq!(url.href(), "https://yagiz.co:8080/");
323
333
/// ```
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 ( ) ) } )
326
337
}
327
338
328
339
/// Return this URL’s fragment identifier, or an empty string.
@@ -374,17 +385,17 @@ impl Url {
374
385
}
375
386
376
387
/// Updates the `host` of the URL.
377
- /// Returns true if operation is successful.
378
388
///
379
389
/// ```
380
390
/// use ada_url::Url;
381
391
///
382
392
/// 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( );
384
394
/// assert_eq!(url.href(), "https://localhost:3000/");
385
395
/// ```
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 ( ) ) } )
388
399
}
389
400
390
401
/// Return the parsed representation of the host for this URL. Non-ASCII domain labels are
@@ -406,17 +417,17 @@ impl Url {
406
417
}
407
418
408
419
/// Updates the `hostname` of the URL.
409
- /// Returns true if operation is successful.
410
420
///
411
421
/// ```
412
422
/// use ada_url::Url;
413
423
///
414
424
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
415
- /// assert!( url.set_hostname("localhost"));
425
+ /// url.set_hostname("localhost").unwrap( );
416
426
/// assert_eq!(url.href(), "https://localhost/");
417
427
/// ```
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 ( ) ) } )
420
431
}
421
432
422
433
/// Return the path for this URL, as a percent-encoded ASCII string.
@@ -434,17 +445,17 @@ impl Url {
434
445
}
435
446
436
447
/// Updates the `pathname` of the URL.
437
- /// Returns true if operation is successful.
438
448
///
439
449
/// ```
440
450
/// use ada_url::Url;
441
451
///
442
452
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
443
- /// assert!( url.set_pathname("/contact"));
453
+ /// url.set_pathname("/contact").unwrap( );
444
454
/// assert_eq!(url.href(), "https://yagiz.co/contact");
445
455
/// ```
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 ( ) ) } )
448
459
}
449
460
450
461
/// Return this URL’s query string, if any, as a percent-encoded ASCII string.
@@ -492,17 +503,17 @@ impl Url {
492
503
}
493
504
494
505
/// Updates the `protocol` of the URL.
495
- /// Returns true if operation is successful.
496
506
///
497
507
/// ```
498
508
/// use ada_url::Url;
499
509
///
500
510
/// let mut url = Url::parse("http://yagiz.co", None).expect("Invalid URL");
501
- /// assert!( url.set_protocol("http"));
511
+ /// url.set_protocol("http").unwrap( );
502
512
/// assert_eq!(url.href(), "http://yagiz.co/");
503
513
/// ```
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 ( ) ) } )
506
517
}
507
518
508
519
/// A URL includes credentials if its username or password is not the empty string.
@@ -827,31 +838,31 @@ mod test {
827
838
"https://username:[email protected] :9090/search?query#hash"
828
839
) ;
829
840
830
- assert ! ( out. set_username( "new-username" ) ) ;
841
+ out. set_username ( "new-username" ) . unwrap ( ) ;
831
842
assert_eq ! ( out. username( ) , "new-username" ) ;
832
843
833
- assert ! ( out. set_password( "new-password" ) ) ;
844
+ out. set_password ( "new-password" ) . unwrap ( ) ;
834
845
assert_eq ! ( out. password( ) , "new-password" ) ;
835
846
836
- assert ! ( out. set_port( "4242" ) ) ;
847
+ out. set_port ( "4242" ) . unwrap ( ) ;
837
848
assert_eq ! ( out. port( ) , "4242" ) ;
838
849
839
850
out. set_hash ( "#new-hash" ) ;
840
851
assert_eq ! ( out. hash( ) , "#new-hash" ) ;
841
852
842
- assert ! ( out. set_host( "yagiz.co:9999" ) ) ;
853
+ out. set_host ( "yagiz.co:9999" ) . unwrap ( ) ;
843
854
assert_eq ! ( out. host( ) , "yagiz.co:9999" ) ;
844
855
845
- assert ! ( out. set_hostname( "domain.com" ) ) ;
856
+ out. set_hostname ( "domain.com" ) . unwrap ( ) ;
846
857
assert_eq ! ( out. hostname( ) , "domain.com" ) ;
847
858
848
- assert ! ( out. set_pathname( "/new-search" ) ) ;
859
+ out. set_pathname ( "/new-search" ) . unwrap ( ) ;
849
860
assert_eq ! ( out. pathname( ) , "/new-search" ) ;
850
861
851
862
out. set_search ( "updated-query" ) ;
852
863
assert_eq ! ( out. search( ) , "?updated-query" ) ;
853
864
854
- out. set_protocol ( "wss" ) ;
865
+ out. set_protocol ( "wss" ) . unwrap ( ) ;
855
866
assert_eq ! ( out. protocol( ) , "wss:" ) ;
856
867
857
868
assert ! ( out. has_credentials( ) ) ;
@@ -886,7 +897,7 @@ mod test {
886
897
fn should_clone ( ) {
887
898
let first = Url :: parse ( "https://lemire.me" , None ) . unwrap ( ) ;
888
899
let mut second = first. clone ( ) ;
889
- second. set_href ( "https://yagiz.co" ) ;
900
+ second. set_href ( "https://yagiz.co" ) . unwrap ( ) ;
890
901
assert_ne ! ( first. href( ) , second. href( ) ) ;
891
902
assert_eq ! ( first. href( ) , "https://lemire.me/" ) ;
892
903
assert_eq ! ( second. href( ) , "https://yagiz.co/" ) ;
0 commit comments