From 407d12a7113a453020c7297ac7b4a80952f02d1d Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Sun, 9 Apr 2023 01:38:39 -0400 Subject: [PATCH] Remove stale backup of version::mod.rs --- src/version/mod.rs.bak | 206 ----------------------------------------- 1 file changed, 206 deletions(-) delete mode 100644 src/version/mod.rs.bak diff --git a/src/version/mod.rs.bak b/src/version/mod.rs.bak deleted file mode 100644 index 9208624..0000000 --- a/src/version/mod.rs.bak +++ /dev/null @@ -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, - }, -} - -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 for Version { - fn from(value: SemVer) -> Self { - Self::SemVer { - major: value.major, - minor: value.minor, - patch: value.patch, - } - } -} - -impl From for Version { - fn from(value: Rapid) -> Self { - Self::Rapid { - major: value.major, - minor: value.minor, - } - } -} - -impl From for Version { - fn from(value: GitRev) -> Self { - Self::Git { - hash: value.hash, - datetime: value.datetime, - } - } -} - -impl From 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 { - 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 { - if let Ok(v) = s.parse::() { - Ok(v.into()) - } else if let Ok(v) = s.parse::() { - Ok(v.into()) - } else if let Ok(v) = s.parse::() { - Ok(v.into()) - } else if let Ok(v) = s.parse::() { - Ok(v.into()) - } else { - Err(ParseVersionError) - } - } -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - fn cmp_semver_rapid_gt() { - let sem = "1.42.1".parse::().unwrap(); - let rpd = "1.42".parse::().unwrap(); - assert!(sem > rpd); - } - - #[test] - fn cmp_semver_rapid_eq() { - let sem = "1.42.0".parse::().unwrap(); - let rpd = "1.42".parse::().unwrap(); - assert!(sem == rpd); - } -}