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

Parse x and X as wildcard #247

Merged
merged 3 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
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
18 changes: 15 additions & 3 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl FromStr for VersionReq {

fn from_str(text: &str) -> Result<Self, Self::Err> {
let text = text.trim_start_matches(' ');
if let Some(text) = text.strip_prefix('*') {
if let Some(text) = wildcard(text) {
if text.trim_start_matches(' ').is_empty() {
#[cfg(not(no_const_vec_new))]
return Ok(VersionReq::STAR);
Expand Down Expand Up @@ -181,6 +181,18 @@ fn numeric_identifier(input: &str, pos: Position) -> Result<(u64, &str), Error>
}
}

fn wildcard(input: &str) -> Option<&str> {
if let Some(rest) = input.strip_prefix('*') {
Some(rest)
} else if let Some(rest) = input.strip_prefix('x') {
Some(rest)
} else if let Some(rest) = input.strip_prefix('X') {
Some(rest)
} else {
None
}
}

fn dot(input: &str, pos: Position) -> Result<&str, Error> {
if let Some(rest) = input.strip_prefix('.') {
Ok(rest)
Expand Down Expand Up @@ -281,7 +293,7 @@ fn comparator(input: &str) -> Result<(Comparator, Position, &str), Error> {

let (minor, text) = if let Some(text) = text.strip_prefix('.') {
pos = Position::Minor;
if let Some(text) = text.strip_prefix('*') {
if let Some(text) = wildcard(text) {
has_wildcard = true;
if default_op {
op = Op::Wildcard;
Expand All @@ -297,7 +309,7 @@ fn comparator(input: &str) -> Result<(Comparator, Position, &str), Error> {

let (patch, text) = if let Some(text) = text.strip_prefix('.') {
pos = Position::Patch;
if let Some(text) = text.strip_prefix('*') {
if let Some(text) = wildcard(text) {
if default_op {
op = Op::Wildcard;
}
Expand Down
48 changes: 12 additions & 36 deletions tests/test_version_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,51 +264,27 @@ pub fn test_wildcard() {

let ref r = req("*");
assert_match_all(r, &["0.9.1", "2.9.0", "0.0.9", "1.0.1", "1.1.1"]);
assert_match_none(r, &[]);
assert_match_none(r, &["1.0.0-pre"]);

let err = req_err("x");
assert_to_string(
err,
"unexpected character 'x' while parsing major version number",
);

let err = req_err("X");
assert_to_string(
err,
"unexpected character 'X' while parsing major version number",
);
for s in &["x", "X"] {
assert_eq!(*r, req(s));
}

let ref r = req("1.*");
assert_match_all(r, &["1.2.0", "1.2.1", "1.1.1", "1.3.0"]);
assert_match_none(r, &["0.0.9"]);

let err = req_err("1.x");
assert_to_string(
err,
"unexpected character 'x' while parsing minor version number",
);
assert_match_none(r, &["0.0.9", "1.2.0-pre"]);

let err = req_err("1.X");
assert_to_string(
err,
"unexpected character 'X' while parsing minor version number",
);
for s in &["1.x", "1.X", "1.*.*"] {
assert_eq!(*r, req(s));
}

let ref r = req("1.2.*");
assert_match_all(r, &["1.2.0", "1.2.2", "1.2.4"]);
assert_match_none(r, &["1.9.0", "1.0.9", "2.0.1", "0.1.3"]);

let err = req_err("1.2.x");
assert_to_string(
err,
"unexpected character 'x' while parsing patch version number",
);
assert_match_none(r, &["1.9.0", "1.0.9", "2.0.1", "0.1.3", "1.2.2-pre"]);

let err = req_err("1.2.X");
assert_to_string(
err,
"unexpected character 'X' while parsing patch version number",
);
for s in &["1.2.x", "1.2.X"] {
assert_eq!(*r, req(s));
}
}

#[test]
Expand Down