160 lines
4.0 KiB
Rust
160 lines
4.0 KiB
Rust
use {
|
|
crate::{
|
|
error::Error, extended::Extended, gitrev::GitRev, prerelease::PreRelease, semver::SemVer,
|
|
simple::Simple, MAX_U12,
|
|
},
|
|
serde::{Deserialize, Serialize},
|
|
std::{cmp::Ordering, fmt, str::FromStr},
|
|
};
|
|
|
|
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
|
|
pub struct Rapid {
|
|
pub major: u16,
|
|
pub minor: u16,
|
|
pub pre: PreRelease,
|
|
}
|
|
|
|
impl fmt::Display for Rapid {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "{}.{}", self.major, self.minor)?;
|
|
match self.pre {
|
|
PreRelease::None => Ok(()),
|
|
p => write!(f, "_{p}"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl FromStr for Rapid {
|
|
type Err = Error;
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
let (s, pre) = match s.split_once('_') {
|
|
Some((a, b)) => (a, b.parse::<PreRelease>()?),
|
|
None => (s, PreRelease::None),
|
|
};
|
|
let split = s.split('.').collect::<Vec<_>>();
|
|
match split.len() {
|
|
2 => {
|
|
let major = split.first().unwrap().parse::<u16>()?;
|
|
let minor = split.get(1).unwrap().parse::<u16>()?;
|
|
if major > MAX_U12 || minor > MAX_U12 {
|
|
return Err(Error::Range);
|
|
}
|
|
Ok(Self { major, minor, pre })
|
|
}
|
|
_ => Err(Error::ParseSemver),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<Rapid> for u64 {
|
|
fn from(value: Rapid) -> Self {
|
|
let major = u64::from(value.major) << 52;
|
|
let minor = u64::from(value.minor) << 40;
|
|
let pre = u64::from(u16::from(value.pre));
|
|
major | minor | pre
|
|
}
|
|
}
|
|
|
|
impl TryFrom<u64> for Rapid {
|
|
type Error = Error;
|
|
|
|
fn try_from(value: u64) -> Result<Self, Self::Error> {
|
|
let mut mask: u64 = 0o7777 << 52;
|
|
let major = (value & mask) >> 52;
|
|
mask = 0o7777 << 40;
|
|
let minor = (value & mask) >> 40;
|
|
mask = 0o37777;
|
|
let p = u16::try_from(value & mask)?;
|
|
let pre: PreRelease = p.try_into()?;
|
|
Ok(Self {
|
|
major: major.try_into()?,
|
|
minor: minor.try_into()?,
|
|
pre,
|
|
})
|
|
}
|
|
}
|
|
|
|
impl PartialEq for Rapid {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
self.major == other.major && self.minor == other.minor && self.pre == other.pre
|
|
}
|
|
}
|
|
|
|
impl Eq for Rapid {}
|
|
|
|
impl Ord for Rapid {
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
let a = u64::from(*self);
|
|
let b = u64::from(*other);
|
|
a.cmp(&b)
|
|
}
|
|
}
|
|
|
|
impl PartialOrd for Rapid {
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
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
|
|
}
|
|
}
|