aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRuben Pollan2018-04-29 04:29:09 +0200
committerRuben Pollan2018-04-29 04:29:09 +0200
commit7fb600005e7f233970a613a7babcd7cca32da9e3 (patch)
tree82be5c1b4da4ffd8fbb2eea60c8736c19810e106 /src
parentba5f37d3bcc7a6cbf09581d474c26f3c69c60130 (diff)
Sort the events
Diffstat (limited to 'src')
-rw-r--r--src/ics.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/ics.rs b/src/ics.rs
index 3373d14..1ec9417 100644
--- a/src/ics.rs
+++ b/src/ics.rs
@@ -5,6 +5,7 @@ use std::path::Path;
use std::str::FromStr;
use std::num::ParseIntError;
use std::fmt;
+use std::cmp::Ordering;
use ical::parser;
use ical::IcalParser;
@@ -59,6 +60,7 @@ pub fn parse<P: AsRef<Path>>(ics: P) -> Result<Vec<Event>, IcsError> {
}
}
+ events.sort_by(|a, b| a.start.cmp(&b.start));
Ok(events)
}
@@ -110,6 +112,33 @@ impl fmt::Display for Event {
}
}
+impl Date {
+ fn cmp(&self, other: &Date) -> Ordering {
+ match *self {
+ Date::Time(t1) => {
+ match *other {
+ Date::Time(t2) => t1.cmp(&t2),
+ Date::AllDay(d) => cmp_date_time(&d, &t1).reverse(),
+ }
+ }
+ Date::AllDay(d1) => {
+ match *other {
+ Date::Time(t) => cmp_date_time(&d1, &t),
+ Date::AllDay(d2) => d1.cmp(&d2),
+ }
+ }
+ }
+ }
+}
+
+fn cmp_date_time<T: TimeZone>(date: &chrono::Date<T>, time: &DateTime<T>) -> Ordering {
+ let d2 = time.date();
+ if date.eq(&d2) {
+ return Ordering::Less;
+ }
+ date.cmp(&d2)
+}
+
#[derive(Debug)]
pub enum IcsError {
IoError(io::Error),