Skip to content

Commit 46584a4

Browse files
committed
refactor(schema): Pull RustVersion out into a mod
1 parent a980eed commit 46584a4

File tree

2 files changed

+84
-75
lines changed

2 files changed

+84
-75
lines changed

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

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ use serde::{Deserialize, Serialize};
1616
use serde_untagged::UntaggedEnumVisitor;
1717

1818
use crate::core::PackageIdSpec;
19-
use crate::core::PartialVersion;
20-
use crate::core::PartialVersionError;
2119
use crate::restricted_names;
2220

21+
mod rust_version;
22+
2323
pub use crate::restricted_names::NameValidationError;
24+
pub use rust_version::RustVersion;
25+
pub use rust_version::RustVersionError;
2426

2527
/// This type is used to deserialize `Cargo.toml` files.
2628
#[derive(Debug, Deserialize, Serialize)]
@@ -1399,79 +1401,6 @@ pub enum TomlLintLevel {
13991401
Allow,
14001402
}
14011403

1402-
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug, serde::Serialize)]
1403-
#[serde(transparent)]
1404-
pub struct RustVersion(PartialVersion);
1405-
1406-
impl std::ops::Deref for RustVersion {
1407-
type Target = PartialVersion;
1408-
1409-
fn deref(&self) -> &Self::Target {
1410-
&self.0
1411-
}
1412-
}
1413-
1414-
impl std::str::FromStr for RustVersion {
1415-
type Err = RustVersionError;
1416-
1417-
fn from_str(value: &str) -> Result<Self, Self::Err> {
1418-
let partial = value.parse::<PartialVersion>();
1419-
let partial = partial.map_err(RustVersionErrorKind::PartialVersion)?;
1420-
partial.try_into()
1421-
}
1422-
}
1423-
1424-
impl TryFrom<PartialVersion> for RustVersion {
1425-
type Error = RustVersionError;
1426-
1427-
fn try_from(partial: PartialVersion) -> Result<Self, Self::Error> {
1428-
if partial.pre.is_some() {
1429-
return Err(RustVersionErrorKind::Prerelease.into());
1430-
}
1431-
if partial.build.is_some() {
1432-
return Err(RustVersionErrorKind::BuildMetadata.into());
1433-
}
1434-
Ok(Self(partial))
1435-
}
1436-
}
1437-
1438-
impl<'de> serde::Deserialize<'de> for RustVersion {
1439-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1440-
where
1441-
D: serde::Deserializer<'de>,
1442-
{
1443-
UntaggedEnumVisitor::new()
1444-
.expecting("SemVer version")
1445-
.string(|value| value.parse().map_err(serde::de::Error::custom))
1446-
.deserialize(deserializer)
1447-
}
1448-
}
1449-
1450-
impl Display for RustVersion {
1451-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1452-
self.0.fmt(f)
1453-
}
1454-
}
1455-
1456-
/// Error parsing a [`RustVersion`].
1457-
#[derive(Debug, thiserror::Error)]
1458-
#[error(transparent)]
1459-
pub struct RustVersionError(#[from] RustVersionErrorKind);
1460-
1461-
/// Non-public error kind for [`RustVersionError`].
1462-
#[non_exhaustive]
1463-
#[derive(Debug, thiserror::Error)]
1464-
enum RustVersionErrorKind {
1465-
#[error("unexpected prerelease field, expected a version like \"1.32\"")]
1466-
Prerelease,
1467-
1468-
#[error("unexpected build field, expected a version like \"1.32\"")]
1469-
BuildMetadata,
1470-
1471-
#[error(transparent)]
1472-
PartialVersion(#[from] PartialVersionError),
1473-
}
1474-
14751404
#[derive(Copy, Clone, Debug)]
14761405
pub struct InvalidCargoFeatures {}
14771406

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use std::fmt;
2+
use std::fmt::Display;
3+
4+
use serde_untagged::UntaggedEnumVisitor;
5+
6+
use crate::core::PartialVersion;
7+
use crate::core::PartialVersionError;
8+
9+
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug, serde::Serialize)]
10+
#[serde(transparent)]
11+
pub struct RustVersion(PartialVersion);
12+
13+
impl std::ops::Deref for RustVersion {
14+
type Target = PartialVersion;
15+
16+
fn deref(&self) -> &Self::Target {
17+
&self.0
18+
}
19+
}
20+
21+
impl std::str::FromStr for RustVersion {
22+
type Err = RustVersionError;
23+
24+
fn from_str(value: &str) -> Result<Self, Self::Err> {
25+
let partial = value.parse::<PartialVersion>();
26+
let partial = partial.map_err(RustVersionErrorKind::PartialVersion)?;
27+
partial.try_into()
28+
}
29+
}
30+
31+
impl TryFrom<PartialVersion> for RustVersion {
32+
type Error = RustVersionError;
33+
34+
fn try_from(partial: PartialVersion) -> Result<Self, Self::Error> {
35+
if partial.pre.is_some() {
36+
return Err(RustVersionErrorKind::Prerelease.into());
37+
}
38+
if partial.build.is_some() {
39+
return Err(RustVersionErrorKind::BuildMetadata.into());
40+
}
41+
Ok(Self(partial))
42+
}
43+
}
44+
45+
impl<'de> serde::Deserialize<'de> for RustVersion {
46+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
47+
where
48+
D: serde::Deserializer<'de>,
49+
{
50+
UntaggedEnumVisitor::new()
51+
.expecting("SemVer version")
52+
.string(|value| value.parse().map_err(serde::de::Error::custom))
53+
.deserialize(deserializer)
54+
}
55+
}
56+
57+
impl Display for RustVersion {
58+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59+
self.0.fmt(f)
60+
}
61+
}
62+
63+
/// Error parsing a [`RustVersion`].
64+
#[derive(Debug, thiserror::Error)]
65+
#[error(transparent)]
66+
pub struct RustVersionError(#[from] RustVersionErrorKind);
67+
68+
/// Non-public error kind for [`RustVersionError`].
69+
#[non_exhaustive]
70+
#[derive(Debug, thiserror::Error)]
71+
enum RustVersionErrorKind {
72+
#[error("unexpected prerelease field, expected a version like \"1.32\"")]
73+
Prerelease,
74+
75+
#[error("unexpected build field, expected a version like \"1.32\"")]
76+
BuildMetadata,
77+
78+
#[error(transparent)]
79+
PartialVersion(#[from] PartialVersionError),
80+
}

0 commit comments

Comments
 (0)