ticker-update: restrict event serialization to keys that are relevant to rrule

This commit is contained in:
Astro 2021-06-10 20:21:42 +02:00
parent 1407c5f47a
commit 3aef06aa95
2 changed files with 20 additions and 12 deletions

View File

@ -1,4 +1,5 @@
use std::collections::HashMap; use std::collections::{HashMap, HashSet};
use std::fmt::Write;
use std::str::FromStr; use std::str::FromStr;
use chrono::{NaiveDate, NaiveDateTime}; use chrono::{NaiveDate, NaiveDateTime};
@ -57,20 +58,28 @@ impl<'a> GetValue<'a, Timestamp> for Object {
} }
} }
impl std::fmt::Display for Object { impl Object {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { pub fn fmt_rrule(&self) -> Result<String, std::fmt::Error> {
writeln!(fmt, "BEGIN:{}", self.name)?; let rrule_keys = [
"DTSTART",
"RRULE", "RDATE",
"EXRULE", "EXDATE",
].iter().cloned().collect::<HashSet<_>>();
let mut s = String::new();
for (name, values) in &self.content { for (name, values) in &self.content {
if ! rrule_keys.contains(name.as_str()) {
continue;
}
for (props, value) in values { for (props, value) in values {
write!(fmt, "{}", name)?; write!(s, "{}", name)?;
for (key, prop) in props { 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(s)
Ok(())
} }
} }

View File

@ -63,7 +63,7 @@ fn obj_to_events(calendar: String, obj: &Object) -> Vec<Event> {
} }
}; };
match RRule::from_str(&format!("{}", obj)) { match RRule::from_str(&obj.fmt_rrule().unwrap()) {
Ok(rrule) => { Ok(rrule) => {
let now = Utc::now(); let now = Utc::now();
let start = now - Duration::days(RRULE_LOOKBACK); let start = now - Duration::days(RRULE_LOOKBACK);
@ -75,8 +75,7 @@ fn obj_to_events(calendar: String, obj: &Object) -> Vec<Event> {
.map(|dtstart| generate_event(dtstart, true)) .map(|dtstart| generate_event(dtstart, true))
.collect() .collect()
} }
Err(e) => { Err(_) => {
println!("Error parsing RRULE: {}", e);
vec![generate_event(dtstart, false)] vec![generate_event(dtstart, false)]
} }
} }