Implement PartialEq and PartialOrd between SemVer and Rapid versioning

This commit is contained in:
Nathan Fisher 2023-03-29 00:18:49 -04:00
parent 1acf6f4051
commit e8aead5365
2 changed files with 22 additions and 26 deletions

View File

@ -1,9 +1,7 @@
use std::cmp::Ordering;
use { use {
chrono::{offset::Utc, DateTime}, chrono::{offset::Utc, DateTime},
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
std::{error::Error, fmt, str::FromStr}, std::{cmp::Ordering, error::Error, fmt, str::FromStr},
}; };
mod gitrev; mod gitrev;
@ -86,7 +84,10 @@ impl From<SemVer> for Version {
impl From<Rapid> for Version { impl From<Rapid> for Version {
fn from(value: Rapid) -> Self { fn from(value: Rapid) -> Self {
Self::Rapid { major: value.major, minor: value.minor } Self::Rapid {
major: value.major,
minor: value.minor,
}
} }
} }
@ -111,27 +112,20 @@ impl PartialOrd for Version {
match (self, other) { match (self, other) {
(Self::Number(s), Self::Number(o)) => s.partial_cmp(o), (Self::Number(s), Self::Number(o)) => s.partial_cmp(o),
( (
Self::SemVer {
major,
minor,
patch,
},
Self::SemVer { Self::SemVer {
major: a, major: a,
minor: b, minor: b,
patch: c, patch: c,
}, },
Self::SemVer { ) => (major, minor, patch).partial_cmp(&(a, b, c)),
major: d, (Self::Rapid { major, minor }, Self::Rapid { major: a, minor: b }) => {
minor: e, (major, minor).partial_cmp(&(a, b))
patch: f, }
},
) => (a, b, c).partial_cmp(&(d, e, f)),
(
Self::Rapid {
major: a,
minor: b,
},
Self::Rapid {
major: c,
minor: d,
},
) => (a, b).partial_cmp(&(c, d)),
( (
Self::Git { Self::Git {
hash: _a, hash: _a,
@ -142,6 +136,11 @@ impl PartialOrd for Version {
datetime: d, datetime: d,
}, },
) => b.partial_cmp(&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, _ => None,
} }
} }
@ -195,8 +194,8 @@ impl PartialOrd<Rapid> for SemVer {
Some(Ordering::Equal) => match self.patch { Some(Ordering::Equal) => match self.patch {
0 => Some(Ordering::Equal), 0 => Some(Ordering::Equal),
_ => Some(Ordering::Greater), _ => Some(Ordering::Greater),
} },
} },
} }
} }
} }

View File

@ -62,10 +62,7 @@ impl FromStr for Rapid {
2 => { 2 => {
let major = split.first().unwrap().parse::<u32>()?; let major = split.first().unwrap().parse::<u32>()?;
let minor = split.get(1).unwrap().parse::<u32>()?; let minor = split.get(1).unwrap().parse::<u32>()?;
Ok(Self { Ok(Self { major, minor })
major,
minor,
})
} }
_ => Err(ParseRapidError), _ => Err(ParseRapidError),
} }