Skip to content

Commit b9d2402

Browse files
committed
Inline the contents of UrlPatternResult
1 parent 4437fe8 commit b9d2402

File tree

1 file changed

+56
-102
lines changed

1 file changed

+56
-102
lines changed

src/lib.rs

Lines changed: 56 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#![allow(non_snake_case)]
22

33
use pyo3::{
4-
BoundObject,
54
exceptions::{PyTypeError, PyValueError},
65
prelude::*,
76
types::{PyDict, PyList, PyString},
87
};
9-
use std::collections::HashMap;
108

119
#[derive(FromPyObject)]
1210
enum UrlPatternInput<'py> {
@@ -227,19 +225,21 @@ impl UrlPattern {
227225
py: Python<'py>,
228226
input: Option<&Bound<'py, PyAny>>,
229227
baseURL: Option<&Bound<'py, PyString>>,
230-
) -> PyResult<Option<UrlPatternResult<'py>>> {
228+
) -> PyResult<Option<Bound<'py, PyDict>>> {
231229
let base_url = baseURL;
232230

233-
let urlpattern_input: Option<UrlPatternInput> = input.map(|i| i.extract()).transpose()?;
234-
let input: ::urlpattern::UrlPatternMatchInput = match &urlpattern_input {
231+
let urlpattern_input: ::urlpattern::UrlPatternMatchInput = match input
232+
.map(|i| i.extract())
233+
.transpose()?
234+
{
235235
Some(UrlPatternInput::String(input)) => match base_url {
236236
Some(base_url) => {
237237
let base_url = match url::Url::parse(base_url.to_str()?) {
238238
Ok(url) => url,
239239
Err(_) => return Ok(None),
240240
};
241241
::urlpattern::UrlPatternMatchInput::Url(
242-
match url::Url::options().base_url(Some(&base_url)).parse(input) {
242+
match url::Url::options().base_url(Some(&base_url)).parse(&input) {
243243
Ok(url) => url,
244244
Err(_) => return Ok(None),
245245
},
@@ -303,54 +303,59 @@ impl UrlPattern {
303303
}
304304
};
305305

306-
let Some(result) = self.0.exec(input).map_err(Error)? else {
306+
let Some(urlpattern_result) = self.0.exec(urlpattern_input).map_err(Error)? else {
307307
return Ok(None);
308308
};
309309

310-
Ok(Some(UrlPatternResult {
311-
inputs: {
312-
let mut vec = Vec::new();
313-
vec.push(
314-
urlpattern_input.unwrap_or(UrlPatternInput::Init(PyDict::new(py).into_bound())),
315-
);
316-
if let Some(base_url) = base_url {
317-
vec.push(UrlPatternInput::String(base_url.to_string()));
318-
}
319-
vec
320-
},
321-
protocol: UrlPatternComponentResult {
322-
input: result.protocol.input,
323-
groups: result.protocol.groups,
324-
},
325-
username: UrlPatternComponentResult {
326-
input: result.username.input,
327-
groups: result.username.groups,
328-
},
329-
password: UrlPatternComponentResult {
330-
input: result.password.input,
331-
groups: result.password.groups,
332-
},
333-
hostname: UrlPatternComponentResult {
334-
input: result.hostname.input,
335-
groups: result.hostname.groups,
336-
},
337-
port: UrlPatternComponentResult {
338-
input: result.port.input,
339-
groups: result.port.groups,
340-
},
341-
pathname: UrlPatternComponentResult {
342-
input: result.pathname.input,
343-
groups: result.pathname.groups,
344-
},
345-
search: UrlPatternComponentResult {
346-
input: result.search.input,
347-
groups: result.search.groups,
348-
},
349-
hash: UrlPatternComponentResult {
350-
input: result.hash.input,
351-
groups: result.hash.groups,
352-
},
353-
}))
310+
let result = PyDict::new(py);
311+
312+
let inputs = PyList::new(py, vec![input.unwrap_or(&PyDict::new(py))])?;
313+
if let Some(base_url) = base_url {
314+
inputs.append(base_url)?;
315+
}
316+
result.set_item("inputs", inputs)?;
317+
318+
let protocol = PyDict::new(py);
319+
protocol.set_item("input", urlpattern_result.protocol.input)?;
320+
protocol.set_item("groups", urlpattern_result.protocol.groups)?;
321+
result.set_item("protocol", protocol)?;
322+
323+
let username = PyDict::new(py);
324+
username.set_item("input", urlpattern_result.username.input)?;
325+
username.set_item("groups", urlpattern_result.username.groups)?;
326+
result.set_item("username", username)?;
327+
328+
let password = PyDict::new(py);
329+
password.set_item("input", urlpattern_result.password.input)?;
330+
password.set_item("groups", urlpattern_result.password.groups)?;
331+
result.set_item("password", password)?;
332+
333+
let hostname = PyDict::new(py);
334+
hostname.set_item("input", urlpattern_result.hostname.input)?;
335+
hostname.set_item("groups", urlpattern_result.hostname.groups)?;
336+
result.set_item("hostname", hostname)?;
337+
338+
let port = PyDict::new(py);
339+
port.set_item("input", urlpattern_result.port.input)?;
340+
port.set_item("groups", urlpattern_result.port.groups)?;
341+
result.set_item("port", port)?;
342+
343+
let pathname = PyDict::new(py);
344+
pathname.set_item("input", urlpattern_result.pathname.input)?;
345+
pathname.set_item("groups", urlpattern_result.pathname.groups)?;
346+
result.set_item("pathname", pathname)?;
347+
348+
let search = PyDict::new(py);
349+
search.set_item("input", urlpattern_result.search.input)?;
350+
search.set_item("groups", urlpattern_result.search.groups)?;
351+
result.set_item("search", search)?;
352+
353+
let hash = PyDict::new(py);
354+
hash.set_item("input", urlpattern_result.hash.input)?;
355+
hash.set_item("groups", urlpattern_result.hash.groups)?;
356+
result.set_item("hash", hash)?;
357+
358+
Ok(Some(result))
354359
}
355360

356361
#[getter]
@@ -399,57 +404,6 @@ impl UrlPattern {
399404
}
400405
}
401406

402-
struct UrlPatternResult<'py> {
403-
inputs: Vec<UrlPatternInput<'py>>,
404-
protocol: UrlPatternComponentResult,
405-
username: UrlPatternComponentResult,
406-
password: UrlPatternComponentResult,
407-
hostname: UrlPatternComponentResult,
408-
port: UrlPatternComponentResult,
409-
pathname: UrlPatternComponentResult,
410-
search: UrlPatternComponentResult,
411-
hash: UrlPatternComponentResult,
412-
}
413-
414-
impl<'py> IntoPyObject<'py> for UrlPatternResult<'py> {
415-
type Target = PyDict;
416-
type Output = Bound<'py, Self::Target>;
417-
type Error = PyErr;
418-
419-
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
420-
let dict = PyDict::new(py);
421-
422-
let inputs = PyList::empty(py);
423-
for input in self.inputs {
424-
match input {
425-
UrlPatternInput::String(string) => {
426-
inputs.append(string)?;
427-
}
428-
UrlPatternInput::Init(init) => {
429-
inputs.append(init)?;
430-
}
431-
}
432-
}
433-
434-
dict.set_item("inputs", inputs)?;
435-
dict.set_item("protocol", self.protocol)?;
436-
dict.set_item("username", self.username)?;
437-
dict.set_item("password", self.password)?;
438-
dict.set_item("hostname", self.hostname)?;
439-
dict.set_item("port", self.port)?;
440-
dict.set_item("pathname", self.pathname)?;
441-
dict.set_item("search", self.search)?;
442-
dict.set_item("hash", self.hash)?;
443-
Ok(dict.into_bound())
444-
}
445-
}
446-
447-
#[derive(IntoPyObject, IntoPyObjectRef)]
448-
struct UrlPatternComponentResult {
449-
input: String,
450-
groups: HashMap<String, Option<String>>,
451-
}
452-
453407
struct Error(::urlpattern::Error);
454408

455409
impl From<Error> for PyErr {

0 commit comments

Comments
 (0)