@@ -274,12 +274,18 @@ impl Url {
274
274
/// use ada_url::Url;
275
275
///
276
276
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
277
- /// url.set_username("username").unwrap();
277
+ /// url.set_username(Some( "username") ).unwrap();
278
278
/// assert_eq!(url.href(), "https://[email protected] /");
279
279
/// ```
280
280
#[ allow( clippy:: result_unit_err) ]
281
- pub fn set_username ( & mut self , input : & str ) -> SetterResult {
282
- setter_result ( unsafe { ffi:: ada_set_username ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) } )
281
+ pub fn set_username ( & mut self , input : Option < & str > ) -> SetterResult {
282
+ setter_result ( unsafe {
283
+ ffi:: ada_set_username (
284
+ self . 0 ,
285
+ input. unwrap_or ( "" ) . as_ptr ( ) . cast ( ) ,
286
+ input. map_or ( 0 , |i| i. len ( ) ) ,
287
+ )
288
+ } )
283
289
}
284
290
285
291
/// Return the password for this URL, if any, as a percent-encoded ASCII string.
@@ -302,12 +308,18 @@ impl Url {
302
308
/// use ada_url::Url;
303
309
///
304
310
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
305
- /// url.set_password("password").unwrap();
311
+ /// url.set_password(Some( "password") ).unwrap();
306
312
/// assert_eq!(url.href(), "https://:[email protected] /");
307
313
/// ```
308
314
#[ allow( clippy:: result_unit_err) ]
309
- pub fn set_password ( & mut self , input : & str ) -> SetterResult {
310
- setter_result ( unsafe { ffi:: ada_set_password ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) } )
315
+ pub fn set_password ( & mut self , input : Option < & str > ) -> SetterResult {
316
+ setter_result ( unsafe {
317
+ ffi:: ada_set_password (
318
+ self . 0 ,
319
+ input. unwrap_or ( "" ) . as_ptr ( ) . cast ( ) ,
320
+ input. map_or ( 0 , |i| i. len ( ) ) ,
321
+ )
322
+ } )
311
323
}
312
324
313
325
/// Return the port number for this URL, or an empty string.
@@ -333,12 +345,20 @@ impl Url {
333
345
/// use ada_url::Url;
334
346
///
335
347
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
336
- /// url.set_port("8080").unwrap();
348
+ /// url.set_port(Some( "8080") ).unwrap();
337
349
/// assert_eq!(url.href(), "https://yagiz.co:8080/");
338
350
/// ```
339
351
#[ allow( clippy:: result_unit_err) ]
340
- pub fn set_port ( & mut self , input : & str ) -> SetterResult {
341
- setter_result ( unsafe { ffi:: ada_set_port ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) } )
352
+ pub fn set_port ( & mut self , input : Option < & str > ) -> SetterResult {
353
+ match input {
354
+ Some ( value) => setter_result ( unsafe {
355
+ ffi:: ada_set_port ( self . 0 , value. as_ptr ( ) . cast ( ) , value. len ( ) )
356
+ } ) ,
357
+ None => {
358
+ unsafe { ffi:: ada_clear_port ( self . 0 ) }
359
+ Ok ( ( ) )
360
+ }
361
+ }
342
362
}
343
363
344
364
/// Return this URL’s fragment identifier, or an empty string.
@@ -368,11 +388,14 @@ impl Url {
368
388
/// use ada_url::Url;
369
389
///
370
390
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
371
- /// url.set_hash("this-is-my-hash");
391
+ /// url.set_hash(Some( "this-is-my-hash") );
372
392
/// assert_eq!(url.href(), "https://yagiz.co/#this-is-my-hash");
373
393
/// ```
374
- pub fn set_hash ( & mut self , input : & str ) {
375
- unsafe { ffi:: ada_set_hash ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
394
+ pub fn set_hash ( & mut self , input : Option < & str > ) {
395
+ match input {
396
+ Some ( value) => unsafe { ffi:: ada_set_hash ( self . 0 , value. as_ptr ( ) . cast ( ) , value. len ( ) ) } ,
397
+ None => unsafe { ffi:: ada_clear_hash ( self . 0 ) } ,
398
+ }
376
399
}
377
400
378
401
/// Return the parsed representation of the host for this URL with an optional port number.
@@ -395,12 +418,18 @@ impl Url {
395
418
/// use ada_url::Url;
396
419
///
397
420
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
398
- /// url.set_host("localhost:3000").unwrap();
421
+ /// url.set_host(Some( "localhost:3000") ).unwrap();
399
422
/// assert_eq!(url.href(), "https://localhost:3000/");
400
423
/// ```
401
424
#[ allow( clippy:: result_unit_err) ]
402
- pub fn set_host ( & mut self , input : & str ) -> SetterResult {
403
- setter_result ( unsafe { ffi:: ada_set_host ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) } )
425
+ pub fn set_host ( & mut self , input : Option < & str > ) -> SetterResult {
426
+ setter_result ( unsafe {
427
+ ffi:: ada_set_host (
428
+ self . 0 ,
429
+ input. unwrap_or ( "" ) . as_ptr ( ) . cast ( ) ,
430
+ input. map_or ( 0 , |i| i. len ( ) ) ,
431
+ )
432
+ } )
404
433
}
405
434
406
435
/// Return the parsed representation of the host for this URL. Non-ASCII domain labels are
@@ -427,12 +456,18 @@ impl Url {
427
456
/// use ada_url::Url;
428
457
///
429
458
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
430
- /// url.set_hostname("localhost").unwrap();
459
+ /// url.set_hostname(Some( "localhost") ).unwrap();
431
460
/// assert_eq!(url.href(), "https://localhost/");
432
461
/// ```
433
462
#[ allow( clippy:: result_unit_err) ]
434
- pub fn set_hostname ( & mut self , input : & str ) -> SetterResult {
435
- setter_result ( unsafe { ffi:: ada_set_hostname ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) } )
463
+ pub fn set_hostname ( & mut self , input : Option < & str > ) -> SetterResult {
464
+ setter_result ( unsafe {
465
+ ffi:: ada_set_hostname (
466
+ self . 0 ,
467
+ input. unwrap_or ( "" ) . as_ptr ( ) . cast ( ) ,
468
+ input. map_or ( 0 , |i| i. len ( ) ) ,
469
+ )
470
+ } )
436
471
}
437
472
438
473
/// Return the path for this URL, as a percent-encoded ASCII string.
@@ -455,12 +490,18 @@ impl Url {
455
490
/// use ada_url::Url;
456
491
///
457
492
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
458
- /// url.set_pathname("/contact").unwrap();
493
+ /// url.set_pathname(Some( "/contact") ).unwrap();
459
494
/// assert_eq!(url.href(), "https://yagiz.co/contact");
460
495
/// ```
461
496
#[ allow( clippy:: result_unit_err) ]
462
- pub fn set_pathname ( & mut self , input : & str ) -> SetterResult {
463
- setter_result ( unsafe { ffi:: ada_set_pathname ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) } )
497
+ pub fn set_pathname ( & mut self , input : Option < & str > ) -> SetterResult {
498
+ setter_result ( unsafe {
499
+ ffi:: ada_set_pathname (
500
+ self . 0 ,
501
+ input. unwrap_or ( "" ) . as_ptr ( ) . cast ( ) ,
502
+ input. map_or ( 0 , |i| i. len ( ) ) ,
503
+ )
504
+ } )
464
505
}
465
506
466
507
/// Return this URL’s query string, if any, as a percent-encoded ASCII string.
@@ -486,11 +527,16 @@ impl Url {
486
527
/// use ada_url::Url;
487
528
///
488
529
/// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
489
- /// url.set_search("?page=1");
530
+ /// url.set_search(Some( "?page=1") );
490
531
/// assert_eq!(url.href(), "https://yagiz.co/?page=1");
491
532
/// ```
492
- pub fn set_search ( & mut self , input : & str ) {
493
- unsafe { ffi:: ada_set_search ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
533
+ pub fn set_search ( & mut self , input : Option < & str > ) {
534
+ match input {
535
+ Some ( value) => unsafe {
536
+ ffi:: ada_set_search ( self . 0 , value. as_ptr ( ) . cast ( ) , value. len ( ) )
537
+ } ,
538
+ None => unsafe { ffi:: ada_clear_search ( self . 0 ) } ,
539
+ }
494
540
}
495
541
496
542
/// Return the scheme of this URL, lower-cased, as an ASCII string with the ‘:’ delimiter.
@@ -845,28 +891,32 @@ mod test {
845
891
"https://username:[email protected] :9090/search?query#hash"
846
892
) ;
847
893
848
- out. set_username ( "new-username" ) . unwrap ( ) ;
894
+ out. set_username ( Some ( "new-username" ) ) . unwrap ( ) ;
849
895
assert_eq ! ( out. username( ) , "new-username" ) ;
850
896
851
- out. set_password ( "new-password" ) . unwrap ( ) ;
897
+ out. set_password ( Some ( "new-password" ) ) . unwrap ( ) ;
852
898
assert_eq ! ( out. password( ) , "new-password" ) ;
853
899
854
- out. set_port ( "4242" ) . unwrap ( ) ;
900
+ out. set_port ( Some ( "4242" ) ) . unwrap ( ) ;
855
901
assert_eq ! ( out. port( ) , "4242" ) ;
902
+ out. set_port ( None ) . unwrap ( ) ;
903
+ assert_eq ! ( out. port( ) , "" ) ;
856
904
857
- out. set_hash ( "#new-hash" ) ;
905
+ out. set_hash ( Some ( "#new-hash" ) ) ;
858
906
assert_eq ! ( out. hash( ) , "#new-hash" ) ;
859
907
860
- out. set_host ( "yagiz.co:9999" ) . unwrap ( ) ;
908
+ out. set_host ( Some ( "yagiz.co:9999" ) ) . unwrap ( ) ;
861
909
assert_eq ! ( out. host( ) , "yagiz.co:9999" ) ;
862
910
863
- out. set_hostname ( "domain.com" ) . unwrap ( ) ;
911
+ out. set_hostname ( Some ( "domain.com" ) ) . unwrap ( ) ;
864
912
assert_eq ! ( out. hostname( ) , "domain.com" ) ;
865
913
866
- out. set_pathname ( "/new-search" ) . unwrap ( ) ;
914
+ out. set_pathname ( Some ( "/new-search" ) ) . unwrap ( ) ;
867
915
assert_eq ! ( out. pathname( ) , "/new-search" ) ;
916
+ out. set_pathname ( None ) . unwrap ( ) ;
917
+ assert_eq ! ( out. pathname( ) , "/" ) ;
868
918
869
- out. set_search ( "updated-query" ) ;
919
+ out. set_search ( Some ( "updated-query" ) ) ;
870
920
assert_eq ! ( out. search( ) , "?updated-query" ) ;
871
921
872
922
out. set_protocol ( "wss" ) . unwrap ( ) ;
0 commit comments