Skip to content

Commit 660b112

Browse files
committed
improve building global sceham validators
1 parent 4e19c63 commit 660b112

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

src/url.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,12 @@ impl From<PyUrl> for Url {
4545
}
4646
}
4747

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-
6948
#[pymethods]
7049
impl PyUrl {
7150
#[new]
7251
#[pyo3(signature = (url, *, add_trailing_slash=true))]
7352
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)?;
7554
let schema_obj = schema_validator.validate_python(py, url, None, None, None, None, false.into(), None, None)?;
7655
schema_obj.extract(py)
7756
}
@@ -250,7 +229,7 @@ impl PyMultiHostUrl {
250229
#[new]
251230
#[pyo3(signature = (url, *, add_trailing_slash=true))]
252231
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)?;
254233
let schema_obj = schema_validator.validate_python(py, url, None, None, None, None, false.into(), None, None)?;
255234
schema_obj.extract(py)
256235
}
@@ -545,3 +524,29 @@ fn is_punnycode_domain(lib_url: &Url, domain: &str) -> bool {
545524
pub fn schema_is_special(schema: &str) -> bool {
546525
matches!(schema, "http" | "https" | "ws" | "wss" | "ftp" | "file")
547526
}
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

Comments
 (0)