From 3b9c6d58dee043717ddb77a7006c96380bab714d Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Mon, 14 Aug 2023 12:20:33 -0700 Subject: [PATCH] Implement PartialEq for SVGMeasure --- dyesub-tool/src/svg/measure.rs | 13 +++++++++++++ dyesub-tool/src/svg/tests.rs | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/dyesub-tool/src/svg/measure.rs b/dyesub-tool/src/svg/measure.rs index 4a63dd0..7f442aa 100644 --- a/dyesub-tool/src/svg/measure.rs +++ b/dyesub-tool/src/svg/measure.rs @@ -1,4 +1,5 @@ use super::SVGUnit; +use std::cmp::Ordering; use std::fmt; use std::{ fmt::Display, @@ -40,6 +41,18 @@ impl PartialEq for SVGMeasure { } } +impl PartialOrd for SVGMeasure { + fn partial_cmp(&self, other: &Self) -> Option { + Some(if self == other { + Ordering::Equal + } else if self.to_user_units() > other.to_user_units() { + Ordering::Greater + } else { + Ordering::Less + }) + } +} + impl Display for SVGMeasure { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}{}", self.measure, self.unit) diff --git a/dyesub-tool/src/svg/tests.rs b/dyesub-tool/src/svg/tests.rs index 559e599..fd57bd3 100644 --- a/dyesub-tool/src/svg/tests.rs +++ b/dyesub-tool/src/svg/tests.rs @@ -44,3 +44,11 @@ fn ne() { let b = SVGMeasure::new(10.0, SVGUnit::Pixel); assert_ne!(a, b); } + +#[test] +fn cmp() { + let m_10cm = SVGMeasure::new(10.0, SVGUnit::Centimeter); + let m_5mm = SVGMeasure::new(5.0, SVGUnit::Millimeter); + assert!(m_10cm > m_5mm); + assert!(m_5mm < m_10cm); +}