Skip to content

Commit

Permalink
Use matches rather than contains in requirements.txt parsing (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Jan 9, 2025
1 parent 58a81f7 commit 15ec830
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions crates/uv-requirements-txt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,11 @@ impl RequirementsTxt {
}
}

/// Returns `true` if the character is a newline or a comment character.
const fn is_terminal(c: char) -> bool {
matches!(c, '\n' | '\r' | '#')
}

/// Parse a single entry, that is a requirement, an inclusion or a comment line
///
/// Consumes all preceding trivia (whitespace and comments). If it returns None, we've reached
Expand All @@ -436,15 +441,15 @@ fn parse_entry(

let start = s.cursor();
Ok(Some(if s.eat_if("-r") || s.eat_if("--requirement") {
let requirements_file = parse_value(content, s, |c: char| !['\n', '\r', '#'].contains(&c))?;
let requirements_file = parse_value(content, s, |c: char| !is_terminal(c))?;
let end = s.cursor();
RequirementsTxtStatement::Requirements {
filename: requirements_file.to_string(),
start,
end,
}
} else if s.eat_if("-c") || s.eat_if("--constraint") {
let constraints_file = parse_value(content, s, |c: char| !['\n', '\r', '#'].contains(&c))?;
let constraints_file = parse_value(content, s, |c: char| !is_terminal(c))?;
let end = s.cursor();
RequirementsTxtStatement::Constraint {
filename: constraints_file.to_string(),
Expand Down Expand Up @@ -475,7 +480,7 @@ fn parse_entry(
hashes,
})
} else if s.eat_if("-i") || s.eat_if("--index-url") {
let given = parse_value(content, s, |c: char| !['\n', '\r', '#'].contains(&c))?;
let given = parse_value(content, s, |c: char| !is_terminal(c))?;
let expanded = expand_env_vars(given);
let url = if let Some(path) = std::path::absolute(expanded.as_ref())
.ok()
Expand All @@ -501,7 +506,7 @@ fn parse_entry(
};
RequirementsTxtStatement::IndexUrl(url.with_given(given))
} else if s.eat_if("--extra-index-url") {
let given = parse_value(content, s, |c: char| !['\n', '\r', '#'].contains(&c))?;
let given = parse_value(content, s, |c: char| !is_terminal(c))?;
let expanded = expand_env_vars(given);
let url = if let Some(path) = std::path::absolute(expanded.as_ref())
.ok()
Expand Down Expand Up @@ -529,7 +534,7 @@ fn parse_entry(
} else if s.eat_if("--no-index") {
RequirementsTxtStatement::NoIndex
} else if s.eat_if("--find-links") || s.eat_if("-f") {
let given = parse_value(content, s, |c: char| !['\n', '\r', '#'].contains(&c))?;
let given = parse_value(content, s, |c: char| !is_terminal(c))?;
let expanded = expand_env_vars(given);
let url = if let Some(path) = std::path::absolute(expanded.as_ref())
.ok()
Expand All @@ -555,7 +560,7 @@ fn parse_entry(
};
RequirementsTxtStatement::FindLinks(url.with_given(given))
} else if s.eat_if("--no-binary") {
let given = parse_value(content, s, |c: char| !['\n', '\r', '#'].contains(&c))?;
let given = parse_value(content, s, |c: char| !is_terminal(c))?;
let specifier = PackageNameSpecifier::from_str(given).map_err(|err| {
RequirementsTxtParserError::NoBinary {
source: err,
Expand All @@ -566,7 +571,7 @@ fn parse_entry(
})?;
RequirementsTxtStatement::NoBinary(NoBinary::from_pip_arg(specifier))
} else if s.eat_if("--only-binary") {
let given = parse_value(content, s, |c: char| !['\n', '\r', '#'].contains(&c))?;
let given = parse_value(content, s, |c: char| !is_terminal(c))?;
let specifier = PackageNameSpecifier::from_str(given).map_err(|err| {
RequirementsTxtParserError::NoBinary {
source: err,
Expand Down

0 comments on commit 15ec830

Please sign in to comment.