aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Pollan2018-12-10 02:45:32 +0100
committerRuben Pollan2018-12-10 02:45:32 +0100
commit3097964b7bc8a96df8c619e3006a745db870175e (patch)
treebaa7409f79d075117c364722e34e6cc8799ec8f0
parent5f09796938cbdf11a99fa023aa8baeb76571b967 (diff)
Use iterators and itertools to produce events
-rw-r--r--Cargo.lock16
-rw-r--r--Cargo.toml1
-rw-r--r--src/calendar.rs20
-rw-r--r--src/event.rs14
-rw-r--r--src/lib.rs1
-rw-r--r--src/main.rs22
6 files changed, 38 insertions, 36 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 00cfe7d..6335f17 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -13,6 +13,7 @@ dependencies = [
"chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono-tz 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ical 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -66,6 +67,11 @@ dependencies = [
]
[[package]]
+name = "either"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
name = "error-chain"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -82,6 +88,14 @@ dependencies = [
]
[[package]]
+name = "itertools"
+version = "0.7.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "lazy_static"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -212,8 +226,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
"checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878"
"checksum chrono-tz 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44420a821d3075c6b4dcdba557104274a240b5b6e323dc17136507e96ca2db59"
+"checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0"
"checksum error-chain 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9435d864e017c3c6afeac1654189b06cdb491cf2ff73dbf0d73b0f292f42ff8"
"checksum ical 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a985f921bf84e6d00286f0f41363e2d39978370d8d8868c114002d863c27094f"
+"checksum itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0d47946d458e94a1b7bcabbf6521ea7c037062c81f534615abcad76e84d4970d"
"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1"
"checksum libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)" = "10923947f84a519a45c8fefb7dd1b3e8c08747993381adee176d7a82b4195311"
"checksum memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a3eb002f0535929f1199681417029ebea04aadc0c7a4224b46be99c7f5d6a16"
diff --git a/Cargo.toml b/Cargo.toml
index 5bae8e6..6f5bdb3 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,6 +6,7 @@ authors = ["meskio <meskio@sindominio.net>"]
[dependencies]
chrono = "0.4"
chrono-tz = "0.5"
+itertools = "0.7"
[dependencies.ical]
version = "0.5.*"
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]
- }
- }
-}
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<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()
}