Skip to content

Commit f0eef04

Browse files
danielhuangzkat
andauthored
fix(range): handle partial = ranges, which was causing panics (#7)
Fixes: #6 Co-authored-by: Kat Marchán <kzm@zkat.tech>
1 parent ee8376e commit f0eef04

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ mod tests {
803803
fn individual_version_component_has_an_upper_bound() {
804804
let out_of_range = MAX_SAFE_INTEGER + 1;
805805
let v = Version::parse(format!("1.2.{}", out_of_range));
806-
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");
806+
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");
807807
}
808808

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

815815
assert_eq!(
816-
v.err().expect("Parse should have failed").to_string(),
816+
v.expect_err("Parse should have failed").to_string(),
817817
"Semver string can't be longer than 256 characters."
818818
);
819819

src/range.rs

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -685,13 +685,57 @@ fn primitive(input: &str) -> IResult<&str, Option<BoundSet>, SemverParseError<&s
685685
(
686686
Exact,
687687
Partial {
688-
major,
688+
major: Some(major),
689689
minor: Some(minor),
690690
patch: Some(patch),
691+
pre_release,
692+
..
693+
},
694+
) => BoundSet::exact( Version {
695+
major,
696+
minor,
697+
patch,
698+
pre_release,
699+
build: vec![],
700+
}),
701+
(
702+
Exact,
703+
Partial {
704+
major: Some(major),
705+
minor: Some(minor),
706+
..
707+
},
708+
) => BoundSet::new(
709+
Bound::Lower(Predicate::Including(
710+
(major, minor, 0).into(),
711+
)),
712+
Bound::Upper(Predicate::Excluding(Version {
713+
major,
714+
minor: minor + 1,
715+
patch: 0,
716+
pre_release: vec![Identifier::Numeric(0)],
717+
build: vec![],
718+
})),
719+
),
720+
(
721+
Exact,
722+
Partial {
723+
major: Some(major),
691724
..
692725
},
693-
) => BoundSet::exact((major.unwrap_or(0), minor, patch).into()),
694-
_ => unreachable!("Odd parsed version: {:?}", parsed),
726+
) => BoundSet::new(
727+
Bound::Lower(Predicate::Including(
728+
(major, 0, 0).into(),
729+
)),
730+
Bound::Upper(Predicate::Excluding(Version {
731+
major: major + 1,
732+
minor: 0,
733+
patch: 0,
734+
pre_release: vec![Identifier::Numeric(0)],
735+
build: vec![],
736+
})),
737+
),
738+
_ => unreachable!("Failed to parse operation. This should not happen and should be reported as a bug, while parsing {}", input),
695739
},
696740
),
697741
)(input)
@@ -833,15 +877,15 @@ fn tilde(input: &str) -> IResult<&str, Option<BoundSet>, SemverParseError<&str>>
833877
Partial {
834878
major: Some(major),
835879
minor: Some(minor),
836-
patch: Some(patch),
880+
patch,
837881
pre_release,
838882
..
839883
},
840884
) => BoundSet::new(
841885
Bound::Lower(Predicate::Including(Version {
842886
major,
843887
minor,
844-
patch,
888+
patch: patch.unwrap_or(0),
845889
pre_release,
846890
build: vec![],
847891
})),
@@ -890,7 +934,7 @@ fn tilde(input: &str) -> IResult<&str, Option<BoundSet>, SemverParseError<&str>>
890934
Bound::Lower(Predicate::Including((major, 0, 0).into())),
891935
Bound::Upper(Predicate::Excluding((major + 1, 0, 0, 0).into())),
892936
),
893-
_ => unreachable!("Should not have gotten here"),
937+
_ => unreachable!("This should not have parsed: {}", input)
894938
}),
895939
)(input)
896940
}
@@ -955,7 +999,7 @@ fn caret(input: &str) -> IResult<&str, Option<BoundSet>, SemverParseError<&str>>
955999
(n, _, _) => Version::from((n + 1, 0, 0, 0)),
9561000
})),
9571001
),
958-
_ => unreachable!(),
1002+
_ => None,
9591003
},
9601004
),
9611005
)(input)
@@ -1617,6 +1661,8 @@ mod tests {
16171661
loose1 => [">01.02.03", ">1.2.3"],
16181662
loose2 => ["~1.2.3beta", ">=1.2.3-beta <1.3.0-0"],
16191663
caret_weird => ["^ 1.2 ^ 1", ">=1.2.0 <2.0.0-0"],
1664+
loose_eq1 => ["=0.7", ">=0.7.0 <0.8.0-0"],
1665+
loose_eq2 => ["=1", ">=1.0.0 <2.0.0-0"],
16201666
];
16211667

16221668
/*

0 commit comments

Comments
 (0)