Skip to content

Commit

Permalink
Improve error on wildcard after another comparator
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Feb 4, 2022
1 parent 86af565 commit af79743
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(crate) enum ErrorKind {
Overflow(Position),
EmptySegment(Position),
IllegalCharacter(Position),
CommaAfterWildcard(char),
WildcardNotTheOnlyComparator(char),
UnexpectedAfterWildcard,
ExcessiveComparators,
}
Expand Down Expand Up @@ -59,7 +59,7 @@ impl Display for Error {
ErrorKind::IllegalCharacter(pos) => {
write!(formatter, "unexpected character in {}", pos)
}
ErrorKind::CommaAfterWildcard(ch) => {
ErrorKind::WildcardNotTheOnlyComparator(ch) => {
write!(
formatter,
"wildcard req ({}) must be the only comparator in the version req",
Expand Down
15 changes: 13 additions & 2 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl FromStr for VersionReq {
comparators: Vec::new(),
});
} else if rest.starts_with(',') {
return Err(Error::new(ErrorKind::CommaAfterWildcard(ch)));
return Err(Error::new(ErrorKind::WildcardNotTheOnlyComparator(ch)));
} else {
return Err(Error::new(ErrorKind::UnexpectedAfterWildcard));
}
Expand Down Expand Up @@ -365,7 +365,18 @@ fn comparator(input: &str) -> Result<(Comparator, Position, &str), Error> {
}

fn version_req(input: &str, out: &mut Vec<Comparator>, depth: usize) -> Result<usize, Error> {
let (comparator, pos, text) = comparator(input)?;
let (comparator, pos, text) = match comparator(input) {
Ok(success) => success,
Err(mut error) => {
if let Some((ch, mut rest)) = wildcard(input) {
rest = rest.trim_start_matches(' ');
if rest.is_empty() || rest.starts_with(',') {
error.kind = ErrorKind::WildcardNotTheOnlyComparator(ch);
}
}
return Err(error);
}
};

if text.is_empty() {
out.reserve_exact(depth + 1);
Expand Down
8 changes: 7 additions & 1 deletion tests/test_version_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,12 @@ fn test_wildcard_and_another() {
let err = req_err("0.20.0-any, *");
assert_to_string(
err,
"unexpected character '*' while parsing major version number",
"wildcard req (*) must be the only comparator in the version req",
);

let err = req_err("0.20.0-any, *, 1.0");
assert_to_string(
err,
"wildcard req (*) must be the only comparator in the version req",
);
}

0 comments on commit af79743

Please sign in to comment.