Skip to content

Commit af79743

Browse files
committed
Improve error on wildcard after another comparator
1 parent 86af565 commit af79743

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub(crate) enum ErrorKind {
1010
Overflow(Position),
1111
EmptySegment(Position),
1212
IllegalCharacter(Position),
13-
CommaAfterWildcard(char),
13+
WildcardNotTheOnlyComparator(char),
1414
UnexpectedAfterWildcard,
1515
ExcessiveComparators,
1616
}
@@ -59,7 +59,7 @@ impl Display for Error {
5959
ErrorKind::IllegalCharacter(pos) => {
6060
write!(formatter, "unexpected character in {}", pos)
6161
}
62-
ErrorKind::CommaAfterWildcard(ch) => {
62+
ErrorKind::WildcardNotTheOnlyComparator(ch) => {
6363
write!(
6464
formatter,
6565
"wildcard req ({}) must be the only comparator in the version req",

src/parse.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl FromStr for VersionReq {
9292
comparators: Vec::new(),
9393
});
9494
} else if rest.starts_with(',') {
95-
return Err(Error::new(ErrorKind::CommaAfterWildcard(ch)));
95+
return Err(Error::new(ErrorKind::WildcardNotTheOnlyComparator(ch)));
9696
} else {
9797
return Err(Error::new(ErrorKind::UnexpectedAfterWildcard));
9898
}
@@ -365,7 +365,18 @@ fn comparator(input: &str) -> Result<(Comparator, Position, &str), Error> {
365365
}
366366

367367
fn version_req(input: &str, out: &mut Vec<Comparator>, depth: usize) -> Result<usize, Error> {
368-
let (comparator, pos, text) = comparator(input)?;
368+
let (comparator, pos, text) = match comparator(input) {
369+
Ok(success) => success,
370+
Err(mut error) => {
371+
if let Some((ch, mut rest)) = wildcard(input) {
372+
rest = rest.trim_start_matches(' ');
373+
if rest.is_empty() || rest.starts_with(',') {
374+
error.kind = ErrorKind::WildcardNotTheOnlyComparator(ch);
375+
}
376+
}
377+
return Err(error);
378+
}
379+
};
369380

370381
if text.is_empty() {
371382
out.reserve_exact(depth + 1);

tests/test_version_req.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,12 @@ fn test_wildcard_and_another() {
425425
let err = req_err("0.20.0-any, *");
426426
assert_to_string(
427427
err,
428-
"unexpected character '*' while parsing major version number",
428+
"wildcard req (*) must be the only comparator in the version req",
429+
);
430+
431+
let err = req_err("0.20.0-any, *, 1.0");
432+
assert_to_string(
433+
err,
434+
"wildcard req (*) must be the only comparator in the version req",
429435
);
430436
}

0 commit comments

Comments
 (0)