refactor(schema): Pull RustVersion out into a mod

This commit is contained in:
Ed Page 2024-03-04 16:45:58 -06:00
parent a980eed118
commit 46584a4d6b
2 changed files with 84 additions and 75 deletions

View file

@ -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 {}

View 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),
}