@@ -156,30 +156,26 @@ impl PyUrl {
156
156
}
157
157
158
158
#[ classmethod]
159
- #[ pyo3( signature=( * , scheme, host, user =None , password=None , port=None , path=None , query=None , fragment=None ) ) ]
159
+ #[ pyo3( signature=( * , scheme, host, username =None , password=None , port=None , path=None , query=None , fragment=None ) ) ]
160
160
#[ allow( clippy:: too_many_arguments) ]
161
161
pub fn build < ' a > (
162
162
cls : & ' a PyType ,
163
163
scheme : & str ,
164
164
host : & str ,
165
- user : Option < & str > ,
165
+ username : Option < & str > ,
166
166
password : Option < & str > ,
167
167
port : Option < u16 > ,
168
168
path : Option < & str > ,
169
169
query : Option < & str > ,
170
170
fragment : Option < & str > ,
171
171
) -> PyResult < & ' a PyAny > {
172
- let user_password = match ( user , password ) {
173
- ( Some ( user ) , None ) => format ! ( "{user}@" ) ,
174
- ( None , Some ( password) ) => format ! ( ":{password}@" ) ,
175
- ( Some ( user ) , Some ( password ) ) => format ! ( "{user}:{password}@" ) ,
176
- ( None , None ) => String :: new ( ) ,
172
+ let url_host = UrlHostParts {
173
+ username : username . map ( Into :: into ) ,
174
+ password : password . map ( Into :: into ) ,
175
+ host : Some ( host . into ( ) ) ,
176
+ port ,
177
177
} ;
178
- let mut url = format ! ( "{scheme}://{user_password}{host}" ) ;
179
- if let Some ( port) = port {
180
- url. push ( ':' ) ;
181
- url. push_str ( & format ! ( "{port}" ) ) ;
182
- }
178
+ let mut url = format ! ( "{scheme}://{url_host}" ) ;
183
179
if let Some ( path) = path {
184
180
url. push ( '/' ) ;
185
181
url. push_str ( path) ;
@@ -360,52 +356,53 @@ impl PyMultiHostUrl {
360
356
}
361
357
362
358
#[ classmethod]
363
- #[ pyo3( signature=( * , scheme, hosts=None , path=None , query=None , fragment=None , host=None , user =None , password=None , port=None ) ) ]
359
+ #[ pyo3( signature=( * , scheme, hosts=None , path=None , query=None , fragment=None , host=None , username =None , password=None , port=None ) ) ]
364
360
#[ allow( clippy:: too_many_arguments) ]
365
361
pub fn build < ' a > (
366
362
cls : & ' a PyType ,
367
363
scheme : & str ,
368
- hosts : Option < Vec < MultiHostUrlHost > > ,
364
+ hosts : Option < Vec < UrlHostParts > > ,
369
365
path : Option < & str > ,
370
366
query : Option < & str > ,
371
367
fragment : Option < & str > ,
372
368
// convenience parameters to build with a single host
373
369
host : Option < & str > ,
374
- user : Option < & str > ,
370
+ username : Option < & str > ,
375
371
password : Option < & str > ,
376
372
port : Option < u16 > ,
377
373
) -> PyResult < & ' a PyAny > {
378
- let mut url = if hosts. is_some ( ) && ( host. is_some ( ) || user. is_some ( ) || password. is_some ( ) || port. is_some ( ) ) {
379
- return Err ( PyValueError :: new_err (
380
- "expected one of `hosts` or singular values to be set." ,
381
- ) ) ;
382
- } else if let Some ( hosts) = hosts {
383
- // check all of host / user / password / port empty
384
- // build multi-host url
385
- let mut multi_url = format ! ( "{scheme}://" ) ;
386
- for ( index, single_host) in hosts. iter ( ) . enumerate ( ) {
387
- if single_host. is_empty ( ) {
388
- return Err ( PyValueError :: new_err (
389
- "expected one of 'host', 'username', 'password' or 'port' to be set" ,
390
- ) ) ;
374
+ let mut url =
375
+ if hosts. is_some ( ) && ( host. is_some ( ) || username. is_some ( ) || password. is_some ( ) || port. is_some ( ) ) {
376
+ return Err ( PyValueError :: new_err (
377
+ "expected one of `hosts` or singular values to be set." ,
378
+ ) ) ;
379
+ } else if let Some ( hosts) = hosts {
380
+ // check all of host / user / password / port empty
381
+ // build multi-host url
382
+ let mut multi_url = format ! ( "{scheme}://" ) ;
383
+ for ( index, single_host) in hosts. iter ( ) . enumerate ( ) {
384
+ if single_host. is_empty ( ) {
385
+ return Err ( PyValueError :: new_err (
386
+ "expected one of 'host', 'username', 'password' or 'port' to be set" ,
387
+ ) ) ;
388
+ }
389
+ multi_url. push_str ( & single_host. to_string ( ) ) ;
390
+ if index != hosts. len ( ) - 1 {
391
+ multi_url. push ( ',' ) ;
392
+ } ;
391
393
}
392
- multi_url. push_str ( & single_host. to_string ( ) ) ;
393
- if index != hosts. len ( ) - 1 {
394
- multi_url. push ( ',' ) ;
394
+ multi_url
395
+ } else if host. is_some ( ) {
396
+ let url_host = UrlHostParts {
397
+ username : username. map ( Into :: into) ,
398
+ password : password. map ( Into :: into) ,
399
+ host : host. map ( Into :: into) ,
400
+ port : port. map ( Into :: into) ,
395
401
} ;
396
- }
397
- multi_url
398
- } else if host. is_some ( ) {
399
- let url_host = MultiHostUrlHost {
400
- username : user. map ( Into :: into) ,
401
- password : password. map ( Into :: into) ,
402
- host : host. map ( Into :: into) ,
403
- port : port. map ( Into :: into) ,
402
+ format ! ( "{scheme}://{url_host}" )
403
+ } else {
404
+ return Err ( PyValueError :: new_err ( "expected either `host` or `hosts` to be set" ) ) ;
404
405
} ;
405
- format ! ( "{scheme}://{url_host}" )
406
- } else {
407
- return Err ( PyValueError :: new_err ( "expected either `host` or `hosts` to be set" ) ) ;
408
- } ;
409
406
410
407
if let Some ( path) = path {
411
408
url. push ( '/' ) ;
@@ -423,24 +420,24 @@ impl PyMultiHostUrl {
423
420
}
424
421
}
425
422
426
- pub struct MultiHostUrlHost {
423
+ pub struct UrlHostParts {
427
424
username : Option < String > ,
428
425
password : Option < String > ,
429
426
host : Option < String > ,
430
427
port : Option < u16 > ,
431
428
}
432
429
433
- impl MultiHostUrlHost {
430
+ impl UrlHostParts {
434
431
fn is_empty ( & self ) -> bool {
435
432
self . host . is_none ( ) && self . password . is_none ( ) && self . host . is_none ( ) && self . port . is_none ( )
436
433
}
437
434
}
438
435
439
- impl FromPyObject < ' _ > for MultiHostUrlHost {
436
+ impl FromPyObject < ' _ > for UrlHostParts {
440
437
fn extract ( ob : & ' _ PyAny ) -> PyResult < Self > {
441
438
let py = ob. py ( ) ;
442
439
let dict = ob. downcast :: < PyDict > ( ) ?;
443
- Ok ( MultiHostUrlHost {
440
+ Ok ( UrlHostParts {
444
441
username : dict. get_as ( intern ! ( py, "username" ) ) ?,
445
442
password : dict. get_as ( intern ! ( py, "password" ) ) ?,
446
443
host : dict. get_as ( intern ! ( py, "host" ) ) ?,
@@ -449,16 +446,30 @@ impl FromPyObject<'_> for MultiHostUrlHost {
449
446
}
450
447
}
451
448
452
- impl fmt:: Display for MultiHostUrlHost {
449
+ impl fmt:: Display for UrlHostParts {
453
450
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
454
- if self . username . is_some ( ) && self . password . is_some ( ) {
455
- write ! (
456
- f,
457
- "{}:{}" ,
458
- self . username. as_ref( ) . unwrap( ) ,
459
- self . password. as_ref( ) . unwrap( )
460
- ) ?;
461
- }
451
+ let _ = match self {
452
+ UrlHostParts {
453
+ username : Some ( username) ,
454
+ password : None ,
455
+ ..
456
+ } => write ! ( f, "{username}" ) ,
457
+ UrlHostParts {
458
+ username : None ,
459
+ password : Some ( password) ,
460
+ ..
461
+ } => write ! ( f, ":{password}" ) ,
462
+ UrlHostParts {
463
+ username : Some ( username) ,
464
+ password : Some ( password) ,
465
+ ..
466
+ } => write ! ( f, "{username}:{password}" ) ,
467
+ UrlHostParts {
468
+ username : None ,
469
+ password : None ,
470
+ ..
471
+ } => Ok ( ( ) ) ,
472
+ } ;
462
473
if self . host . is_some ( ) {
463
474
write ! ( f, "@{}" , self . host. as_ref( ) . unwrap( ) ) ?;
464
475
}
0 commit comments