aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Pollan2018-11-27 10:33:01 -0600
committerRuben Pollan2018-11-27 10:33:01 -0600
commit87544cc523491e9618dc7e093349fc6501fdf46b (patch)
treeb59970cba5d25e0a633eba08b18abf7ab203e71a
parent6a14d38bceed38b3a7ccc51f81b6cd845548e6b8 (diff)
Implement Ord for events
-rw-r--r--src/calendar.rs2
-rw-r--r--src/event.rs18
2 files changed, 16 insertions, 4 deletions
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;