impl PartialEq and PartialOrd for all types

This commit is contained in:
Nathan Fisher 2024-01-08 19:02:19 -05:00
parent 298185d8d4
commit 748a786632
6 changed files with 331 additions and 9 deletions

View File

@ -1,3 +1,5 @@
use crate::GitRev;
use { use {
crate::{Error, PreRelease, Rapid, SemVer, Simple, MAX_U12}, crate::{Error, PreRelease, Rapid, SemVer, Simple, MAX_U12},
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
@ -186,6 +188,12 @@ impl PartialEq<Simple> for Extended {
} }
} }
impl PartialEq<GitRev> for Extended {
fn eq(&self, _other: &GitRev) -> bool {
false
}
}
impl PartialOrd<SemVer> for Extended { impl PartialOrd<SemVer> for Extended {
fn partial_cmp(&self, other: &SemVer) -> Option<Ordering> { fn partial_cmp(&self, other: &SemVer) -> Option<Ordering> {
Some(u64::from(*self).cmp(&u64::from(*other))) Some(u64::from(*self).cmp(&u64::from(*other)))
@ -203,3 +211,9 @@ impl PartialOrd<Simple> for Extended {
Some(u64::from(*self).cmp(&u64::from(*other))) Some(u64::from(*self).cmp(&u64::from(*other)))
} }
} }
impl PartialOrd<GitRev> for Extended {
fn partial_cmp(&self, _other: &GitRev) -> Option<Ordering> {
None
}
}

View File

@ -1,5 +1,5 @@
use { use {
crate::Error, crate::{Error, Extended, Rapid, SemVer, Simple, Version},
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},
@ -50,17 +50,65 @@ impl FromStr for GitRev {
Err(Error::ParseGitRev) Err(Error::ParseGitRev)
} }
} }
/*
impl PartialEq<Extended> for GitRev {
fn eq(&self, _other: &Extended) -> bool {
false
}
}
impl PartialOrd<Extended> for GitRev {
fn partial_cmp(&self, _other: &Extended) -> Option<Ordering> {
None
}
}
impl PartialEq<SemVer> for GitRev {
fn eq(&self, _other: &SemVer) -> bool {
false
}
}
impl PartialOrd<SemVer> for GitRev {
fn partial_cmp(&self, _other: &SemVer) -> Option<Ordering> {
None
}
}
impl PartialEq<Rapid> for GitRev {
fn eq(&self, _other: &Rapid) -> bool {
false
}
}
impl PartialOrd<Rapid> for GitRev {
fn partial_cmp(&self, _other: &Rapid) -> Option<Ordering> {
None
}
}
impl PartialEq<Simple> for GitRev {
fn eq(&self, _other: &Simple) -> bool {
false
}
}
impl PartialOrd<Simple> for GitRev {
fn partial_cmp(&self, _other: &Simple) -> Option<Ordering> {
None
}
}
impl TryFrom<Version> for GitRev { impl TryFrom<Version> for GitRev {
type Error = ParseGitRevError; type Error = Error;
fn try_from(value: Version) -> Result<Self, Self::Error> { fn try_from(value: Version) -> Result<Self, Self::Error> {
match value { match value {
Version::Git(g) => Ok(g), Version::GitRev(g) => Ok(g),
_ => Err(ParseGitRevError), _ => Err(Error::ParseGitRev),
} }
} }
}*/ }
#[cfg(test)] #[cfg(test)]
mod test { mod test {

View File

@ -21,3 +21,83 @@ pub enum Version {
Extended(Extended), Extended(Extended),
GitRev(GitRev), GitRev(GitRev),
} }
impl From<Simple> for Version {
fn from(value: Simple) -> Self {
Self::Simple(value)
}
}
impl From<Rapid> for Version {
fn from(value: Rapid) -> Self {
Self::Rapid(value)
}
}
impl From<SemVer> for Version {
fn from(value: SemVer) -> Self {
Self::SemVer(value)
}
}
impl From<Extended> for Version {
fn from(value: Extended) -> Self {
Self::Extended(value)
}
}
impl From<GitRev> 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<std::cmp::Ordering> {
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,
}
}
}

View File

@ -1,5 +1,5 @@
use { use {
crate::{Error, PreRelease, MAX_U12}, crate::{Error, Extended, GitRev, PreRelease, SemVer, Simple, MAX_U12},
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
std::{cmp::Ordering, fmt, str::FromStr}, std::{cmp::Ordering, fmt, str::FromStr},
}; };
@ -93,3 +93,64 @@ impl PartialOrd for Rapid {
Some(self.cmp(other)) Some(self.cmp(other))
} }
} }
impl PartialEq<Extended> 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<SemVer> 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<Simple> for Rapid {
fn eq(&self, other: &Simple) -> bool {
self.major == other.major && self.minor == 0 && self.pre == other.pre
}
}
impl PartialEq<GitRev> for Rapid {
fn eq(&self, _other: &GitRev) -> bool {
false
}
}
impl PartialOrd<Extended> for Rapid {
fn partial_cmp(&self, other: &Extended) -> Option<Ordering> {
let a = u64::from(*self);
let b = u64::from(*other);
a.partial_cmp(&b)
}
}
impl PartialOrd<SemVer> for Rapid {
fn partial_cmp(&self, other: &SemVer) -> Option<Ordering> {
let a = u64::from(*self);
let b = u64::from(*other);
a.partial_cmp(&b)
}
}
impl PartialOrd<Simple> for Rapid {
fn partial_cmp(&self, other: &Simple) -> Option<Ordering> {
let a = u64::from(*self);
let b = u64::from(*other);
a.partial_cmp(&b)
}
}
impl PartialOrd<GitRev> for Rapid {
fn partial_cmp(&self, _other: &GitRev) -> Option<Ordering> {
None
}
}

View File

@ -1,5 +1,5 @@
use { use {
crate::{Error, PreRelease, MAX_U12}, crate::{Error, Extended, GitRev, PreRelease, Rapid, Simple, MAX_U12},
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
std::{cmp::Ordering, fmt, str::FromStr}, std::{cmp::Ordering, fmt, str::FromStr},
}; };
@ -108,6 +108,67 @@ impl PartialOrd for SemVer {
} }
} }
impl PartialEq<Extended> 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<Rapid> 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<Simple> for SemVer {
fn eq(&self, other: &Simple) -> bool {
self.major == other.major && self.minor == 0 && self.patch == 0 && self.pre == other.pre
}
}
impl PartialEq<GitRev> for SemVer {
fn eq(&self, _other: &GitRev) -> bool {
false
}
}
impl PartialOrd<Extended> for SemVer {
fn partial_cmp(&self, other: &Extended) -> Option<Ordering> {
let a = u64::from(*self);
let b = u64::from(*other);
Some(a.cmp(&b))
}
}
impl PartialOrd<Rapid> for SemVer {
fn partial_cmp(&self, other: &Rapid) -> Option<Ordering> {
let a = u64::from(*self);
let b = u64::from(*other);
Some(a.cmp(&b))
}
}
impl PartialOrd<Simple> for SemVer {
fn partial_cmp(&self, other: &Simple) -> Option<Ordering> {
let a = u64::from(*self);
let b = u64::from(*other);
Some(a.cmp(&b))
}
}
impl PartialOrd<GitRev> for SemVer {
fn partial_cmp(&self, _other: &GitRev) -> Option<Ordering> {
None
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::num::NonZeroU16; use std::num::NonZeroU16;

View File

@ -1,5 +1,5 @@
use { use {
crate::{Error, PreRelease, MAX_U12}, crate::{Error, Extended, GitRev, PreRelease, Rapid, SemVer, MAX_U12},
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
std::{cmp::Ordering, fmt, str::FromStr}, std::{cmp::Ordering, fmt, str::FromStr},
}; };
@ -81,3 +81,61 @@ impl PartialOrd for Simple {
Some(self.cmp(other)) Some(self.cmp(other))
} }
} }
impl PartialEq<Extended> 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<SemVer> for Simple {
fn eq(&self, other: &SemVer) -> bool {
self.major == other.major && other.minor == 0 && other.patch == 0 && self.pre == other.pre
}
}
impl PartialEq<Rapid> for Simple {
fn eq(&self, other: &Rapid) -> bool {
self.major == other.major && other.minor == 0 && self.pre == other.pre
}
}
impl PartialEq<GitRev> for Simple {
fn eq(&self, _other: &GitRev) -> bool {
false
}
}
impl PartialOrd<Extended> for Simple {
fn partial_cmp(&self, other: &Extended) -> Option<Ordering> {
let a = u64::from(*self);
let b = u64::from(*other);
Some(a.cmp(&b))
}
}
impl PartialOrd<SemVer> for Simple {
fn partial_cmp(&self, other: &SemVer) -> Option<Ordering> {
let a = u64::from(*self);
let b = u64::from(*other);
Some(a.cmp(&b))
}
}
impl PartialOrd<Rapid> for Simple {
fn partial_cmp(&self, other: &Rapid) -> Option<Ordering> {
let a = u64::from(*self);
let b = u64::from(*other);
Some(a.cmp(&b))
}
}
impl PartialOrd<GitRev> for Simple {
fn partial_cmp(&self, _other: &GitRev) -> Option<Ordering> {
None
}
}