Remove stale backup of version::mod.rs

This commit is contained in:
Nathan Fisher 2023-04-09 01:38:39 -04:00
parent 4cede60a80
commit 407d12a711

View File

@ -1,206 +0,0 @@
use {
chrono::{offset::Utc, DateTime},
serde::{Deserialize, Serialize},
std::{error::Error, fmt, str::FromStr},
};
mod gitrev;
mod rapid;
mod semver;
pub use {gitrev::GitRev, rapid::Rapid, semver::SemVer};
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub enum Version {
Number(u32),
Rapid {
major: u32,
minor: u32,
},
SemVer {
major: u32,
minor: u32,
patch: u32,
},
Git {
hash: String,
datetime: DateTime<Utc>,
},
}
impl Default for Version {
fn default() -> Self {
Self::SemVer {
major: 0,
minor: 1,
patch: 0,
}
}
}
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Number(n) => write!(f, "{n}"),
Self::SemVer {
major,
minor,
patch,
} => {
let v = SemVer {
major: *major,
minor: *minor,
patch: *patch,
};
write!(f, "{v}")
}
Self::Rapid { major, minor } => {
let v = Rapid {
major: *major,
minor: *minor,
};
write!(f, "{v}")
}
Self::Git { hash, datetime } => {
let v = GitRev {
hash: hash.clone(),
datetime: *datetime,
};
write!(f, "{v}")
}
}
}
}
impl From<SemVer> for Version {
fn from(value: SemVer) -> Self {
Self::SemVer {
major: value.major,
minor: value.minor,
patch: value.patch,
}
}
}
impl From<Rapid> for Version {
fn from(value: Rapid) -> Self {
Self::Rapid {
major: value.major,
minor: value.minor,
}
}
}
impl From<GitRev> for Version {
fn from(value: GitRev) -> Self {
Self::Git {
hash: value.hash,
datetime: value.datetime,
}
}
}
impl From<u32> for Version {
fn from(value: u32) -> Self {
Self::Number(value)
}
}
impl PartialOrd for Version {
#[allow(clippy::many_single_char_names)]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match (self, other) {
(Self::Number(s), Self::Number(o)) => s.partial_cmp(o),
(
Self::SemVer {
major,
minor,
patch,
},
Self::SemVer {
major: a,
minor: b,
patch: c,
},
) => (major, minor, patch).partial_cmp(&(a, b, c)),
(Self::Rapid { major, minor }, Self::Rapid { major: a, minor: b }) => {
(major, minor).partial_cmp(&(a, b))
}
(
Self::Git {
hash: _a,
datetime: b,
},
Self::Git {
hash: _c,
datetime: d,
},
) => b.partial_cmp(&d),
(
Self::SemVer {
major,
minor,
patch,
},
Self::Rapid { major: a, minor: b },
) => SemVer {
major: *major,
minor: *minor,
patch: *patch,
}
.partial_cmp(&Rapid {
major: *a,
minor: *b,
}),
_ => None,
}
}
}
#[derive(Debug)]
pub struct ParseVersionError;
impl fmt::Display for ParseVersionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "error parsing version")
}
}
impl Error for ParseVersionError {}
impl FromStr for Version {
type Err = ParseVersionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Ok(v) = s.parse::<SemVer>() {
Ok(v.into())
} else if let Ok(v) = s.parse::<Rapid>() {
Ok(v.into())
} else if let Ok(v) = s.parse::<GitRev>() {
Ok(v.into())
} else if let Ok(v) = s.parse::<u32>() {
Ok(v.into())
} else {
Err(ParseVersionError)
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn cmp_semver_rapid_gt() {
let sem = "1.42.1".parse::<SemVer>().unwrap();
let rpd = "1.42".parse::<Rapid>().unwrap();
assert!(sem > rpd);
}
#[test]
fn cmp_semver_rapid_eq() {
let sem = "1.42.0".parse::<SemVer>().unwrap();
let rpd = "1.42".parse::<Rapid>().unwrap();
assert!(sem == rpd);
}
}