Skip to content

Commit

Permalink
CrateScope: Remove crate name normalization (#5575)
Browse files Browse the repository at this point in the history
As discussed in the team meeting, this should instead be solved by having both variants as dedicated scopes, if necessary.
  • Loading branch information
Turbo87 authored Dec 3, 2022
1 parent a36fc3a commit 90eef28
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/models/token/scopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,13 @@ impl CrateScope {
}

pub fn matches(&self, crate_name: &str) -> bool {
let canonicalize = |name: &str| name.replace('-', "_");

if self.pattern == "*" {
return true;
}

return match self.pattern.strip_suffix('*') {
Some(prefix) => {
crate_name.starts_with(prefix)
|| canonicalize(crate_name).starts_with(&canonicalize(prefix))
}
None => {
crate_name == self.pattern
|| canonicalize(crate_name) == canonicalize(&self.pattern)
}
Some(prefix) => crate_name.starts_with(prefix),
None => crate_name == self.pattern,
};
}
}
Expand Down Expand Up @@ -174,12 +166,12 @@ mod tests {
assert!(!scope("foo").matches("foo-bar"));
assert!(!scope("foo").matches("foo_bar"));
assert!(scope("foo-bar").matches("foo-bar"));
assert!(scope("foo-bar").matches("foo_bar"));
assert!(scope("foo_bar").matches("foo-bar"));
assert!(!scope("foo-bar").matches("foo_bar"));
assert!(!scope("foo_bar").matches("foo-bar"));
assert!(scope("foo_bar").matches("foo_bar"));
assert!(scope("foo-*").matches("foo-bar"));
assert!(scope("foo-*").matches("foo_bar"));
assert!(scope("foo_*").matches("foo-bar"));
assert!(!scope("foo-*").matches("foo_bar"));
assert!(!scope("foo_*").matches("foo-bar"));
assert!(scope("foo_*").matches("foo_bar"));
}
}

0 comments on commit 90eef28

Please sign in to comment.