From 7fb600005e7f233970a613a7babcd7cca32da9e3 Mon Sep 17 00:00:00 2001 From: Ruben Pollan Date: Sun, 29 Apr 2018 04:29:09 +0200 Subject: Sort the events --- src/ics.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/ics.rs') 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>(ics: P) -> Result, 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(date: &chrono::Date, time: &DateTime) -> Ordering { + let d2 = time.date(); + if date.eq(&d2) { + return Ordering::Less; + } + date.cmp(&d2) +} + #[derive(Debug)] pub enum IcsError { IoError(io::Error), -- cgit v1.2.3