ticker-update: feed serialized event to rrule

master
Astro 2 years ago
parent 6f95611155
commit 1407c5f47a
  1. 17
      libticker/src/ics/mod.rs
  2. 38
      ticker-update/src/main.rs

@ -57,6 +57,23 @@ 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)?;
for (name, values) in &self.content {
for (props, value) in values {
write!(fmt, "{}", name)?;
for (key, prop) in props {
write!(fmt, ";{}={}", key, prop)?;
}
writeln!(fmt, ":{}", value)?;
}
}
writeln!(fmt, "END:{}", self.name)?;
Ok(())
}
}
#[derive(Debug, Clone)]
pub enum Timestamp {
Date(NaiveDate),

@ -33,9 +33,9 @@ fn obj_to_events(calendar: String, obj: &Object) -> Vec<Event> {
return vec![];
}
let (dtstart, dtstart_str): (Timestamp, &str) = match (obj.get("DTSTART"), obj.get("DTSTART")) {
(Some(dtstart), Some(dtstart_str)) => (dtstart, dtstart_str),
_ => return vec![],
let dtstart: Timestamp = match obj.get("DTSTART") {
Some(dtstart) => dtstart,
None => return vec![],
};
let dtstart = dtstart.or_hms(0, 0, 0);
let dtend: Option<Timestamp> = obj.get("DTEND");
@ -63,30 +63,8 @@ fn obj_to_events(calendar: String, obj: &Object) -> Vec<Event> {
}
};
let rrule_str: Option<&str> = obj.get("RRULE");
let rrule = rrule_str.map(|rrule_str|
format!("DTSTART:{}\nRRULE:{}", dtstart_str, rrule_str)
).map(|mut s| {
for exdate in obj.get("EXDATE")
.unwrap_or(&[][..]) {
s = format!("{}\nEXDATE:{}", s, exdate.1);
}
for exrule in obj.get("EXRULE")
.unwrap_or(&[][..]) {
s = format!("{}\nEXRULE:{}", s, exrule.1);
}
for rdate in obj.get("RDATE")
.unwrap_or(&[][..]) {
s = format!("{}\nRDATE:{}", s, rdate.1);
}
s
}).and_then(|s|
RRule::from_str(&s)
.map_err(|e| println!("Error parsing RRULE: {}", e))
.ok()
);
match rrule {
Some(rrule) => {
match RRule::from_str(&format!("{}", obj)) {
Ok(rrule) => {
let now = Utc::now();
let start = now - Duration::days(RRULE_LOOKBACK);
let end = now + Duration::days(RRULE_LOOKAHEAD);
@ -97,8 +75,10 @@ fn obj_to_events(calendar: String, obj: &Object) -> Vec<Event> {
.map(|dtstart| generate_event(dtstart, true))
.collect()
}
None =>
vec![generate_event(dtstart, false)],
Err(e) => {
println!("Error parsing RRULE: {}", e);
vec![generate_event(dtstart, false)]
}
}
}

Loading…
Cancel
Save