Skip to content

Commit 6d8d3b6

Browse files
committed
fix(schemas): Allow parsing pre-release with X
1 parent 6f22e9d commit 6d8d3b6

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

crates/cargo-util-schemas/src/core/partial_version.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ impl std::str::FromStr for PartialVersion {
8383
type Err = PartialVersionError;
8484

8585
fn from_str(value: &str) -> Result<Self, Self::Err> {
86-
if is_req(value) {
87-
return Err(ErrorKind::VersionReq.into());
88-
}
8986
match semver::Version::parse(value) {
9087
Ok(ver) => Ok(ver.into()),
9188
Err(_) => {
@@ -96,9 +93,16 @@ impl std::str::FromStr for PartialVersion {
9693
Err(_) if value.contains('+') => return Err(ErrorKind::BuildMetadata.into()),
9794
Err(_) => return Err(ErrorKind::Unexpected.into()),
9895
};
99-
assert_eq!(version_req.comparators.len(), 1, "guaranteed by is_req");
96+
if version_req.comparators.len() != 1 {
97+
return Err(ErrorKind::VersionReq.into());
98+
}
10099
let comp = version_req.comparators.pop().unwrap();
101-
assert_eq!(comp.op, semver::Op::Caret, "guaranteed by is_req");
100+
if comp.op != semver::Op::Caret {
101+
return Err(ErrorKind::VersionReq.into());
102+
} else if value.starts_with('^') {
103+
// Can't distinguish between `^` present or not
104+
return Err(ErrorKind::VersionReq.into());
105+
}
102106
let pre = if comp.pre.is_empty() {
103107
None
104108
} else {
@@ -179,17 +183,6 @@ enum ErrorKind {
179183
Unexpected,
180184
}
181185

182-
fn is_req(value: &str) -> bool {
183-
let Some(first) = value.chars().next() else {
184-
return false;
185-
};
186-
"<>=^~".contains(first)
187-
|| value.contains('*')
188-
|| value.contains(',')
189-
|| value.contains('x')
190-
|| value.contains('X')
191-
}
192-
193186
#[cfg(test)]
194187
mod test {
195188
use super::*;
@@ -200,6 +193,8 @@ mod test {
200193
let cases = &[
201194
// Valid pre-release
202195
("1.43.0-beta.1", str!["1.43.0-beta.1"]),
196+
// Valid pre-release with wildcard
197+
("1.43.0-beta.1.x", str!["1.43.0-beta.1.x"]),
203198
];
204199
for (input, expected) in cases {
205200
let actual: Result<PartialVersion, _> = input.parse();

0 commit comments

Comments
 (0)