From 6159bb321bc8b14fa4f5832c34ca50a76034e91f Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Wed, 10 Jan 2024 13:16:08 -0500 Subject: [PATCH] Rework `Version` into tuple struct with included Arch --- src/gitrev.rs | 13 +------------ src/lib.rs | 52 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/gitrev.rs b/src/gitrev.rs index 2099853..50c584e 100644 --- a/src/gitrev.rs +++ b/src/gitrev.rs @@ -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 for GitRev { } } -impl TryFrom for GitRev { - type Error = Error; - - fn try_from(value: Version) -> Result { - match value { - Version::GitRev(g) => Ok(g), - _ => Err(Error::ParseGitRev), - } - } -} - #[cfg(test)] mod test { use std::{thread, time::Duration}; diff --git a/src/lib.rs b/src/lib.rs index 7ae7b28..333fb47 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { + let Some((s, Some(arch))) = s.split_once('-').map(|(s, a)| (s, a.parse::().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 for Version { +impl From for Kind { fn from(value: Simple) -> Self { Self::Simple(value) } } -impl From for Version { +impl From for Kind { fn from(value: Rapid) -> Self { Self::Rapid(value) } } -impl From for Version { +impl From for Kind { fn from(value: SemVer) -> Self { Self::SemVer(value) } } -impl From for Version { +impl From for Kind { fn from(value: Extended) -> Self { Self::Extended(value) } } -impl From for Version { +impl From 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 { match (self, other) { (Self::Simple(a), Self::Simple(b)) => a.partial_cmp(b),