@@ -274,12 +274,18 @@ impl Url {
274274 /// use ada_url::Url;
275275 ///
276276 /// 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();
278278 /// assert_eq!(url.href(), "https://username@yagiz.co/");
279279 /// ```
280280 #[ 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+ } )
283289 }
284290
285291 /// Return the password for this URL, if any, as a percent-encoded ASCII string.
@@ -302,12 +308,18 @@ impl Url {
302308 /// use ada_url::Url;
303309 ///
304310 /// 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();
306312 /// assert_eq!(url.href(), "https://:password@yagiz.co/");
307313 /// ```
308314 #[ 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+ } )
311323 }
312324
313325 /// Return the port number for this URL, or an empty string.
@@ -333,12 +345,20 @@ impl Url {
333345 /// use ada_url::Url;
334346 ///
335347 /// 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();
337349 /// assert_eq!(url.href(), "https://yagiz.co:8080/");
338350 /// ```
339351 #[ 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+ }
342362 }
343363
344364 /// Return this URL’s fragment identifier, or an empty string.
@@ -368,11 +388,14 @@ impl Url {
368388 /// use ada_url::Url;
369389 ///
370390 /// 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") );
372392 /// assert_eq!(url.href(), "https://yagiz.co/#this-is-my-hash");
373393 /// ```
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+ }
376399 }
377400
378401 /// Return the parsed representation of the host for this URL with an optional port number.
@@ -395,12 +418,18 @@ impl Url {
395418 /// use ada_url::Url;
396419 ///
397420 /// 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();
399422 /// assert_eq!(url.href(), "https://localhost:3000/");
400423 /// ```
401424 #[ 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+ } )
404433 }
405434
406435 /// Return the parsed representation of the host for this URL. Non-ASCII domain labels are
@@ -427,12 +456,18 @@ impl Url {
427456 /// use ada_url::Url;
428457 ///
429458 /// 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();
431460 /// assert_eq!(url.href(), "https://localhost/");
432461 /// ```
433462 #[ 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+ } )
436471 }
437472
438473 /// Return the path for this URL, as a percent-encoded ASCII string.
@@ -455,12 +490,18 @@ impl Url {
455490 /// use ada_url::Url;
456491 ///
457492 /// 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();
459494 /// assert_eq!(url.href(), "https://yagiz.co/contact");
460495 /// ```
461496 #[ 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+ } )
464505 }
465506
466507 /// Return this URL’s query string, if any, as a percent-encoded ASCII string.
@@ -486,11 +527,16 @@ impl Url {
486527 /// use ada_url::Url;
487528 ///
488529 /// 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") );
490531 /// assert_eq!(url.href(), "https://yagiz.co/?page=1");
491532 /// ```
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+ }
494540 }
495541
496542 /// Return the scheme of this URL, lower-cased, as an ASCII string with the ‘:’ delimiter.
@@ -845,28 +891,32 @@ mod test {
845891 "https://username:password@google.com:9090/search?query#hash"
846892 ) ;
847893
848- out. set_username ( "new-username" ) . unwrap ( ) ;
894+ out. set_username ( Some ( "new-username" ) ) . unwrap ( ) ;
849895 assert_eq ! ( out. username( ) , "new-username" ) ;
850896
851- out. set_password ( "new-password" ) . unwrap ( ) ;
897+ out. set_password ( Some ( "new-password" ) ) . unwrap ( ) ;
852898 assert_eq ! ( out. password( ) , "new-password" ) ;
853899
854- out. set_port ( "4242" ) . unwrap ( ) ;
900+ out. set_port ( Some ( "4242" ) ) . unwrap ( ) ;
855901 assert_eq ! ( out. port( ) , "4242" ) ;
902+ out. set_port ( None ) . unwrap ( ) ;
903+ assert_eq ! ( out. port( ) , "" ) ;
856904
857- out. set_hash ( "#new-hash" ) ;
905+ out. set_hash ( Some ( "#new-hash" ) ) ;
858906 assert_eq ! ( out. hash( ) , "#new-hash" ) ;
859907
860- out. set_host ( "yagiz.co:9999" ) . unwrap ( ) ;
908+ out. set_host ( Some ( "yagiz.co:9999" ) ) . unwrap ( ) ;
861909 assert_eq ! ( out. host( ) , "yagiz.co:9999" ) ;
862910
863- out. set_hostname ( "domain.com" ) . unwrap ( ) ;
911+ out. set_hostname ( Some ( "domain.com" ) ) . unwrap ( ) ;
864912 assert_eq ! ( out. hostname( ) , "domain.com" ) ;
865913
866- out. set_pathname ( "/new-search" ) . unwrap ( ) ;
914+ out. set_pathname ( Some ( "/new-search" ) ) . unwrap ( ) ;
867915 assert_eq ! ( out. pathname( ) , "/new-search" ) ;
916+ out. set_pathname ( None ) . unwrap ( ) ;
917+ assert_eq ! ( out. pathname( ) , "/" ) ;
868918
869- out. set_search ( "updated-query" ) ;
919+ out. set_search ( Some ( "updated-query" ) ) ;
870920 assert_eq ! ( out. search( ) , "?updated-query" ) ;
871921
872922 out. set_protocol ( "wss" ) . unwrap ( ) ;
0 commit comments