From 9255c3e1f298f23bee3b647c9ab79a12cf625541 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Mon, 15 Jan 2024 19:02:53 -0500 Subject: [PATCH] Refactor and fix Clippy lints --- src/arch.rs | 3 +- src/extended.rs | 5 +- src/gitrev.rs | 15 ++++- src/lib.rs | 158 +--------------------------------------------- src/prelude.rs | 4 ++ src/prerelease.rs | 26 ++------ src/rapid.rs | 5 +- src/semver.rs | 5 +- src/simple.rs | 5 +- src/version.rs | 154 ++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 198 insertions(+), 182 deletions(-) create mode 100644 src/prelude.rs create mode 100644 src/version.rs diff --git a/src/arch.rs b/src/arch.rs index 1daecb2..4b428a4 100644 --- a/src/arch.rs +++ b/src/arch.rs @@ -1,10 +1,11 @@ use { - crate::Error, + crate::error::Error, serde::{Deserialize, Serialize}, std::{fmt, str}, }; #[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)] +/// Represents the processor architecture the software is compiled for pub enum Arch { Any, Arm, diff --git a/src/extended.rs b/src/extended.rs index 931b805..6d63d34 100644 --- a/src/extended.rs +++ b/src/extended.rs @@ -1,5 +1,8 @@ use { - crate::{Error, GitRev, PreRelease, Rapid, SemVer, Simple, MAX_U12}, + crate::{ + error::Error, gitrev::GitRev, prerelease::PreRelease, rapid::Rapid, semver::SemVer, + simple::Simple, MAX_U12, + }, serde::{Deserialize, Serialize}, std::{cmp::Ordering, fmt, str::FromStr}, }; diff --git a/src/gitrev.rs b/src/gitrev.rs index e7c34b3..69e1d4b 100644 --- a/src/gitrev.rs +++ b/src/gitrev.rs @@ -1,12 +1,21 @@ use { - crate::{Error, Extended, Kind, Rapid, SemVer, Simple, Version}, + crate::{ + error::Error, extended::Extended, rapid::Rapid, semver::SemVer, simple::Simple, + version::Kind, version::Version, + }, chrono::{offset::Utc, DateTime}, serde::{Deserialize, Serialize}, std::{cmp::Ordering, fmt, str::FromStr}, }; +/// Represents a Git revisionrather than a regular release. +/// A `GitRev` release contains the revision's hash and the Date/Time of the release. +/// Git revisions are not the preferred method of distribution for obvious reasons, as +/// they generally do not represent a stable release of the code. Another drawback is +/// that a Git revision can only be compared against another Git revision, making it +/// impossible to properly order updates between a proper SemVer type release and a\ +/// Git revision. #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] -/// Represents a Git revision pub struct GitRev { /// the short revision hash pub hash: String, @@ -22,7 +31,7 @@ impl fmt::Display for GitRev { impl PartialOrd for GitRev { fn partial_cmp(&self, other: &Self) -> Option { - self.datetime.partial_cmp(&other.datetime) + Some(self.cmp(other)) } } diff --git a/src/lib.rs b/src/lib.rs index 2dbd867..d6e5ae9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,166 +1,14 @@ -use { - serde::{Deserialize, Serialize}, - std::{fmt, str::FromStr}, -}; - mod arch; mod error; mod extended; mod gitrev; +pub mod prelude; mod prerelease; mod rapid; mod semver; mod simple; +mod version; -pub use { - arch::Arch, error::Error, extended::Extended, gitrev::GitRev, prerelease::PreRelease, - rapid::Rapid, semver::SemVer, simple::Simple, -}; +pub use version::Version; pub static MAX_U12: u16 = 4096; - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub enum Kind { - Simple(Simple), - Rapid(Rapid), - SemVer(SemVer), - Extended(Extended), - 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.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), - } - } -} - -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(Kind::Simple(v), arch)) - } else if let Ok(v) = Rapid::from_str(s) { - Ok(Self(Kind::Rapid(v), arch)) - } else if let Ok(v) = SemVer::from_str(s) { - Ok(Self(Kind::SemVer(v), arch)) - } else if let Ok(v) = Extended::from_str(s) { - Ok(Self(Kind::Extended(v), arch)) - } else if let Ok(v) = GitRev::from_str(s) { - Ok(Self(Kind::GitRev(v), arch)) - } else { - Err(Error::ParseVersion) - } - } -} - -impl From for Kind { - fn from(value: Simple) -> Self { - Self::Simple(value) - } -} - -impl From for Kind { - fn from(value: Rapid) -> Self { - Self::Rapid(value) - } -} - -impl From for Kind { - fn from(value: SemVer) -> Self { - Self::SemVer(value) - } -} - -impl From for Kind { - fn from(value: Extended) -> Self { - Self::Extended(value) - } -} - -impl From for Kind { - fn from(value: GitRev) -> Self { - Self::GitRev(value) - } -} - -impl PartialEq for Kind { - fn eq(&self, other: &Self) -> bool { - match (self, other) { - (Self::Simple(a), Self::Simple(b)) => a == b, - (Self::Simple(a), Self::Rapid(b)) => a == b, - (Self::Simple(a), Self::SemVer(b)) => a == b, - (Self::Simple(a), Self::Extended(b)) => a == b, - (Self::Rapid(a), Self::Simple(b)) => a == b, - (Self::Rapid(a), Self::Rapid(b)) => a == b, - (Self::Rapid(a), Self::SemVer(b)) => a == b, - (Self::Rapid(a), Self::Extended(b)) => a == b, - (Self::SemVer(a), Self::Simple(b)) => a == b, - (Self::SemVer(a), Self::Rapid(b)) => a == b, - (Self::SemVer(a), Self::SemVer(b)) => a == b, - (Self::SemVer(a), Self::Extended(b)) => a == b, - (Self::Extended(a), Self::Simple(b)) => a == b, - (Self::Extended(a), Self::Rapid(b)) => a == b, - (Self::Extended(a), Self::SemVer(b)) => a == b, - (Self::Extended(a), Self::Extended(b)) => a == b, - (Self::GitRev(a), Self::GitRev(b)) => a == b, - _ => false, - } - } -} - -impl Eq for Kind {} - -impl PartialOrd for Kind { - fn partial_cmp(&self, other: &Self) -> Option { - match (self, other) { - (Self::Simple(a), Self::Simple(b)) => a.partial_cmp(b), - (Self::Simple(a), Self::Rapid(b)) => a.partial_cmp(b), - (Self::Simple(a), Self::SemVer(b)) => a.partial_cmp(b), - (Self::Simple(a), Self::Extended(b)) => a.partial_cmp(b), - (Self::Rapid(a), Self::Simple(b)) => a.partial_cmp(b), - (Self::Rapid(a), Self::Rapid(b)) => a.partial_cmp(b), - (Self::Rapid(a), Self::SemVer(b)) => a.partial_cmp(b), - (Self::Rapid(a), Self::Extended(b)) => a.partial_cmp(b), - (Self::SemVer(a), Self::Simple(b)) => a.partial_cmp(b), - (Self::SemVer(a), Self::Rapid(b)) => a.partial_cmp(b), - (Self::SemVer(a), Self::SemVer(b)) => a.partial_cmp(b), - (Self::SemVer(a), Self::Extended(b)) => a.partial_cmp(b), - (Self::Extended(a), Self::Simple(b)) => a.partial_cmp(b), - (Self::Extended(a), Self::Rapid(b)) => a.partial_cmp(b), - (Self::Extended(a), Self::SemVer(b)) => a.partial_cmp(b), - (Self::Extended(a), Self::Extended(b)) => a.partial_cmp(b), - (Self::GitRev(a), Self::GitRev(b)) => a.partial_cmp(b), - _ => None, - } - } -} - -impl PartialEq for Version { - fn eq(&self, other: &Self) -> bool { - self.0 == other.0 && self.1 == other.1 - } -} - -impl PartialOrd for Version { - fn partial_cmp(&self, other: &Self) -> Option { - if self.1 == other.1 { - self.0.partial_cmp(&other.0) - } else { - None - } - } -} diff --git a/src/prelude.rs b/src/prelude.rs new file mode 100644 index 0000000..b599f4d --- /dev/null +++ b/src/prelude.rs @@ -0,0 +1,4 @@ +pub use crate::{ + arch::Arch, error::Error as VersionError, extended::Extended, gitrev::GitRev, rapid::Rapid, + semver::SemVer, simple::Simple, version::Kind as VersionKind, +}; diff --git a/src/prerelease.rs b/src/prerelease.rs index 6f45ff8..4fcc315 100644 --- a/src/prerelease.rs +++ b/src/prerelease.rs @@ -1,5 +1,5 @@ use { - crate::Error, + crate::error::Error, serde::{Deserialize, Serialize}, std::{cmp, fmt, num::NonZeroU16, str}, }; @@ -18,31 +18,19 @@ pub enum PreRelease { impl PreRelease { pub fn is_prerelease(&self) -> bool { - match self { - Self::None => false, - _ => true, - } + !matches!(self, Self::None) } pub fn is_alpha(&self) -> bool { - match self { - Self::Alpha(_) => true, - _ => false, - } + matches!(self, Self::Alpha(_)) } pub fn is_beta(&self) -> bool { - match self { - Self::Beta(_) => true, - _ => false, - } + matches!(self, Self::Beta(_)) } pub fn is_rc(&self) -> bool { - match self { - Self::RC(_) => true, - _ => false, - } + matches!(self, Self::RC(_)) } pub fn number(&self) -> Option { @@ -187,9 +175,9 @@ impl From for u16 { } impl TryFrom for PreRelease { - type Error = crate::Error; + type Error = crate::error::Error; - fn try_from(value: u16) -> Result { + fn try_from(value: u16) -> Result { let mask = 0o1_777; let v = value & mask; let flag = value & !mask; diff --git a/src/rapid.rs b/src/rapid.rs index 6fdb68b..acbcc53 100644 --- a/src/rapid.rs +++ b/src/rapid.rs @@ -1,5 +1,8 @@ use { - crate::{Error, Extended, GitRev, PreRelease, SemVer, Simple, MAX_U12}, + crate::{ + error::Error, extended::Extended, gitrev::GitRev, prerelease::PreRelease, semver::SemVer, + simple::Simple, MAX_U12, + }, serde::{Deserialize, Serialize}, std::{cmp::Ordering, fmt, str::FromStr}, }; diff --git a/src/semver.rs b/src/semver.rs index 51eecc7..ce53845 100644 --- a/src/semver.rs +++ b/src/semver.rs @@ -1,5 +1,8 @@ use { - crate::{Error, Extended, GitRev, PreRelease, Rapid, Simple, MAX_U12}, + crate::{ + error::Error, extended::Extended, gitrev::GitRev, prerelease::PreRelease, rapid::Rapid, + simple::Simple, MAX_U12, + }, serde::{Deserialize, Serialize}, std::{cmp::Ordering, fmt, str::FromStr}, }; diff --git a/src/simple.rs b/src/simple.rs index 23b02c3..82fbdbe 100644 --- a/src/simple.rs +++ b/src/simple.rs @@ -1,5 +1,8 @@ use { - crate::{Error, Extended, GitRev, PreRelease, Rapid, SemVer, MAX_U12}, + crate::{ + error::Error, extended::Extended, gitrev::GitRev, prerelease::PreRelease, rapid::Rapid, + semver::SemVer, MAX_U12, + }, serde::{Deserialize, Serialize}, std::{cmp::Ordering, fmt, str::FromStr}, }; diff --git a/src/version.rs b/src/version.rs new file mode 100644 index 0000000..48b4d1b --- /dev/null +++ b/src/version.rs @@ -0,0 +1,154 @@ +use { + crate::{ + arch::Arch, error::Error, extended::Extended, gitrev::GitRev, rapid::Rapid, semver::SemVer, + simple::Simple, + }, + serde::{Deserialize, Serialize}, + std::{fmt, str::FromStr}, +}; + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub enum Kind { + Simple(Simple), + Rapid(Rapid), + SemVer(SemVer), + Extended(Extended), + GitRev(GitRev), +} + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct Version(pub Kind, pub Arch); + +impl fmt::Display for Version { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + 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), + } + } +} + +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(Kind::Simple(v), arch)) + } else if let Ok(v) = Rapid::from_str(s) { + Ok(Self(Kind::Rapid(v), arch)) + } else if let Ok(v) = SemVer::from_str(s) { + Ok(Self(Kind::SemVer(v), arch)) + } else if let Ok(v) = Extended::from_str(s) { + Ok(Self(Kind::Extended(v), arch)) + } else if let Ok(v) = GitRev::from_str(s) { + Ok(Self(Kind::GitRev(v), arch)) + } else { + Err(Error::ParseVersion) + } + } +} + +impl From for Kind { + fn from(value: Simple) -> Self { + Self::Simple(value) + } +} + +impl From for Kind { + fn from(value: Rapid) -> Self { + Self::Rapid(value) + } +} + +impl From for Kind { + fn from(value: SemVer) -> Self { + Self::SemVer(value) + } +} + +impl From for Kind { + fn from(value: Extended) -> Self { + Self::Extended(value) + } +} + +impl From for Kind { + fn from(value: GitRev) -> Self { + Self::GitRev(value) + } +} + +impl PartialEq for Kind { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (Self::Simple(a), Self::Simple(b)) => a == b, + (Self::Simple(a), Self::Rapid(b)) => a == b, + (Self::Simple(a), Self::SemVer(b)) => a == b, + (Self::Simple(a), Self::Extended(b)) => a == b, + (Self::Rapid(a), Self::Simple(b)) => a == b, + (Self::Rapid(a), Self::Rapid(b)) => a == b, + (Self::Rapid(a), Self::SemVer(b)) => a == b, + (Self::Rapid(a), Self::Extended(b)) => a == b, + (Self::SemVer(a), Self::Simple(b)) => a == b, + (Self::SemVer(a), Self::Rapid(b)) => a == b, + (Self::SemVer(a), Self::SemVer(b)) => a == b, + (Self::SemVer(a), Self::Extended(b)) => a == b, + (Self::Extended(a), Self::Simple(b)) => a == b, + (Self::Extended(a), Self::Rapid(b)) => a == b, + (Self::Extended(a), Self::SemVer(b)) => a == b, + (Self::Extended(a), Self::Extended(b)) => a == b, + (Self::GitRev(a), Self::GitRev(b)) => a == b, + _ => false, + } + } +} + +impl Eq for Kind {} + +impl PartialOrd for Kind { + fn partial_cmp(&self, other: &Self) -> Option { + match (self, other) { + (Self::Simple(a), Self::Simple(b)) => a.partial_cmp(b), + (Self::Simple(a), Self::Rapid(b)) => a.partial_cmp(b), + (Self::Simple(a), Self::SemVer(b)) => a.partial_cmp(b), + (Self::Simple(a), Self::Extended(b)) => a.partial_cmp(b), + (Self::Rapid(a), Self::Simple(b)) => a.partial_cmp(b), + (Self::Rapid(a), Self::Rapid(b)) => a.partial_cmp(b), + (Self::Rapid(a), Self::SemVer(b)) => a.partial_cmp(b), + (Self::Rapid(a), Self::Extended(b)) => a.partial_cmp(b), + (Self::SemVer(a), Self::Simple(b)) => a.partial_cmp(b), + (Self::SemVer(a), Self::Rapid(b)) => a.partial_cmp(b), + (Self::SemVer(a), Self::SemVer(b)) => a.partial_cmp(b), + (Self::SemVer(a), Self::Extended(b)) => a.partial_cmp(b), + (Self::Extended(a), Self::Simple(b)) => a.partial_cmp(b), + (Self::Extended(a), Self::Rapid(b)) => a.partial_cmp(b), + (Self::Extended(a), Self::SemVer(b)) => a.partial_cmp(b), + (Self::Extended(a), Self::Extended(b)) => a.partial_cmp(b), + (Self::GitRev(a), Self::GitRev(b)) => a.partial_cmp(b), + _ => None, + } + } +} + +impl PartialEq for Version { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 && self.1 == other.1 + } +} + +impl PartialOrd for Version { + fn partial_cmp(&self, other: &Self) -> Option { + if self.1 == other.1 { + self.0.partial_cmp(&other.0) + } else { + None + } + } +}