Skip to content

Commit 90017c0

Browse files
committed
refactor(util-schemas): error type for RustVerion
1 parent d7b811b commit 90017c0

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

crates/cargo-util-schemas/src/manifest.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use serde_untagged::UntaggedEnumVisitor;
1818

1919
use crate::core::PackageIdSpec;
2020
use crate::core::PartialVersion;
21+
use crate::core::PartialVersionError;
2122
use crate::restricted_names;
2223

2324
/// This type is used to deserialize `Cargo.toml` files.
@@ -1334,15 +1335,15 @@ impl std::ops::Deref for RustVersion {
13341335
}
13351336

13361337
impl std::str::FromStr for RustVersion {
1337-
type Err = anyhow::Error;
1338+
type Err = RustVersionError;
13381339

13391340
fn from_str(value: &str) -> Result<Self, Self::Err> {
13401341
let partial = value.parse::<PartialVersion>()?;
13411342
if partial.pre.is_some() {
1342-
anyhow::bail!("unexpected prerelease field, expected a version like \"1.32\"")
1343+
return Err(RustVersionError::Prerelease);
13431344
}
13441345
if partial.build.is_some() {
1345-
anyhow::bail!("unexpected prerelease field, expected a version like \"1.32\"")
1346+
return Err(RustVersionError::BuildMetadata);
13461347
}
13471348
Ok(Self(partial))
13481349
}
@@ -1366,6 +1367,20 @@ impl Display for RustVersion {
13661367
}
13671368
}
13681369

1370+
/// Error parsing a [`PartialVersion`].
1371+
#[non_exhaustive]
1372+
#[derive(Debug, thiserror::Error)]
1373+
pub enum RustVersionError {
1374+
#[error("unexpected prerelease field, expected a version like \"1.32\"")]
1375+
Prerelease,
1376+
1377+
#[error("unexpected build field, expected a version like \"1.32\"")]
1378+
BuildMetadata,
1379+
1380+
#[error(transparent)]
1381+
PartialVersion(#[from] PartialVersionError),
1382+
}
1383+
13691384
#[derive(Copy, Clone, Debug)]
13701385
pub struct InvalidCargoFeatures {}
13711386

0 commit comments

Comments
 (0)