From b80fb44d83f9460f9d1ab4c831e7c6a9baf44c16 Mon Sep 17 00:00:00 2001 From: Ruben Pollan Date: Sat, 25 Aug 2018 19:09:30 +0200 Subject: Basic periodic events support --- src/events.rs | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'src/events.rs') diff --git a/src/events.rs b/src/events.rs index a78a286..9cda846 100644 --- a/src/events.rs +++ b/src/events.rs @@ -3,25 +3,31 @@ use std::fmt; use ical::IcalParser; use event::{Event, Date}; +use periodic::Periodic; use errors::EventError; pub struct Events { single: Vec, + periodic: Vec, } impl Events { pub fn parse(buf: B) -> Result { let reader = IcalParser::new(buf); let mut single = Vec::new(); + let mut periodic = Vec::new(); for line in reader { for ev in line?.events { let mut event = Event::new(); + let mut maybe_periodic = None; + for property in ev.properties { let value = property.value.unwrap_or("".to_string()); let mut time_zone = "".to_string(); - for (param, value) in property.params.unwrap_or(vec![]) { + let params = property.params.unwrap_or(vec![]); + for (param, value) in ¶ms { if param == "TZID" && value.len() > 0 { time_zone = value[0].clone(); } @@ -34,15 +40,22 @@ impl Events { "STATUS" => event.status = value.parse()?, "DTSTART" => event.start = Date::parse(&value, &time_zone)?, "DTEND" => event.end = Date::parse(&value, &time_zone)?, + "RRULE" => maybe_periodic = Some(rrule(&value, ¶ms)?), _ => (), }; } - single.push(event); + match maybe_periodic { + Some(mut p) => { + p.event = event; + periodic.push(p); + } + None => single.push(event), + } } } single.sort_by(|a, b| a.start.cmp(&b.start)); - Ok(Events { single }) + Ok(Events { single, periodic }) } } @@ -51,6 +64,27 @@ impl fmt::Display for Events { for event in &self.single { writeln!(f, "{}", event)?; } + writeln!(f, "")?; + for periodic in &self.periodic { + writeln!(f, "{}", periodic)?; + } Ok(()) } } + +fn rrule(value: &String, params: &Vec<(String, Vec)>) -> Result { + let mut periodic = Periodic::new(); + + let p: Vec<&str> = value.splitn(2, "=").collect(); + periodic.set_param(p[0], p[1])?; + + for (param, values) in params { + let mut value = ""; + if values.len() > 0 { + value = &values[0]; + } + periodic.set_param(param, value)?; + } + + Ok(periodic) +} -- cgit v1.2.3