Skip to content

Commit

Permalink
fix(range): handle partial = ranges, which was causing panics (#7)
Browse files Browse the repository at this point in the history
Fixes: #6
Co-authored-by: Kat Marchán <kzm@zkat.tech>
  • Loading branch information
danielhuang and zkat authored Sep 4, 2022
1 parent ee8376e commit f0eef04
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ mod tests {
fn individual_version_component_has_an_upper_bound() {
let out_of_range = MAX_SAFE_INTEGER + 1;
let v = Version::parse(format!("1.2.{}", out_of_range));
assert_eq!(v.err().expect("Parse should have failed.").to_string(), "Integer component of semver string is larger than JavaScript's Number.MAX_SAFE_INTEGER: 900719925474100");
assert_eq!(v.expect_err("Parse should have failed.").to_string(), "Integer component of semver string is larger than JavaScript's Number.MAX_SAFE_INTEGER: 900719925474100");
}

#[test]
Expand All @@ -813,7 +813,7 @@ mod tests {
let v = Version::parse(version_string.clone());

assert_eq!(
v.err().expect("Parse should have failed").to_string(),
v.expect_err("Parse should have failed").to_string(),
"Semver string can't be longer than 256 characters."
);

Expand Down
60 changes: 53 additions & 7 deletions src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,13 +685,57 @@ fn primitive(input: &str) -> IResult<&str, Option<BoundSet>, SemverParseError<&s
(
Exact,
Partial {
major,
major: Some(major),
minor: Some(minor),
patch: Some(patch),
pre_release,
..
},
) => BoundSet::exact( Version {
major,
minor,
patch,
pre_release,
build: vec![],
}),
(
Exact,
Partial {
major: Some(major),
minor: Some(minor),
..
},
) => BoundSet::new(
Bound::Lower(Predicate::Including(
(major, minor, 0).into(),
)),
Bound::Upper(Predicate::Excluding(Version {
major,
minor: minor + 1,
patch: 0,
pre_release: vec![Identifier::Numeric(0)],
build: vec![],
})),
),
(
Exact,
Partial {
major: Some(major),
..
},
) => BoundSet::exact((major.unwrap_or(0), minor, patch).into()),
_ => unreachable!("Odd parsed version: {:?}", parsed),
) => BoundSet::new(
Bound::Lower(Predicate::Including(
(major, 0, 0).into(),
)),
Bound::Upper(Predicate::Excluding(Version {
major: major + 1,
minor: 0,
patch: 0,
pre_release: vec![Identifier::Numeric(0)],
build: vec![],
})),
),
_ => unreachable!("Failed to parse operation. This should not happen and should be reported as a bug, while parsing {}", input),
},
),
)(input)
Expand Down Expand Up @@ -833,15 +877,15 @@ fn tilde(input: &str) -> IResult<&str, Option<BoundSet>, SemverParseError<&str>>
Partial {
major: Some(major),
minor: Some(minor),
patch: Some(patch),
patch,
pre_release,
..
},
) => BoundSet::new(
Bound::Lower(Predicate::Including(Version {
major,
minor,
patch,
patch: patch.unwrap_or(0),
pre_release,
build: vec![],
})),
Expand Down Expand Up @@ -890,7 +934,7 @@ fn tilde(input: &str) -> IResult<&str, Option<BoundSet>, SemverParseError<&str>>
Bound::Lower(Predicate::Including((major, 0, 0).into())),
Bound::Upper(Predicate::Excluding((major + 1, 0, 0, 0).into())),
),
_ => unreachable!("Should not have gotten here"),
_ => unreachable!("This should not have parsed: {}", input)
}),
)(input)
}
Expand Down Expand Up @@ -955,7 +999,7 @@ fn caret(input: &str) -> IResult<&str, Option<BoundSet>, SemverParseError<&str>>
(n, _, _) => Version::from((n + 1, 0, 0, 0)),
})),
),
_ => unreachable!(),
_ => None,
},
),
)(input)
Expand Down Expand Up @@ -1617,6 +1661,8 @@ mod tests {
loose1 => [">01.02.03", ">1.2.3"],
loose2 => ["~1.2.3beta", ">=1.2.3-beta <1.3.0-0"],
caret_weird => ["^ 1.2 ^ 1", ">=1.2.0 <2.0.0-0"],
loose_eq1 => ["=0.7", ">=0.7.0 <0.8.0-0"],
loose_eq2 => ["=1", ">=1.0.0 <2.0.0-0"],
];

/*
Expand Down

0 comments on commit f0eef04

Please sign in to comment.