From 87544cc523491e9618dc7e093349fc6501fdf46b Mon Sep 17 00:00:00 2001 From: Ruben Pollan Date: Tue, 27 Nov 2018 10:33:01 -0600 Subject: Implement Ord for events --- src/calendar.rs | 2 +- src/event.rs | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/calendar.rs b/src/calendar.rs index a835480..6df2f74 100644 --- a/src/calendar.rs +++ b/src/calendar.rs @@ -71,7 +71,7 @@ impl Calendar { .collect(); events.append(&mut p_events); } - events.sort_by_key(|k| k.start); + events.sort(); events } } diff --git a/src/event.rs b/src/event.rs index db32b22..b5e9d27 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,3 +1,4 @@ +use std::cmp::{Ordering, Ord}; use std::fmt; use std::str::FromStr; @@ -7,7 +8,7 @@ use date::Date; use errors::EventError; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq)] pub struct Event { pub start: Date, pub end: End, @@ -17,14 +18,14 @@ pub struct Event { pub status: Status, } -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq)] pub enum Status { Confirmed, Tentative, Canceled, } -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq)] pub enum End { Date(Date), Duration(Duration), @@ -70,6 +71,17 @@ impl fmt::Display for Event { } } +impl Ord for Event { + fn cmp(&self, other: &Self) -> Ordering { + let ord = self.start.cmp(&other.start); + if ord == Ordering::Equal { + self.end_date().cmp(&other.end_date()) + } else { + ord + } + } +} + impl FromStr for Status { type Err = EventError; -- cgit v1.2.3