From 00c041694c919b87f063992238b2bebbd17754e7 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 3 Jun 2021 20:31:06 -0700 Subject: [PATCH 1/3] Parse x and X as wildcard --- src/parse.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/parse.rs b/src/parse.rs index ba5cdc63..017a5b53 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -82,7 +82,7 @@ impl FromStr for VersionReq { fn from_str(text: &str) -> Result { 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); @@ -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) @@ -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; @@ -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; } From b39b59b9d8877a7d4cdbb50cb37001091ec2562c Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 3 Jun 2021 20:34:56 -0700 Subject: [PATCH 2/3] Update wildcard test with xs --- tests/test_version_req.rs | 42 +++++++++------------------------------ 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/tests/test_version_req.rs b/tests/test_version_req.rs index 2b7064d1..c0e04014 100644 --- a/tests/test_version_req.rs +++ b/tests/test_version_req.rs @@ -266,49 +266,25 @@ pub fn test_wildcard() { assert_match_all(r, &["0.9.1", "2.9.0", "0.0.9", "1.0.1", "1.1.1"]); assert_match_none(r, &[]); - 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", - ); - - 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", - ); - - 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] From 4358bd05a2b0c7ebe1db0d45124439bb4cdcacae Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 3 Jun 2021 20:38:41 -0700 Subject: [PATCH 3/3] Add pre-release versions to wildcard test --- tests/test_version_req.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_version_req.rs b/tests/test_version_req.rs index c0e04014..982fcd6b 100644 --- a/tests/test_version_req.rs +++ b/tests/test_version_req.rs @@ -264,7 +264,7 @@ 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"]); for s in &["x", "X"] { assert_eq!(*r, req(s)); @@ -272,7 +272,7 @@ pub fn test_wildcard() { 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"]); + assert_match_none(r, &["0.0.9", "1.2.0-pre"]); for s in &["1.x", "1.X", "1.*.*"] { assert_eq!(*r, req(s)); @@ -280,7 +280,7 @@ pub fn test_wildcard() { 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"]); + assert_match_none(r, &["1.9.0", "1.0.9", "2.0.1", "0.1.3", "1.2.2-pre"]); for s in &["1.2.x", "1.2.X"] { assert_eq!(*r, req(s));