Merge branch 'odin' of git.hitchhiker-linux.org:jeang3nie/hpk into odin

This commit is contained in:
Nathan Fisher 2023-03-29 23:33:28 -04:00
commit 94eb94952f
3 changed files with 162 additions and 20 deletions

View File

@ -1,21 +1,26 @@
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;
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 +54,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 +82,15 @@ 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 {
@ -91,27 +112,35 @@ 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::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),
(
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,
} }
} }
@ -134,6 +163,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 +174,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),
},
},
}
}
}

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

@ -0,0 +1,86 @@
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,