diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/calendar.rs | 20 | ||||
-rw-r--r-- | src/event.rs | 14 | ||||
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/main.rs | 22 |
4 files changed, 21 insertions, 36 deletions
diff --git a/src/calendar.rs b/src/calendar.rs index 6df2f74..fcec54e 100644 --- a/src/calendar.rs +++ b/src/calendar.rs @@ -2,9 +2,9 @@ use std::io::BufRead; use std::fmt; use ical::IcalParser; use chrono::Duration; +use itertools::Itertools; use date::Date; -use event; use event::{Event, End}; use periodic::Periodic; use errors::EventError; @@ -62,17 +62,13 @@ impl Calendar { Ok(Calendar { single, periodic }) } - pub fn get(&self, first: &Date, last: &Date) -> Vec<Event> { - let mut events = event::get(&self.single, first, last).to_vec(); - for p in &self.periodic { - let mut p_events = p.iter() - .skip_while(|e| e.end_date() < *first) - .take_while(|e| e.start <= *last) - .collect(); - events.append(&mut p_events); - } - events.sort(); - events + pub fn iter<'a>(&'a self) -> impl Iterator<Item = Event> + 'a { + self.single.iter().map(Event::clone).merge( + self.periodic + .iter() + .map(|p| p.iter()) + .kmerge(), + ) } } diff --git a/src/event.rs b/src/event.rs index b5e9d27..e186cfd 100644 --- a/src/event.rs +++ b/src/event.rs @@ -94,17 +94,3 @@ impl FromStr for Status { } } } - - -pub fn get<'a>(events: &'a Vec<Event>, first: &Date, last: &Date) -> &'a [Event] { - match events.iter().position(|ref e| e.start >= *first) { - None => &[], - Some(i) => { - let j = events - .iter() - .position(|ref e| e.end_date() > *last) - .unwrap_or(events.len()); - &events[i..j] - } - } -} @@ -1,6 +1,7 @@ extern crate ical; extern crate chrono; extern crate chrono_tz; +extern crate itertools; mod date; mod event; diff --git a/src/main.rs b/src/main.rs index ce2284d..d531687 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,32 +1,34 @@ extern crate almanac; +extern crate itertools; use std::env; use std::io::BufReader; use std::fs::File; +use itertools::Itertools; use almanac::Calendar; use almanac::Date; use almanac::Duration; -use almanac::Event; fn main() { let first = Date::now(); let last = first + Duration::days(7); - let mut events: Vec<_> = env::args() - .skip(1) - .map(|arg| ics_calendar(&arg, &first, &last)) - .flatten() - .collect(); - events.sort(); + let calendars: Vec<_> = env::args().skip(1).map(|arg| ics_calendar(&arg)).collect(); + let events = calendars + .iter() + .map(|c| c.iter()) + .kmerge() + .skip_while(|e| e.end_date() < first) + .take_while(|e| e.start <= last); + for event in events { println!("{}", event); } } -fn ics_calendar(file_path: &str, first: &Date, last: &Date) -> Vec<Event> { +fn ics_calendar(file_path: &str) -> Calendar { let file = File::open(file_path).unwrap(); let buf = BufReader::new(file); - let calendar = Calendar::parse(buf).unwrap(); - calendar.get(first, last) + Calendar::parse(buf).unwrap() } |