mirror of
https://github.com/rust-lang/cargo
synced 2024-10-05 23:39:47 +00:00
refactor(schema): Pull RustVersion out into a mod
This commit is contained in:
parent
a980eed118
commit
46584a4d6b
|
@ -16,11 +16,13 @@ use serde::{Deserialize, Serialize};
|
|||
use serde_untagged::UntaggedEnumVisitor;
|
||||
|
||||
use crate::core::PackageIdSpec;
|
||||
use crate::core::PartialVersion;
|
||||
use crate::core::PartialVersionError;
|
||||
use crate::restricted_names;
|
||||
|
||||
mod rust_version;
|
||||
|
||||
pub use crate::restricted_names::NameValidationError;
|
||||
pub use rust_version::RustVersion;
|
||||
pub use rust_version::RustVersionError;
|
||||
|
||||
/// This type is used to deserialize `Cargo.toml` files.
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
|
@ -1399,79 +1401,6 @@ pub enum TomlLintLevel {
|
|||
Allow,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug, serde::Serialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct RustVersion(PartialVersion);
|
||||
|
||||
impl std::ops::Deref for RustVersion {
|
||||
type Target = PartialVersion;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for RustVersion {
|
||||
type Err = RustVersionError;
|
||||
|
||||
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
||||
let partial = value.parse::<PartialVersion>();
|
||||
let partial = partial.map_err(RustVersionErrorKind::PartialVersion)?;
|
||||
partial.try_into()
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<PartialVersion> for RustVersion {
|
||||
type Error = RustVersionError;
|
||||
|
||||
fn try_from(partial: PartialVersion) -> Result<Self, Self::Error> {
|
||||
if partial.pre.is_some() {
|
||||
return Err(RustVersionErrorKind::Prerelease.into());
|
||||
}
|
||||
if partial.build.is_some() {
|
||||
return Err(RustVersionErrorKind::BuildMetadata.into());
|
||||
}
|
||||
Ok(Self(partial))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> serde::Deserialize<'de> for RustVersion {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
UntaggedEnumVisitor::new()
|
||||
.expecting("SemVer version")
|
||||
.string(|value| value.parse().map_err(serde::de::Error::custom))
|
||||
.deserialize(deserializer)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for RustVersion {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
/// Error parsing a [`RustVersion`].
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[error(transparent)]
|
||||
pub struct RustVersionError(#[from] RustVersionErrorKind);
|
||||
|
||||
/// Non-public error kind for [`RustVersionError`].
|
||||
#[non_exhaustive]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
enum RustVersionErrorKind {
|
||||
#[error("unexpected prerelease field, expected a version like \"1.32\"")]
|
||||
Prerelease,
|
||||
|
||||
#[error("unexpected build field, expected a version like \"1.32\"")]
|
||||
BuildMetadata,
|
||||
|
||||
#[error(transparent)]
|
||||
PartialVersion(#[from] PartialVersionError),
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct InvalidCargoFeatures {}
|
||||
|
||||
|
|
80
crates/cargo-util-schemas/src/manifest/rust_version.rs
Normal file
80
crates/cargo-util-schemas/src/manifest/rust_version.rs
Normal file
|
@ -0,0 +1,80 @@
|
|||
use std::fmt;
|
||||
use std::fmt::Display;
|
||||
|
||||
use serde_untagged::UntaggedEnumVisitor;
|
||||
|
||||
use crate::core::PartialVersion;
|
||||
use crate::core::PartialVersionError;
|
||||
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug, serde::Serialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct RustVersion(PartialVersion);
|
||||
|
||||
impl std::ops::Deref for RustVersion {
|
||||
type Target = PartialVersion;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for RustVersion {
|
||||
type Err = RustVersionError;
|
||||
|
||||
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
||||
let partial = value.parse::<PartialVersion>();
|
||||
let partial = partial.map_err(RustVersionErrorKind::PartialVersion)?;
|
||||
partial.try_into()
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<PartialVersion> for RustVersion {
|
||||
type Error = RustVersionError;
|
||||
|
||||
fn try_from(partial: PartialVersion) -> Result<Self, Self::Error> {
|
||||
if partial.pre.is_some() {
|
||||
return Err(RustVersionErrorKind::Prerelease.into());
|
||||
}
|
||||
if partial.build.is_some() {
|
||||
return Err(RustVersionErrorKind::BuildMetadata.into());
|
||||
}
|
||||
Ok(Self(partial))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> serde::Deserialize<'de> for RustVersion {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
UntaggedEnumVisitor::new()
|
||||
.expecting("SemVer version")
|
||||
.string(|value| value.parse().map_err(serde::de::Error::custom))
|
||||
.deserialize(deserializer)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for RustVersion {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
/// Error parsing a [`RustVersion`].
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[error(transparent)]
|
||||
pub struct RustVersionError(#[from] RustVersionErrorKind);
|
||||
|
||||
/// Non-public error kind for [`RustVersionError`].
|
||||
#[non_exhaustive]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
enum RustVersionErrorKind {
|
||||
#[error("unexpected prerelease field, expected a version like \"1.32\"")]
|
||||
Prerelease,
|
||||
|
||||
#[error("unexpected build field, expected a version like \"1.32\"")]
|
||||
BuildMetadata,
|
||||
|
||||
#[error(transparent)]
|
||||
PartialVersion(#[from] PartialVersionError),
|
||||
}
|
Loading…
Reference in a new issue