2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-01-13 02:51:32 -05:00
|
|
|
|
2023-10-05 08:34:38 -04:00
|
|
|
use deno_core::op2;
|
2021-09-08 05:14:29 -04:00
|
|
|
|
|
|
|
use urlpattern::quirks;
|
|
|
|
use urlpattern::quirks::MatchInput;
|
|
|
|
use urlpattern::quirks::StringOrInit;
|
|
|
|
use urlpattern::quirks::UrlPattern;
|
|
|
|
|
2024-10-14 17:15:31 -04:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
#[error(transparent)]
|
|
|
|
pub struct UrlPatternError(urlpattern::Error);
|
|
|
|
|
2023-10-05 08:34:38 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2021-09-08 05:14:29 -04:00
|
|
|
pub fn op_urlpattern_parse(
|
2023-10-05 08:34:38 -04:00
|
|
|
#[serde] input: StringOrInit,
|
|
|
|
#[string] base_url: Option<String>,
|
2024-08-05 04:49:28 -04:00
|
|
|
#[serde] options: urlpattern::UrlPatternOptions,
|
2024-10-14 17:15:31 -04:00
|
|
|
) -> Result<UrlPattern, UrlPatternError> {
|
|
|
|
let init =
|
|
|
|
quirks::process_construct_pattern_input(input, base_url.as_deref())
|
|
|
|
.map_err(UrlPatternError)?;
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2024-10-14 17:15:31 -04:00
|
|
|
let pattern =
|
|
|
|
quirks::parse_pattern(init, options).map_err(UrlPatternError)?;
|
2021-09-08 05:14:29 -04:00
|
|
|
|
|
|
|
Ok(pattern)
|
|
|
|
}
|
|
|
|
|
2023-10-05 08:34:38 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2021-09-08 05:14:29 -04:00
|
|
|
pub fn op_urlpattern_process_match_input(
|
2023-10-05 08:34:38 -04:00
|
|
|
#[serde] input: StringOrInit,
|
|
|
|
#[string] base_url: Option<String>,
|
2024-10-14 17:15:31 -04:00
|
|
|
) -> Result<Option<(MatchInput, quirks::Inputs)>, UrlPatternError> {
|
|
|
|
let res = quirks::process_match_input(input, base_url.as_deref())
|
|
|
|
.map_err(UrlPatternError)?;
|
2021-09-08 05:14:29 -04:00
|
|
|
|
|
|
|
let (input, inputs) = match res {
|
|
|
|
Some((input, inputs)) => (input, inputs),
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
|
2024-10-14 17:15:31 -04:00
|
|
|
Ok(quirks::parse_match_input(input).map(|input| (input, inputs)))
|
2021-09-08 05:14:29 -04:00
|
|
|
}
|