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