From 748a786632dadfc411a5eefcc133b128efe17c26 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Mon, 8 Jan 2024 19:02:19 -0500 Subject: [PATCH] impl PartialEq and PartialOrd for all types --- src/extended.rs | 14 +++++++++ src/gitrev.rs | 60 +++++++++++++++++++++++++++++++++---- src/lib.rs | 80 +++++++++++++++++++++++++++++++++++++++++++++++++ src/rapid.rs | 63 +++++++++++++++++++++++++++++++++++++- src/semver.rs | 63 +++++++++++++++++++++++++++++++++++++- src/simple.rs | 60 ++++++++++++++++++++++++++++++++++++- 6 files changed, 331 insertions(+), 9 deletions(-) diff --git a/src/extended.rs b/src/extended.rs index b72b303..4911846 100644 --- a/src/extended.rs +++ b/src/extended.rs @@ -1,3 +1,5 @@ +use crate::GitRev; + use { crate::{Error, PreRelease, Rapid, SemVer, Simple, MAX_U12}, serde::{Deserialize, Serialize}, @@ -186,6 +188,12 @@ impl PartialEq for Extended { } } +impl PartialEq for Extended { + fn eq(&self, _other: &GitRev) -> bool { + false + } +} + impl PartialOrd for Extended { fn partial_cmp(&self, other: &SemVer) -> Option { Some(u64::from(*self).cmp(&u64::from(*other))) @@ -203,3 +211,9 @@ impl PartialOrd for Extended { Some(u64::from(*self).cmp(&u64::from(*other))) } } + +impl PartialOrd for Extended { + fn partial_cmp(&self, _other: &GitRev) -> Option { + None + } +} diff --git a/src/gitrev.rs b/src/gitrev.rs index 54f34fa..2099853 100644 --- a/src/gitrev.rs +++ b/src/gitrev.rs @@ -1,5 +1,5 @@ use { - crate::Error, + crate::{Error, Extended, Rapid, SemVer, Simple, Version}, chrono::{offset::Utc, DateTime}, serde::{Deserialize, Serialize}, std::{cmp::Ordering, fmt, str::FromStr}, @@ -50,17 +50,65 @@ impl FromStr for GitRev { Err(Error::ParseGitRev) } } -/* + +impl PartialEq for GitRev { + fn eq(&self, _other: &Extended) -> bool { + false + } +} + +impl PartialOrd for GitRev { + fn partial_cmp(&self, _other: &Extended) -> Option { + None + } +} + +impl PartialEq for GitRev { + fn eq(&self, _other: &SemVer) -> bool { + false + } +} + +impl PartialOrd for GitRev { + fn partial_cmp(&self, _other: &SemVer) -> Option { + None + } +} + +impl PartialEq for GitRev { + fn eq(&self, _other: &Rapid) -> bool { + false + } +} + +impl PartialOrd for GitRev { + fn partial_cmp(&self, _other: &Rapid) -> Option { + None + } +} + +impl PartialEq for GitRev { + fn eq(&self, _other: &Simple) -> bool { + false + } +} + +impl PartialOrd for GitRev { + fn partial_cmp(&self, _other: &Simple) -> Option { + None + } +} + impl TryFrom for GitRev { - type Error = ParseGitRevError; + type Error = Error; fn try_from(value: Version) -> Result { match value { - Version::Git(g) => Ok(g), - _ => Err(ParseGitRevError), + Version::GitRev(g) => Ok(g), + _ => Err(Error::ParseGitRev), } } -}*/ +} #[cfg(test)] mod test { diff --git a/src/lib.rs b/src/lib.rs index 2100744..3ab66be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,3 +21,83 @@ pub enum Version { Extended(Extended), GitRev(GitRev), } + +impl From for Version { + fn from(value: Simple) -> Self { + Self::Simple(value) + } +} + +impl From for Version { + fn from(value: Rapid) -> Self { + Self::Rapid(value) + } +} + +impl From for Version { + fn from(value: SemVer) -> Self { + Self::SemVer(value) + } +} + +impl From for Version { + fn from(value: Extended) -> Self { + Self::Extended(value) + } +} + +impl From for Version { + fn from(value: GitRev) -> Self { + Self::GitRev(value) + } +} + +impl PartialEq for Version { + 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 PartialOrd for Version { + 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, + } + } +} diff --git a/src/rapid.rs b/src/rapid.rs index ef179a2..6fdb68b 100644 --- a/src/rapid.rs +++ b/src/rapid.rs @@ -1,5 +1,5 @@ use { - crate::{Error, PreRelease, MAX_U12}, + crate::{Error, Extended, GitRev, PreRelease, SemVer, Simple, MAX_U12}, serde::{Deserialize, Serialize}, std::{cmp::Ordering, fmt, str::FromStr}, }; @@ -93,3 +93,64 @@ impl PartialOrd for Rapid { Some(self.cmp(other)) } } + +impl PartialEq for Rapid { + fn eq(&self, other: &Extended) -> bool { + self.major == other.major + && self.minor == other.minor + && other.patch == 0 + && other.build == 0 + && self.pre == other.pre + } +} + +impl PartialEq for Rapid { + fn eq(&self, other: &SemVer) -> bool { + self.major == other.major + && self.minor == other.minor + && other.patch == 0 + && self.pre == other.pre + } +} + +impl PartialEq for Rapid { + fn eq(&self, other: &Simple) -> bool { + self.major == other.major && self.minor == 0 && self.pre == other.pre + } +} + +impl PartialEq for Rapid { + fn eq(&self, _other: &GitRev) -> bool { + false + } +} + +impl PartialOrd for Rapid { + fn partial_cmp(&self, other: &Extended) -> Option { + let a = u64::from(*self); + let b = u64::from(*other); + a.partial_cmp(&b) + } +} + +impl PartialOrd for Rapid { + fn partial_cmp(&self, other: &SemVer) -> Option { + let a = u64::from(*self); + let b = u64::from(*other); + a.partial_cmp(&b) + } +} + +impl PartialOrd for Rapid { + fn partial_cmp(&self, other: &Simple) -> Option { + let a = u64::from(*self); + let b = u64::from(*other); + a.partial_cmp(&b) + } +} + +impl PartialOrd for Rapid { + fn partial_cmp(&self, _other: &GitRev) -> Option { + None + } +} diff --git a/src/semver.rs b/src/semver.rs index c7df370..51eecc7 100644 --- a/src/semver.rs +++ b/src/semver.rs @@ -1,5 +1,5 @@ use { - crate::{Error, PreRelease, MAX_U12}, + crate::{Error, Extended, GitRev, PreRelease, Rapid, Simple, MAX_U12}, serde::{Deserialize, Serialize}, std::{cmp::Ordering, fmt, str::FromStr}, }; @@ -108,6 +108,67 @@ impl PartialOrd for SemVer { } } +impl PartialEq for SemVer { + fn eq(&self, other: &Extended) -> bool { + self.major == other.major + && self.minor == other.minor + && self.patch == other.patch + && other.build == 0 + && self.pre == other.pre + } +} + +impl PartialEq for SemVer { + fn eq(&self, other: &Rapid) -> bool { + self.major == other.major + && self.minor == other.minor + && self.patch == 0 + && self.pre == other.pre + } +} + +impl PartialEq for SemVer { + fn eq(&self, other: &Simple) -> bool { + self.major == other.major && self.minor == 0 && self.patch == 0 && self.pre == other.pre + } +} + +impl PartialEq for SemVer { + fn eq(&self, _other: &GitRev) -> bool { + false + } +} + +impl PartialOrd for SemVer { + fn partial_cmp(&self, other: &Extended) -> Option { + let a = u64::from(*self); + let b = u64::from(*other); + Some(a.cmp(&b)) + } +} + +impl PartialOrd for SemVer { + fn partial_cmp(&self, other: &Rapid) -> Option { + let a = u64::from(*self); + let b = u64::from(*other); + Some(a.cmp(&b)) + } +} + +impl PartialOrd for SemVer { + fn partial_cmp(&self, other: &Simple) -> Option { + let a = u64::from(*self); + let b = u64::from(*other); + Some(a.cmp(&b)) + } +} + +impl PartialOrd for SemVer { + fn partial_cmp(&self, _other: &GitRev) -> Option { + None + } +} + #[cfg(test)] mod tests { use std::num::NonZeroU16; diff --git a/src/simple.rs b/src/simple.rs index 38fb3b9..23b02c3 100644 --- a/src/simple.rs +++ b/src/simple.rs @@ -1,5 +1,5 @@ use { - crate::{Error, PreRelease, MAX_U12}, + crate::{Error, Extended, GitRev, PreRelease, Rapid, SemVer, MAX_U12}, serde::{Deserialize, Serialize}, std::{cmp::Ordering, fmt, str::FromStr}, }; @@ -81,3 +81,61 @@ impl PartialOrd for Simple { Some(self.cmp(other)) } } + +impl PartialEq for Simple { + fn eq(&self, other: &Extended) -> bool { + self.major == other.major + && other.minor == 0 + && other.patch == 0 + && other.build == 0 + && self.pre == other.pre + } +} + +impl PartialEq for Simple { + fn eq(&self, other: &SemVer) -> bool { + self.major == other.major && other.minor == 0 && other.patch == 0 && self.pre == other.pre + } +} + +impl PartialEq for Simple { + fn eq(&self, other: &Rapid) -> bool { + self.major == other.major && other.minor == 0 && self.pre == other.pre + } +} + +impl PartialEq for Simple { + fn eq(&self, _other: &GitRev) -> bool { + false + } +} + +impl PartialOrd for Simple { + fn partial_cmp(&self, other: &Extended) -> Option { + let a = u64::from(*self); + let b = u64::from(*other); + Some(a.cmp(&b)) + } +} + +impl PartialOrd for Simple { + fn partial_cmp(&self, other: &SemVer) -> Option { + let a = u64::from(*self); + let b = u64::from(*other); + Some(a.cmp(&b)) + } +} + +impl PartialOrd for Simple { + fn partial_cmp(&self, other: &Rapid) -> Option { + let a = u64::from(*self); + let b = u64::from(*other); + Some(a.cmp(&b)) + } +} + +impl PartialOrd for Simple { + fn partial_cmp(&self, _other: &GitRev) -> Option { + None + } +}