Finish conversion from u64 to SemVer and add test

This commit is contained in:
Nathan Fisher 2024-01-07 09:55:41 -05:00
parent dd243ab155
commit 31d3bc960b

View File

@ -65,7 +65,21 @@ impl TryFrom<u64> for SemVer {
type Error = Error; type Error = Error;
fn try_from(value: u64) -> Result<Self, Self::Error> { fn try_from(value: u64) -> Result<Self, Self::Error> {
todo!() let mut mask: u64 = 0o7777 << 52;
let major = (value & mask) >> 52;
mask = 0o7777 << 40;
let minor = (value & mask) >> 40;
mask = 0o7777 << 28;
let patch = (value & mask) >> 28;
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()?,
patch: patch.try_into()?,
pre,
})
} }
} }
@ -142,6 +156,17 @@ mod tests {
assert_eq!(s.to_string(), "2.1.14_beta2"); assert_eq!(s.to_string(), "2.1.14_beta2");
} }
#[test]
fn to_from_u64() {
let sem = SemVer {
major: 1,
minor: 0,
patch: 11,
pre: PreRelease::Beta(Some(NonZeroU16::new(2).unwrap())),
};
assert_eq!(SemVer::try_from(u64::from(sem)).unwrap(), sem);
}
#[test] #[test]
fn eq() { fn eq() {
assert_eq!( assert_eq!(