Add Rapid versioning (ala Gnome, Firefox, Chrome) with single decimal

This commit is contained in:
Nathan Fisher 2023-03-28 23:58:18 -04:00
parent 0a94691685
commit 1acf6f4051
3 changed files with 159 additions and 13 deletions

View File

@ -1,3 +1,5 @@
use std::cmp::Ordering;
use { use {
chrono::{offset::Utc, DateTime}, chrono::{offset::Utc, DateTime},
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
@ -5,17 +7,22 @@ use {
}; };
mod gitrev; mod gitrev;
mod rapid;
mod semver; mod semver;
pub use {gitrev::GitRev, semver::SemVer}; pub use {gitrev::GitRev, rapid::Rapid, semver::SemVer};
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub enum Version { pub enum Version {
Number(u32), Number(u32),
Rapid {
major: u32,
minor: u32,
},
SemVer { SemVer {
major: u8, major: u32,
minor: u8, minor: u32,
patch: u8, patch: u32,
}, },
Git { Git {
hash: String, hash: String,
@ -49,6 +56,13 @@ impl fmt::Display for Version {
}; };
write!(f, "{v}") write!(f, "{v}")
} }
Self::Rapid { major, minor } => {
let v = Rapid {
major: *major,
minor: *minor,
};
write!(f, "{v}")
}
Self::Git { hash, datetime } => { Self::Git { hash, datetime } => {
let v = GitRev { let v = GitRev {
hash: hash.clone(), hash: hash.clone(),
@ -70,6 +84,12 @@ impl From<SemVer> for Version {
} }
} }
impl From<Rapid> for Version {
fn from(value: Rapid) -> Self {
Self::Rapid { major: value.major, minor: value.minor }
}
}
impl From<GitRev> for Version { impl From<GitRev> for Version {
fn from(value: GitRev) -> Self { fn from(value: GitRev) -> Self {
Self::Git { Self::Git {
@ -102,16 +122,26 @@ impl PartialOrd for Version {
patch: f, patch: f,
}, },
) => (a, b, c).partial_cmp(&(d, e, 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,
datetime: b, datetime: b,
}, },
Self::Git { Self::Git {
hash: c, hash: _c,
datetime: d, datetime: d,
}, },
) => (a, b).partial_cmp(&(c, d)), ) => b.partial_cmp(&d),
_ => None, _ => None,
} }
} }
@ -134,6 +164,8 @@ impl FromStr for Version {
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Ok(v) = s.parse::<SemVer>() { if let Ok(v) = s.parse::<SemVer>() {
Ok(v.into()) Ok(v.into())
} else if let Ok(v) = s.parse::<Rapid>() {
Ok(v.into())
} else if let Ok(v) = s.parse::<GitRev>() { } else if let Ok(v) = s.parse::<GitRev>() {
Ok(v.into()) Ok(v.into())
} else if let Ok(v) = s.parse::<u32>() { } else if let Ok(v) = s.parse::<u32>() {
@ -143,3 +175,28 @@ impl FromStr for Version {
} }
} }
} }
impl PartialEq<Rapid> for SemVer {
fn eq(&self, other: &Rapid) -> bool {
self.major == other.major && self.minor == other.minor && self.patch == 0
}
}
impl PartialOrd<Rapid> for SemVer {
fn partial_cmp(&self, other: &Rapid) -> Option<std::cmp::Ordering> {
match self.major.partial_cmp(&other.major) {
Some(Ordering::Greater) => Some(Ordering::Greater),
Some(Ordering::Less) => Some(Ordering::Less),
None => None,
Some(Ordering::Equal) => match self.minor.partial_cmp(&other.minor) {
Some(Ordering::Greater) => Some(Ordering::Greater),
Some(Ordering::Less) => Some(Ordering::Less),
None => None,
Some(Ordering::Equal) => match self.patch {
0 => Some(Ordering::Equal),
_ => Some(Ordering::Greater),
}
}
}
}
}

89
src/version/rapid.rs Normal file
View File

@ -0,0 +1,89 @@
use {
serde::{Deserialize, Serialize},
std::{cmp::Ordering, error::Error, fmt, num::ParseIntError, str::FromStr},
};
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Rapid {
pub major: u32,
pub minor: u32,
}
impl fmt::Display for Rapid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}.{}", self.major, self.minor)
}
}
impl PartialOrd for Rapid {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match self.major.partial_cmp(&other.major) {
Some(Ordering::Greater) => Some(Ordering::Greater),
Some(Ordering::Less) => Some(Ordering::Less),
Some(Ordering::Equal) => self.minor.partial_cmp(&other.minor),
None => None,
}
}
}
impl Ord for Rapid {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.major.cmp(&other.major) {
Ordering::Greater => Ordering::Greater,
Ordering::Less => Ordering::Less,
Ordering::Equal => self.minor.cmp(&other.minor),
}
}
}
#[derive(Debug, PartialEq)]
pub struct ParseRapidError;
impl fmt::Display for ParseRapidError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Error parsing Rapid version")
}
}
impl Error for ParseRapidError {}
impl From<ParseIntError> for ParseRapidError {
fn from(_value: ParseIntError) -> Self {
Self
}
}
impl FromStr for Rapid {
type Err = ParseRapidError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let split = s.split('.').collect::<Vec<_>>();
match split.len() {
2 => {
let major = split.first().unwrap().parse::<u32>()?;
let minor = split.get(1).unwrap().parse::<u32>()?;
Ok(Self {
major,
minor,
})
}
_ => Err(ParseRapidError),
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn parse_semver() {
assert_eq!(
"93.0".parse::<Rapid>(),
Ok(Rapid {
major: 93,
minor: 0,
})
);
}
}

View File

@ -5,9 +5,9 @@ use {
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct SemVer { pub struct SemVer {
pub major: u8, pub major: u32,
pub minor: u8, pub minor: u32,
pub patch: u8, pub patch: u32,
} }
impl fmt::Display for SemVer { impl fmt::Display for SemVer {
@ -70,9 +70,9 @@ impl FromStr for SemVer {
let split = s.split('.').collect::<Vec<_>>(); let split = s.split('.').collect::<Vec<_>>();
match split.len() { match split.len() {
3 => { 3 => {
let major = split.first().unwrap().parse::<u8>()?; let major = split.first().unwrap().parse::<u32>()?;
let minor = split.get(1).unwrap().parse::<u8>()?; let minor = split.get(1).unwrap().parse::<u32>()?;
let patch = split.get(2).unwrap().parse::<u8>()?; let patch = split.get(2).unwrap().parse::<u32>()?;
Ok(Self { Ok(Self {
major, major,
minor, minor,