Skip to content

Commit 6911297

Browse files
committed
fixing tests
1 parent 83c1fee commit 6911297

File tree

2 files changed

+10
-28
lines changed

2 files changed

+10
-28
lines changed

src/url.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ use std::hash::{Hash, Hasher};
55

66
use idna::punycode::decode_to_string;
77
use pyo3::exceptions::PyValueError;
8+
use pyo3::prelude::*;
89
use pyo3::pyclass::CompareOp;
910
use pyo3::sync::GILOnceCell;
1011
use pyo3::types::{PyDict, PyType};
11-
use pyo3::{intern, prelude::*};
1212
use url::Url;
1313

14-
use crate::tools::SchemaDict;
1514
use crate::SchemaValidator;
1615

1716
static SCHEMA_DEFINITION_URL: GILOnceCell<SchemaValidator> = GILOnceCell::new();
@@ -423,6 +422,9 @@ impl PyMultiHostUrl {
423422
}
424423
}
425424

425+
#[derive(FromPyObject)]
426+
#[pyo3(from_item_all)]
427+
#[cfg_attr(debug_assertions, derive(Debug))]
426428
pub struct UrlHostParts {
427429
username: Option<String>,
428430
password: Option<String>,
@@ -436,19 +438,6 @@ impl UrlHostParts {
436438
}
437439
}
438440

439-
impl FromPyObject<'_> for UrlHostParts {
440-
fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<Self> {
441-
let py = ob.py();
442-
let dict = ob.downcast::<PyDict>()?;
443-
Ok(UrlHostParts {
444-
username: dict.get_as(intern!(py, "username"))?,
445-
password: dict.get_as(intern!(py, "password"))?,
446-
host: dict.get_as(intern!(py, "host"))?,
447-
port: dict.get_as(intern!(py, "port"))?,
448-
})
449-
}
450-
}
451-
452441
impl fmt::Display for UrlHostParts {
453442
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
454443
match (&self.username, &self.password) {

src/validators/url.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::errors::{ErrorType, ErrorTypeDefaults, ValError, ValResult};
1515
use crate::input::downcast_python_input;
1616
use crate::input::Input;
1717
use crate::tools::SchemaDict;
18-
use crate::url::UrlHostParts;
1918
use crate::url::{schema_is_special, PyMultiHostUrl, PyUrl};
2019

2120
use super::literal::expected_repr_name;
@@ -297,22 +296,16 @@ impl Validator for MultiHostUrlValidator {
297296
EitherMultiHostUrl::Rust(rust_url) => rust_url,
298297
};
299298

300-
let hosts = py_url.hosts(py).map_or(None, |hosts| {
301-
let mut host_parts_vec = Vec::new();
302-
for host in &hosts {
303-
if let Ok(py_dict) = host.downcast::<PyDict>() {
304-
if let Ok(host_parts) = UrlHostParts::extract_bound(py_dict) {
305-
host_parts_vec.push(host_parts);
306-
}
307-
}
308-
}
309-
Some(host_parts_vec)
310-
});
299+
let hosts = py_url
300+
.hosts(py)?
301+
.into_iter()
302+
.map(|host| host.extract().expect("host should be a valid UrlHostParts"))
303+
.collect();
311304

312305
let py_url = PyMultiHostUrl::build(
313306
url_subclass.bind(py),
314307
py_url.scheme(),
315-
hosts,
308+
Some(hosts),
316309
py_url.path().filter(|path| *path != "/"),
317310
py_url.query(),
318311
py_url.fragment(),

0 commit comments

Comments
 (0)