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

Use matches rather than contains in requirements.txt parsing #10423

Merged
merged 1 commit into from
Jan 9, 2025
Merged
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
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw terminal is overloaded for grammars: https://en.wikipedia.org/wiki/Terminal_and_nonterminal_symbols

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
Loading