diff options
author | Ruben Pollan | 2018-04-29 04:29:09 +0200 |
---|---|---|
committer | Ruben Pollan | 2018-04-29 04:29:09 +0200 |
commit | 7fb600005e7f233970a613a7babcd7cca32da9e3 (patch) | |
tree | 82be5c1b4da4ffd8fbb2eea60c8736c19810e106 /src | |
parent | ba5f37d3bcc7a6cbf09581d474c26f3c69c60130 (diff) |
Sort the events
Diffstat (limited to 'src')
-rw-r--r-- | src/ics.rs | 29 |
1 files changed, 29 insertions, 0 deletions
@@ -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), |