diff --git a/src/npm.rs b/src/npm.rs index c97fc2d..a2e4fe9 100644 --- a/src/npm.rs +++ b/src/npm.rs @@ -217,7 +217,7 @@ fn range(input: &str) -> ParseResult { start: hyphen.start.as_lower_bound(), end: hyphen.end.as_upper_bound(), }), - map(separated_list(simple, whitespace), |ranges| { + map(separated_list(simple, range_separator), |ranges| { let mut final_range = VersionRange::all(); for range in ranges { final_range = final_range.clamp(&range); @@ -227,6 +227,14 @@ fn range(input: &str) -> ParseResult { )(input) } +fn range_separator(input: &str) -> ParseResult<()> { + fn comma(input: &str) -> ParseResult<()> { + map(delimited(skip_whitespace, ch(','), skip_whitespace), |_| ())(input) + } + + or3(map(logical_and, |_| ()), comma, map(whitespace, |_| ()))(input) +} + #[derive(Debug, Clone)] struct Hyphen { start: Partial, @@ -254,6 +262,11 @@ fn logical_or(input: &str) -> ParseResult<&str> { delimited(skip_whitespace, tag("||"), skip_whitespace)(input) } +// logical-and ::= ( ' ' ) * '&&' ( ' ' ) * +fn logical_and(input: &str) -> ParseResult<&str> { + delimited(skip_whitespace, tag("&&"), skip_whitespace)(input) +} + fn skip_whitespace_or_v(input: &str) -> ParseResult<()> { map( pair(skip_whitespace, pair(maybe(ch('v')), skip_whitespace)), @@ -635,6 +648,28 @@ mod tests { assert!(!tester.matches("6.1.0")); } + #[test] + pub fn npm_version_req_and_range() { + let tester = NpmVersionReqTester::new(">= 1.2 && <= 2.0.0"); + assert!(!tester.matches("1.1.9")); + assert!(tester.matches("1.2.2")); + assert!(tester.matches("1.2.0")); + assert!(tester.matches("1.9.9")); + assert!(!tester.matches("2.1.1")); + assert!(!tester.matches("2.0.1")); + } + + #[test] + pub fn npm_version_req_comma_range() { + let tester = NpmVersionReqTester::new(">= 1.2, <= 2.0.0"); + assert!(!tester.matches("1.1.9")); + assert!(tester.matches("1.2.2")); + assert!(tester.matches("1.2.0")); + assert!(tester.matches("1.9.9")); + assert!(!tester.matches("2.1.1")); + assert!(!tester.matches("2.0.1")); + } + #[test] pub fn npm_version_req_with_tag() { let req = parse_npm_version_req("latest").unwrap();