diff --git a/libticker/src/ics/mod.rs b/libticker/src/ics/mod.rs index 5b4c052..32cdca0 100644 --- a/libticker/src/ics/mod.rs +++ b/libticker/src/ics/mod.rs @@ -1,4 +1,5 @@ -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; +use std::fmt::Write; use std::str::FromStr; use chrono::{NaiveDate, NaiveDateTime}; @@ -57,20 +58,28 @@ impl<'a> GetValue<'a, Timestamp> for Object { } } -impl std::fmt::Display for Object { - fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { - writeln!(fmt, "BEGIN:{}", self.name)?; +impl Object { + pub fn fmt_rrule(&self) -> Result { + let rrule_keys = [ + "DTSTART", + "RRULE", "RDATE", + "EXRULE", "EXDATE", + ].iter().cloned().collect::>(); + + let mut s = String::new(); for (name, values) in &self.content { + if ! rrule_keys.contains(name.as_str()) { + continue; + } for (props, value) in values { - write!(fmt, "{}", name)?; + write!(s, "{}", name)?; for (key, prop) in props { - write!(fmt, ";{}={}", key, prop)?; + write!(s, ";{}={}", key, prop)?; } - writeln!(fmt, ":{}", value)?; + writeln!(s, ":{}", value)?; } } - writeln!(fmt, "END:{}", self.name)?; - Ok(()) + Ok(s) } } diff --git a/ticker-update/src/main.rs b/ticker-update/src/main.rs index 648cbbe..42368ef 100644 --- a/ticker-update/src/main.rs +++ b/ticker-update/src/main.rs @@ -63,7 +63,7 @@ fn obj_to_events(calendar: String, obj: &Object) -> Vec { } }; - match RRule::from_str(&format!("{}", obj)) { + match RRule::from_str(&obj.fmt_rrule().unwrap()) { Ok(rrule) => { let now = Utc::now(); let start = now - Duration::days(RRULE_LOOKBACK); @@ -75,8 +75,7 @@ fn obj_to_events(calendar: String, obj: &Object) -> Vec { .map(|dtstart| generate_event(dtstart, true)) .collect() } - Err(e) => { - println!("Error parsing RRULE: {}", e); + Err(_) => { vec![generate_event(dtstart, false)] } }