diff options
| author | Botahamec <botahamec@outlook.com> | 2022-01-31 09:42:47 -0500 |
|---|---|---|
| committer | Botahamec <botahamec@outlook.com> | 2022-01-31 09:42:47 -0500 |
| commit | 2a057c1197d70d1d9b31a090c28dd7b929fdb6ae (patch) | |
| tree | bf8323ac8ef1ddc88853301e718615e542c35e4b /src/date.rs | |
| parent | 923e30b1bac5485075df893e8c5ef2d0346bf251 (diff) | |
Added ordering
Diffstat (limited to 'src/date.rs')
| -rw-r--r-- | src/date.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/date.rs b/src/date.rs index bc2d7dd..ff1805b 100644 --- a/src/date.rs +++ b/src/date.rs @@ -1,5 +1,7 @@ use crate::{Month, Year}; +use core::cmp::Ordering; + #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] pub struct Date { year: Year, @@ -52,4 +54,40 @@ impl Date { } } +impl PartialOrd for Date { + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { + let year_ordering = self.year.cmp(&other.year); + let month_ordering = self.month.cmp(&other.month); + let day_ordering = self.day.cmp(&other.day); + + if year_ordering != Ordering::Equal { + Some(year_ordering) + } else if month_ordering != Ordering::Equal { + Some(month_ordering) + } else if day_ordering != Ordering::Equal { + Some(day_ordering) + } else { + Some(Ordering::Equal) + } + } +} + +impl Ord for Date { + fn cmp(&self, other: &Self) -> Ordering { + let year_ordering = self.year.cmp(&other.year); + let month_ordering = self.month.cmp(&other.month); + let day_ordering = self.day.cmp(&other.day); + + if year_ordering != Ordering::Equal { + year_ordering + } else if month_ordering != Ordering::Equal { + month_ordering + } else if day_ordering != Ordering::Equal { + day_ordering + } else { + Ordering::Equal + } + } +} + // TODO addition |
