Skip to content

Commit

Permalink
chore: make clippy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed May 27, 2024
1 parent e610d55 commit 772a786
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ jobs:

- name: Run Clippy
run: |
cargo clippy --all-targets --no-default-features --features=${{ matrix.feature }} -- -D warnings
# we need to exclude --bench, as it only works on nightly
cargo clippy --lib --bins --tests --no-default-features --features=${{ matrix.feature }} -- -D warnings
all-tests:
runs-on: ubuntu-22.04
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ default = []
serde = { version = "1.0.0", features = ["derive"] }
serde_json = "1.0.13"
lazy_static = "^1.0.0"
url = "^1.7.0"
url = "2"
24 changes: 12 additions & 12 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::utils::PercentCodec;
use super::utils::QuickFind;
use super::validation;

pub fn parse_scheme<'a>(input: &str) -> Result<(&str, String)> {
pub fn parse_scheme(input: &str) -> Result<(&str, String)> {
if let Some(i) = input.quickfind(b':') {
if &input[..i] == "pkg" {
let mut j = i + 1;
Expand All @@ -23,7 +23,7 @@ pub fn parse_scheme<'a>(input: &str) -> Result<(&str, String)> {
}
}

pub fn parse_subpath<'a>(input: &str) -> Result<(&str, Option<String>)> {
pub fn parse_subpath(input: &str) -> Result<(&str, Option<String>)> {
if let Some(i) = input.quickrfind(b'#') {
let mut subpath = String::with_capacity(i + 1);
let mut components = input[i + 1..]
Expand All @@ -38,7 +38,7 @@ pub fn parse_subpath<'a>(input: &str) -> Result<(&str, Option<String>)> {
return Err(Error::InvalidSubpathSegment(decoded.to_string()));
}
}
while let Some(c) = components.next() {
for c in components {
let decoded = c.decode().decode_utf8()?;
if validation::is_subpath_segment_valid(&decoded) {
subpath.push('/');
Expand All @@ -53,13 +53,13 @@ pub fn parse_subpath<'a>(input: &str) -> Result<(&str, Option<String>)> {
}
}

pub fn parse_qualifiers<'a>(input: &str) -> Result<(&str, Vec<(String, String)>)> {
pub fn parse_qualifiers(input: &str) -> Result<(&str, Vec<(String, String)>)> {
if let Some(i) = input.quickrfind(b'?') {
let mut qualifiers = Vec::new();
let pairs = input[i + 1..]
.split('&')
.map(|ref pair| utils::cut(pair, b'='))
.filter(|ref pair| !pair.1.is_empty());
.map(|pair| utils::cut(pair, b'='))
.filter(|pair| !pair.1.is_empty());
for (key, value) in pairs {
if validation::is_qualifier_key_valid(key) {
qualifiers.push((
Expand All @@ -76,7 +76,7 @@ pub fn parse_qualifiers<'a>(input: &str) -> Result<(&str, Vec<(String, String)>)
}
}

pub fn parse_version<'a>(input: &str) -> Result<(&str, Option<String>)> {
pub fn parse_version(input: &str) -> Result<(&str, Option<String>)> {
if let Some(i) = input.quickrfind(b'@') {
Ok((
&input[..i],
Expand All @@ -87,17 +87,17 @@ pub fn parse_version<'a>(input: &str) -> Result<(&str, Option<String>)> {
}
}

pub fn parse_type<'a>(input: &str) -> Result<(&str, String)> {
pub fn parse_type(input: &str) -> Result<(&str, String)> {
match input.quickfind(b'/') {
Some(i) if validation::is_type_valid(&input[..i]) => {
Ok((&input[i + 1..], input[..i].to_lowercase().into()))
Ok((&input[i + 1..], input[..i].to_lowercase()))
}
Some(i) => Err(Error::InvalidType(input[..i].to_string())),
None => Err(Error::MissingType),
}
}

pub fn parse_name<'a>(input: &str) -> Result<(&str, String)> {
pub fn parse_name(input: &str) -> Result<(&str, String)> {
let (rem, name) = utils::rcut(input.trim_matches('/'), b'/');
if name.is_empty() {
Err(Error::MissingName)
Expand All @@ -107,7 +107,7 @@ pub fn parse_name<'a>(input: &str) -> Result<(&str, String)> {
}
}

pub fn parse_namespace<'a>(input: &str) -> Result<(&str, Option<String>)> {
pub fn parse_namespace(input: &str) -> Result<(&str, Option<String>)> {
if !input.is_empty() {
let mut namespace = String::with_capacity(input.len());
let mut components = input
Expand All @@ -122,7 +122,7 @@ pub fn parse_namespace<'a>(input: &str) -> Result<(&str, Option<String>)> {
return Err(Error::InvalidNamespaceComponent(decoded.to_string()));
}
}
while let Some(c) = components.next() {
for c in components {
let decoded = c.decode().decode_utf8()?;
if validation::is_namespace_component_valid(&decoded) {
namespace.push('/');
Expand Down
14 changes: 7 additions & 7 deletions src/purl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl<'a> PackageUrl<'a> {
n = Cow::Owned(n.to_lowercase());
}
if n.chars().any(|c| c == '_') {
n = Cow::Owned(n.replace("_", "-"));
n = Cow::Owned(n.replace('_', "-"));

Check warning on line 101 in src/purl.rs

View check run for this annotation

Codecov / codecov/patch

src/purl.rs#L101

Added line #L101 was not covered by tests
}
}
_ => {}
Expand Down Expand Up @@ -237,11 +237,11 @@ impl FromStr for PackageUrl<'static> {
// Special rules for some types
match ty.as_ref() {
"bitbucket" | "github" => {
name = name.to_lowercase().into();
namespace = namespace.map(|ns| ns.to_lowercase().into());
name = name.to_lowercase();
namespace = namespace.map(|ns| ns.to_lowercase());
}
"pypi" => {
name = name.replace('_', "-").to_lowercase().into();
name = name.replace('_', "-").to_lowercase();
}
_ => {}
};
Expand Down Expand Up @@ -301,7 +301,7 @@ impl Display for PackageUrl<'_> {
.and(f.write_str("="))
.and(v.encode(ENCODE_SET).fmt(f))?;
}
while let Some((k, v)) = iter.next() {
for (k, v) in iter {
f.write_str("&")
.and(k.fmt(f))
.and(f.write_str("="))
Expand All @@ -314,11 +314,11 @@ impl Display for PackageUrl<'_> {
f.write_str("#")?;
let mut components = sp
.split('/')
.filter(|&s| !(s == "" || s == "." || s == ".."));
.filter(|&s| !(s.is_empty() || s == "." || s == ".."));
if let Some(component) = components.next() {
component.encode(ENCODE_SET).fmt(f)?;
}
while let Some(component) = components.next() {
for component in components {
f.write_str("/")?;
component.encode(ENCODE_SET).fmt(f)?;
}
Expand Down
6 changes: 4 additions & 2 deletions src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ pub fn is_type_valid(ty: &str) -> bool {
Some(c) => c,
None => return false,
};
if first.is_digit(10) {
if first.is_ascii_digit() {
return false;
}

#[allow(clippy::match_like_matches_macro)]
ty.chars().all(|c| match c {
'.' | '-' | '+' | 'a'..='z' | 'A'..='Z' | '0'..='9' => true,
_ => false,
Expand All @@ -21,12 +22,13 @@ pub fn is_qualifier_key_valid(key: &str) -> bool {
Some(c) => c,
None => return false,
};
if first.is_digit(10) {
if first.is_ascii_digit() {
return false;
}

// check the key contains only valid characters
// The key must be composed only of ASCII letters and numbers, '.', '-' and '_' (period, dash and underscore)
#[allow(clippy::match_like_matches_macro)]
key.chars().all(|c| match c {
'.' | '-' | '_' | 'a'..='z' | 'A'..='Z' | '0'..='9' => true,
_ => false,
Expand Down

0 comments on commit 772a786

Please sign in to comment.