From 31d3bc960b3b0b80eaf29418125908485b3a8c55 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Sun, 7 Jan 2024 09:55:41 -0500 Subject: [PATCH] Finish conversion from u64 to SemVer and add test --- src/semver.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/semver.rs b/src/semver.rs index 9dfd282..c7df370 100644 --- a/src/semver.rs +++ b/src/semver.rs @@ -65,7 +65,21 @@ impl TryFrom for SemVer { type Error = Error; fn try_from(value: u64) -> Result { - 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"); } + #[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] fn eq() { assert_eq!(