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/event.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/event.rs') 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