Rework Version into tuple struct with included Arch

This commit is contained in:
Nathan Fisher 2024-01-10 13:16:08 -05:00
parent 2247d2c572
commit 6159bb321b
2 changed files with 32 additions and 33 deletions

View File

@ -1,5 +1,5 @@
use {
crate::{Error, Extended, Rapid, SemVer, Simple, Version},
crate::{Error, Extended, Rapid, SemVer, Simple},
chrono::{offset::Utc, DateTime},
serde::{Deserialize, Serialize},
std::{cmp::Ordering, fmt, str::FromStr},
@ -99,17 +99,6 @@ impl PartialOrd<Simple> for GitRev {
}
}
impl TryFrom<Version> for GitRev {
type Error = Error;
fn try_from(value: Version) -> Result<Self, Self::Error> {
match value {
Version::GitRev(g) => Ok(g),
_ => Err(Error::ParseGitRev),
}
}
}
#[cfg(test)]
mod test {
use std::{thread, time::Duration};

View File

@ -1,4 +1,7 @@
use std::{fmt, str::FromStr};
use {
serde::{Deserialize, Serialize},
std::{fmt, str::FromStr},
};
mod arch;
mod error;
@ -16,8 +19,8 @@ pub use {
pub static MAX_U12: u16 = 4096;
#[derive(Clone, Debug)]
pub enum Version {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum Kind {
Simple(Simple),
Rapid(Rapid),
SemVer(SemVer),
@ -25,14 +28,17 @@ pub enum Version {
GitRev(GitRev),
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Version(Kind, Arch);
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Simple(s) => write!(f, "{s}"),
Self::Rapid(r) => write!(f, "{r}"),
Self::SemVer(s) => write!(f, "{s}"),
Self::Extended(x) => write!(f, "{x}"),
Self::GitRev(g) => write!(f, "{g}"),
match &self.0 {
Kind::Simple(s) => write!(f, "{s}-{}", self.1),
Kind::Rapid(r) => write!(f, "{r}-{}", self.1),
Kind::SemVer(s) => write!(f, "{s}-{}", self.1),
Kind::Extended(x) => write!(f, "{x}-{}", self.1),
Kind::GitRev(g) => write!(f, "{g}-{}", self.1),
}
}
}
@ -41,53 +47,57 @@ impl FromStr for Version {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let Some((s, Some(arch))) = s.split_once('-').map(|(s, a)| (s, a.parse::<Arch>().ok()))
else {
return Err(Error::ParseArch);
};
if let Ok(v) = Simple::from_str(s) {
Ok(Self::Simple(v))
Ok(Self(Kind::Simple(v), arch))
} else if let Ok(v) = Rapid::from_str(s) {
Ok(Self::Rapid(v))
Ok(Self(Kind::Rapid(v), arch))
} else if let Ok(v) = SemVer::from_str(s) {
Ok(Self::SemVer(v))
Ok(Self(Kind::SemVer(v), arch))
} else if let Ok(v) = Extended::from_str(s) {
Ok(Self::Extended(v))
Ok(Self(Kind::Extended(v), arch))
} else if let Ok(v) = GitRev::from_str(s) {
Ok(Self::GitRev(v))
Ok(Self(Kind::GitRev(v), arch))
} else {
Err(Error::ParseVersion)
}
}
}
impl From<Simple> for Version {
impl From<Simple> for Kind {
fn from(value: Simple) -> Self {
Self::Simple(value)
}
}
impl From<Rapid> for Version {
impl From<Rapid> for Kind {
fn from(value: Rapid) -> Self {
Self::Rapid(value)
}
}
impl From<SemVer> for Version {
impl From<SemVer> for Kind {
fn from(value: SemVer) -> Self {
Self::SemVer(value)
}
}
impl From<Extended> for Version {
impl From<Extended> for Kind {
fn from(value: Extended) -> Self {
Self::Extended(value)
}
}
impl From<GitRev> for Version {
impl From<GitRev> for Kind {
fn from(value: GitRev) -> Self {
Self::GitRev(value)
}
}
impl PartialEq for Version {
impl PartialEq for Kind {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Simple(a), Self::Simple(b)) => a == b,
@ -112,7 +122,7 @@ impl PartialEq for Version {
}
}
impl PartialOrd for Version {
impl PartialOrd for Kind {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match (self, other) {
(Self::Simple(a), Self::Simple(b)) => a.partial_cmp(b),