Skip to content

Commit

Permalink
fix(manifest): Improve rust-version error messages
Browse files Browse the repository at this point in the history
Since we have tests for a couple of cases, I figured we could
improve the error messages for them.
  • Loading branch information
epage committed Aug 25, 2023
1 parent 423a334 commit 1701b4e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/cargo/util/semver_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,18 @@ impl std::str::FromStr for PartialVersion {
// HACK: `PartialVersion` is a subset of the `VersionReq` syntax that only ever
// has one comparator with a required minor and optional patch, and uses no
// other features.
if is_req(value) {
anyhow::bail!("unexpected version requirement, expected a version like \"1.32\"")
}
let version_req = match semver::VersionReq::parse(value) {
// Exclude semver operators like `^` and pre-release identifiers
Ok(req) if value.chars().all(|c| c.is_ascii_digit() || c == '.') => req,
Err(_) if value.contains('+') => {
anyhow::bail!("unexpected build field, expected a version like \"1.32\"")
}
Err(_) if value.contains('-') => {
anyhow::bail!("unexpected prerelease field, expected a version like \"1.32\"")
}
_ => anyhow::bail!("expected a version like \"1.32\""),
};
assert_eq!(
Expand Down Expand Up @@ -211,6 +220,13 @@ impl<'de> serde::Deserialize<'de> for PartialVersion {
}
}

fn is_req(value: &str) -> bool {
let Some(first) = value.chars().next() else {
return false;
};
"<>=^~".contains(first) || value.contains('*') || value.contains(',')
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 2 additions & 2 deletions tests/testsuite/rust_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Caused by:
|
6 | rust-version = \"^1.43\"
| ^^^^^^^
expected a version like \"1.32\"",
unexpected version requirement, expected a version like \"1.32\"",
)
.run();
}
Expand Down Expand Up @@ -85,7 +85,7 @@ Caused by:
|
6 | rust-version = \"1.43-beta.1\"
| ^^^^^^^^^^^^^
expected a version like \"1.32\"",
unexpected prerelease field, expected a version like \"1.32\"",
)
.run();
}
Expand Down

0 comments on commit 1701b4e

Please sign in to comment.