From 3097964b7bc8a96df8c619e3006a745db870175e Mon Sep 17 00:00:00 2001 From: Ruben Pollan Date: Mon, 10 Dec 2018 02:45:32 +0100 Subject: Use iterators and itertools to produce events --- src/calendar.rs | 20 ++++++++------------ src/event.rs | 14 -------------- src/lib.rs | 1 + src/main.rs | 22 ++++++++++++---------- 4 files changed, 21 insertions(+), 36 deletions(-) (limited to 'src') 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 { - 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 + '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, 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] - } - } -} diff --git a/src/lib.rs b/src/lib.rs index e754a11..b82b190 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { +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() } -- cgit v1.2.3