|
1 | 1 | #![allow(non_snake_case)] |
2 | 2 |
|
3 | 3 | use pyo3::{ |
4 | | - BoundObject, |
5 | 4 | exceptions::{PyTypeError, PyValueError}, |
6 | 5 | prelude::*, |
7 | 6 | types::{PyDict, PyList, PyString}, |
8 | 7 | }; |
9 | | -use std::collections::HashMap; |
10 | 8 |
|
11 | 9 | #[derive(FromPyObject)] |
12 | 10 | enum UrlPatternInput<'py> { |
@@ -227,19 +225,21 @@ impl UrlPattern { |
227 | 225 | py: Python<'py>, |
228 | 226 | input: Option<&Bound<'py, PyAny>>, |
229 | 227 | baseURL: Option<&Bound<'py, PyString>>, |
230 | | - ) -> PyResult<Option<UrlPatternResult<'py>>> { |
| 228 | + ) -> PyResult<Option<Bound<'py, PyDict>>> { |
231 | 229 | let base_url = baseURL; |
232 | 230 |
|
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 | + { |
235 | 235 | Some(UrlPatternInput::String(input)) => match base_url { |
236 | 236 | Some(base_url) => { |
237 | 237 | let base_url = match url::Url::parse(base_url.to_str()?) { |
238 | 238 | Ok(url) => url, |
239 | 239 | Err(_) => return Ok(None), |
240 | 240 | }; |
241 | 241 | ::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) { |
243 | 243 | Ok(url) => url, |
244 | 244 | Err(_) => return Ok(None), |
245 | 245 | }, |
@@ -303,54 +303,59 @@ impl UrlPattern { |
303 | 303 | } |
304 | 304 | }; |
305 | 305 |
|
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 { |
307 | 307 | return Ok(None); |
308 | 308 | }; |
309 | 309 |
|
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)) |
354 | 359 | } |
355 | 360 |
|
356 | 361 | #[getter] |
@@ -399,57 +404,6 @@ impl UrlPattern { |
399 | 404 | } |
400 | 405 | } |
401 | 406 |
|
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 | | - |
453 | 407 | struct Error(::urlpattern::Error); |
454 | 408 |
|
455 | 409 | impl From<Error> for PyErr { |
|
0 commit comments