@@ -45,33 +45,12 @@ impl From<PyUrl> for Url {
45
45
}
46
46
}
47
47
48
- static SCHEMA_URL_SINGLE_TRUE : GILOnceCell < SchemaValidator > = GILOnceCell :: new ( ) ;
49
- static SCHEMA_URL_SINGLE_FALSE : GILOnceCell < SchemaValidator > = GILOnceCell :: new ( ) ;
50
- static SCHEMA_URL_MULTI_TRUE : GILOnceCell < SchemaValidator > = GILOnceCell :: new ( ) ;
51
- static SCHEMA_URL_MULTI_FALSE : GILOnceCell < SchemaValidator > = GILOnceCell :: new ( ) ;
52
-
53
- fn get_schema_validator ( py : Python < ' _ > , multi_host : bool , add_trailing_slash : bool ) -> & SchemaValidator {
54
- match ( multi_host, add_trailing_slash) {
55
- ( false , true ) => SCHEMA_URL_SINGLE_TRUE . get_or_init ( py, || build_schema_validator ( py, "url" , true ) ) ,
56
- ( false , false ) => SCHEMA_URL_SINGLE_FALSE . get_or_init ( py, || build_schema_validator ( py, "url" , false ) ) ,
57
- ( true , true ) => SCHEMA_URL_MULTI_TRUE . get_or_init ( py, || build_schema_validator ( py, "multi-host-url" , true ) ) ,
58
- ( true , false ) => SCHEMA_URL_MULTI_FALSE . get_or_init ( py, || build_schema_validator ( py, "multi-host-url" , false ) ) ,
59
- }
60
- }
61
-
62
- fn build_schema_validator ( py : Python , schema_type : & str , add_trailing_slash : bool ) -> SchemaValidator {
63
- let schema = PyDict :: new ( py) ;
64
- schema. set_item ( "type" , schema_type) . unwrap ( ) ;
65
- schema. set_item ( "add_trailing_slash" , add_trailing_slash) . unwrap ( ) ;
66
- SchemaValidator :: py_new ( py, & schema, None ) . unwrap ( )
67
- }
68
-
69
48
#[ pymethods]
70
49
impl PyUrl {
71
50
#[ new]
72
51
#[ pyo3( signature = ( url, * , add_trailing_slash=true ) ) ]
73
52
pub fn py_new ( py : Python , url : & Bound < ' _ , PyAny > , add_trailing_slash : bool ) -> PyResult < Self > {
74
- let schema_validator = get_schema_validator ( py, false , add_trailing_slash) ;
53
+ let schema_validator = get_schema_validator ( py, false , add_trailing_slash) ? ;
75
54
let schema_obj = schema_validator. validate_python ( py, url, None , None , None , None , false . into ( ) , None , None ) ?;
76
55
schema_obj. extract ( py)
77
56
}
@@ -250,7 +229,7 @@ impl PyMultiHostUrl {
250
229
#[ new]
251
230
#[ pyo3( signature = ( url, * , add_trailing_slash=true ) ) ]
252
231
pub fn py_new ( py : Python , url : & Bound < ' _ , PyAny > , add_trailing_slash : bool ) -> PyResult < Self > {
253
- let schema_validator = get_schema_validator ( py, true , add_trailing_slash) ;
232
+ let schema_validator = get_schema_validator ( py, true , add_trailing_slash) ? ;
254
233
let schema_obj = schema_validator. validate_python ( py, url, None , None , None , None , false . into ( ) , None , None ) ?;
255
234
schema_obj. extract ( py)
256
235
}
@@ -545,3 +524,29 @@ fn is_punnycode_domain(lib_url: &Url, domain: &str) -> bool {
545
524
pub fn schema_is_special ( schema : & str ) -> bool {
546
525
matches ! ( schema, "http" | "https" | "ws" | "wss" | "ftp" | "file" )
547
526
}
527
+
528
+ static SCHEMA_URL_SINGLE_TRUE : GILOnceCell < SchemaValidator > = GILOnceCell :: new ( ) ;
529
+ static SCHEMA_URL_SINGLE_FALSE : GILOnceCell < SchemaValidator > = GILOnceCell :: new ( ) ;
530
+ static SCHEMA_URL_MULTI_TRUE : GILOnceCell < SchemaValidator > = GILOnceCell :: new ( ) ;
531
+ static SCHEMA_URL_MULTI_FALSE : GILOnceCell < SchemaValidator > = GILOnceCell :: new ( ) ;
532
+
533
+ macro_rules! make_schema_val {
534
+ ( $py: ident, $schema_type: literal, $add_trailing_slash: literal) => { {
535
+ let schema = PyDict :: new( $py) ;
536
+ schema. set_item( intern!( $py, "type" ) , intern!( $py, $schema_type) ) ?;
537
+ // add_trailing_slash defaults to true, so only set it if false
538
+ if !$add_trailing_slash {
539
+ schema. set_item( intern!( $py, "add_trailing_slash" ) , false ) ?;
540
+ }
541
+ SchemaValidator :: py_new( $py, & schema, None )
542
+ } } ;
543
+ }
544
+
545
+ fn get_schema_validator ( py : Python < ' _ > , multi_host : bool , add_trailing_slash : bool ) -> PyResult < & SchemaValidator > {
546
+ match ( multi_host, add_trailing_slash) {
547
+ ( false , true ) => SCHEMA_URL_SINGLE_TRUE . get_or_try_init ( py, || make_schema_val ! ( py, "url" , true ) ) ,
548
+ ( false , false ) => SCHEMA_URL_SINGLE_FALSE . get_or_try_init ( py, || make_schema_val ! ( py, "url" , false ) ) ,
549
+ ( true , true ) => SCHEMA_URL_MULTI_TRUE . get_or_try_init ( py, || make_schema_val ! ( py, "multi-host-url" , true ) ) ,
550
+ ( true , false ) => SCHEMA_URL_MULTI_FALSE . get_or_try_init ( py, || make_schema_val ! ( py, "multi-host-url" , false ) ) ,
551
+ }
552
+ }
0 commit comments