Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make URLPatternComponentResult::groups values optional #35

Merged
merged 6 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ repository = "https://github.com/denoland/rust-urlpattern"
license = "MIT"

[dependencies]
url = "2.2.2"
regex = "1.4.3"
url = "2.4.1"
regex = "1.10.5"
serde = { version = "1.0.127", features = ["derive"] }
unic-ucd-ident = { version = "0.9.0", features = ["id"] }

Expand Down
4 changes: 2 additions & 2 deletions src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ impl<R: RegExp> Component<R> {
pub(crate) fn create_match_result(
&self,
input: String,
exec_result: Vec<&str>,
exec_result: Vec<Option<&str>>,
) -> crate::UrlPatternComponentResult {
let groups = self
.group_name_list
.clone()
.into_iter()
.zip(exec_result.into_iter().map(str::to_owned))
.zip(exec_result.into_iter().map(|s| s.map(str::to_owned)))
.collect();
crate::UrlPatternComponentResult { input, groups }
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ fn is_absolute_pathname(
/// // Match the pattern against a URL.
/// let url = "https://example.com/users/123".parse().unwrap();
/// let result = pattern.exec(UrlPatternMatchInput::Url(url)).unwrap().unwrap();
/// assert_eq!(result.pathname.groups.get("id").unwrap(), "123");
/// assert_eq!(result.pathname.groups.get("id").unwrap().as_ref().unwrap(), "123");
///# }
/// ```
#[derive(Debug)]
Expand Down Expand Up @@ -496,7 +496,7 @@ pub struct UrlPatternComponentResult {
/// The matched input for this component.
pub input: String,
/// The values for all named groups in the pattern.
pub groups: std::collections::HashMap<String, String>,
pub groups: std::collections::HashMap<String, Option<String>>,
}

#[cfg(test)]
Expand Down Expand Up @@ -525,7 +525,7 @@ mod tests {
#[derive(Debug, Deserialize)]
struct ComponentResult {
input: String,
groups: HashMap<String, String>,
groups: HashMap<String, Option<String>>,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -787,7 +787,7 @@ mod tests {
if !exactly_empty_components
.contains(&stringify!($component).to_owned())
{
groups.insert("0".to_owned(), "".to_owned());
groups.insert("0".to_owned(), Some("".to_owned()));
}
UrlPatternComponentResult {
input: "".to_owned(),
Expand Down
7 changes: 5 additions & 2 deletions src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ impl<R: RegExp> Matcher<R> {
}
}

pub fn matches<'a>(&self, mut input: &'a str) -> Option<Vec<&'a str>> {
pub fn matches<'a>(
&self,
mut input: &'a str,
) -> Option<Vec<Option<&'a str>>> {
let prefix_len = self.prefix.len();
let suffix_len = self.suffix.len();
let input_len = input.len();
Expand Down Expand Up @@ -82,7 +85,7 @@ impl<R: RegExp> Matcher<R> {
return None;
}
}
Some(vec![input])
Some(vec![Some(input)])
}
InnerMatcher::RegExp { regexp, .. } => {
regexp.as_ref().unwrap().matches(input)
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ where
if name_token.is_some() || regexp_or_wildcard_token.is_some() {
let mut prefix = String::new();
if let Some(char_token) = char_token {
prefix = char_token.value.to_owned();
char_token.value.clone_into(&mut prefix);
}
if !prefix.is_empty() && prefix != options.prefix_code_point {
parser.pending_fixed_value.push_str(&prefix);
Expand Down
2 changes: 1 addition & 1 deletion src/quirks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl RegExp for EcmaRegexp {
Ok(EcmaRegexp(pattern.to_string()))
}

fn matches<'a>(&self, text: &'a str) -> Option<Vec<&'a str>> {
fn matches<'a>(&self, text: &'a str) -> Option<Vec<Option<&'a str>>> {
let regexp = regex::Regex::parse(&self.0).ok()?;
regexp.matches(text)
}
Expand Down
8 changes: 4 additions & 4 deletions src/regexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ pub trait RegExp: Sized {
/// of captures. The matches are returned in the order they appear in the
/// regular expression. It is **not** prefixed with the full match. For groups
/// that occur in the regular expression, but did not match, the corresponding
/// capture should be the empty string ("").
/// capture should be `None`.
///
/// Returns `None` if the text does not match the regular expression.
fn matches<'a>(&self, text: &'a str) -> Option<Vec<&'a str>>;
fn matches<'a>(&self, text: &'a str) -> Option<Vec<Option<&'a str>>>;
}

impl RegExp for regex::Regex {
Expand All @@ -26,13 +26,13 @@ impl RegExp for regex::Regex {
regex::Regex::new(pattern).map_err(|_| ())
}

fn matches<'a>(&self, text: &'a str) -> Option<Vec<&'a str>> {
fn matches<'a>(&self, text: &'a str) -> Option<Vec<Option<&'a str>>> {
let captures = self.captures(text)?;

let captures = captures
.iter()
.skip(1)
.map(|c| c.map(|m| m.as_str()).unwrap_or(""))
.map(|c| c.map(|m| m.as_str()))
.collect();
crowlKats marked this conversation as resolved.
Show resolved Hide resolved

Some(captures)
Expand Down
32 changes: 21 additions & 11 deletions src/testdata/urlpatterntestdata.json
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,9 @@
{
"pattern": [{ "pathname": "/foo/:bar?" }],
"inputs": [{ "pathname": "/foo" }],
"//": "The `null` below is translated to undefined in the test harness.",
"expected_match": {
"pathname": { "input": "/foo", "groups": { "bar": "" } }
"pathname": { "input": "/foo", "groups": { "bar": null } }
}
},
{
Expand Down Expand Up @@ -419,8 +420,9 @@
{
"pattern": [{ "pathname": "/foo/:bar*" }],
"inputs": [{ "pathname": "/foo" }],
"//": "The `null` below is translated to undefined in the test harness.",
"expected_match": {
"pathname": { "input": "/foo", "groups": { "bar": "" } }
"pathname": { "input": "/foo", "groups": { "bar": null } }
}
},
{
Expand Down Expand Up @@ -473,15 +475,17 @@
"expected_obj": {
"pathname": "/foo/*?"
},
"//": "The `null` below is translated to undefined in the test harness.",
"expected_match": {
"pathname": { "input": "/foo", "groups": { "0": "" } }
"pathname": { "input": "/foo", "groups": { "0": null } }
}
},
{
"pattern": [{ "pathname": "/foo/*?" }],
"inputs": [{ "pathname": "/foo" }],
"//": "The `null` below is translated to undefined in the test harness.",
"expected_match": {
"pathname": { "input": "/foo", "groups": { "0": "" } }
"pathname": { "input": "/foo", "groups": { "0": null } }
}
},
{
Expand Down Expand Up @@ -657,15 +661,17 @@
"expected_obj": {
"pathname": "/foo/**"
},
"//": "The `null` below is translated to undefined in the test harness.",
"expected_match": {
"pathname": { "input": "/foo", "groups": { "0": "" } }
"pathname": { "input": "/foo", "groups": { "0": null } }
}
},
{
"pattern": [{ "pathname": "/foo/**" }],
"inputs": [{ "pathname": "/foo" }],
"//": "The `null` below is translated to undefined in the test harness.",
"expected_match": {
"pathname": { "input": "/foo", "groups": { "0": "" } }
"pathname": { "input": "/foo", "groups": { "0": null } }
}
},
{
Expand Down Expand Up @@ -1812,9 +1818,10 @@
"hostname": "(sub.)?example.com",
"pathname": "/foo"
},
"//": "The `null` below is translated to undefined in the test harness.",
"expected_match": {
"protocol": { "input": "https", "groups": {} },
"hostname": { "input": "example.com", "groups": { "0": "" } },
"hostname": { "input": "example.com", "groups": { "0": null } },
"pathname": { "input": "/foo", "groups": {} }
}
},
Expand Down Expand Up @@ -1850,9 +1857,10 @@
"hostname": "(sub(?:.))?example.com",
"pathname": "/foo"
},
"//": "The `null` below is translated to undefined in the test harness.",
"expected_match": {
"protocol": { "input": "https", "groups": {} },
"hostname": { "input": "example.com", "groups": { "0": "" } },
"hostname": { "input": "example.com", "groups": { "0": null } },
"pathname": { "input": "/foo", "groups": {} }
}
},
Expand Down Expand Up @@ -2299,9 +2307,10 @@
"protocol": "data",
"pathname": "text/javascript,let x = 100/:tens?5;"
},
"//": "The `null` below is translated to undefined in the test harness.",
"expected_match": {
"protocol": { "input": "data", "groups": {} },
"pathname": { "input": "text/javascript,let x = 100/5;", "groups": { "tens": "" } }
"pathname": { "input": "text/javascript,let x = 100/5;", "groups": { "tens": null } }
}
},
{
Expand Down Expand Up @@ -2416,7 +2425,6 @@
"expected_obj": "error"
},
{
"skip": "bug in rust-url: https://github.com/servo/rust-url/pull/718",
"pattern": [{ "hostname": "bad|hostname" }],
"expected_obj": "error"
},
Expand Down Expand Up @@ -2618,13 +2626,15 @@
}
},
{
"skip": "only works in ecmascript variety of regex",
"pattern": [{ "pathname": "*{}**?" }],
"inputs": [{ "pathname": "foobar" }],
"expected_obj": {
"pathname": "*(.*)?"
},
"//": "The `null` below is translated to undefined in the test harness.",
"expected_match": {
"pathname": { "input": "foobar", "groups": { "0": "foobar", "1": "" }}
"pathname": { "input": "foobar", "groups": { "0": "foobar", "1": null }}
}
},
{
Expand Down
Loading