From e8aead53650c2e2007fa21306bbf3ddb8928c365 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Wed, 29 Mar 2023 00:18:49 -0400 Subject: [PATCH] Implement PartialEq and PartialOrd between SemVer and Rapid versioning --- src/version/mod.rs | 43 +++++++++++++++++++++---------------------- src/version/rapid.rs | 5 +---- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/version/mod.rs b/src/version/mod.rs index 0911f1e..d37217b 100644 --- a/src/version/mod.rs +++ b/src/version/mod.rs @@ -1,9 +1,7 @@ -use std::cmp::Ordering; - use { chrono::{offset::Utc, DateTime}, serde::{Deserialize, Serialize}, - std::{error::Error, fmt, str::FromStr}, + std::{cmp::Ordering, error::Error, fmt, str::FromStr}, }; mod gitrev; @@ -86,7 +84,10 @@ impl From for Version { impl From for Version { fn from(value: Rapid) -> Self { - Self::Rapid { major: value.major, minor: value.minor } + Self::Rapid { + major: value.major, + minor: value.minor, + } } } @@ -111,27 +112,20 @@ impl PartialOrd for Version { match (self, other) { (Self::Number(s), Self::Number(o)) => s.partial_cmp(o), ( + Self::SemVer { + major, + minor, + patch, + }, Self::SemVer { major: a, minor: b, patch: c, }, - Self::SemVer { - major: d, - minor: e, - patch: 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)), + ) => (major, minor, patch).partial_cmp(&(a, b, c)), + (Self::Rapid { major, minor }, Self::Rapid { major: a, minor: b }) => { + (major, minor).partial_cmp(&(a, b)) + } ( Self::Git { hash: _a, @@ -142,6 +136,11 @@ impl PartialOrd for Version { datetime: 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, } } @@ -195,8 +194,8 @@ impl PartialOrd for SemVer { Some(Ordering::Equal) => match self.patch { 0 => Some(Ordering::Equal), _ => Some(Ordering::Greater), - } - } + }, + }, } } } diff --git a/src/version/rapid.rs b/src/version/rapid.rs index 25e2dae..53a667e 100644 --- a/src/version/rapid.rs +++ b/src/version/rapid.rs @@ -62,10 +62,7 @@ impl FromStr for Rapid { 2 => { let major = split.first().unwrap().parse::()?; let minor = split.get(1).unwrap().parse::()?; - Ok(Self { - major, - minor, - }) + Ok(Self { major, minor }) } _ => Err(ParseRapidError), }