Bladeren bron

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

master
Astro 3 maanden geleden
bovenliggende
commit
3aef06aa95
2 gewijzigde bestanden met toevoegingen van 20 en 12 verwijderingen
  1. 18
    9
      libticker/src/ics/mod.rs
  2. 2
    3
      ticker-update/src/main.rs

+ 18
- 9
libticker/src/ics/mod.rs Bestand weergeven

@@ -1,4 +1,5 @@
1
-use std::collections::HashMap;
1
+use std::collections::{HashMap, HashSet};
2
+use std::fmt::Write;
2 3
 use std::str::FromStr;
3 4
 use chrono::{NaiveDate, NaiveDateTime};
4 5
 
@@ -57,20 +58,28 @@ impl<'a> GetValue<'a, Timestamp> for Object {
57 58
     }
58 59
 }
59 60
 
60
-impl std::fmt::Display for Object {
61
-    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
62
-        writeln!(fmt, "BEGIN:{}", self.name)?;
61
+impl Object {
62
+    pub fn fmt_rrule(&self) -> Result<String, std::fmt::Error> {
63
+        let rrule_keys = [
64
+            "DTSTART",
65
+            "RRULE", "RDATE",
66
+            "EXRULE", "EXDATE",
67
+        ].iter().cloned().collect::<HashSet<_>>();
68
+
69
+        let mut s = String::new();
63 70
         for (name, values) in &self.content {
71
+            if ! rrule_keys.contains(name.as_str()) {
72
+                continue;
73
+            }
64 74
             for (props, value) in values {
65
-                write!(fmt, "{}", name)?;
75
+                write!(s, "{}", name)?;
66 76
                 for (key, prop) in props {
67
-                    write!(fmt, ";{}={}", key, prop)?;
77
+                    write!(s, ";{}={}", key, prop)?;
68 78
                 }
69
-                writeln!(fmt, ":{}", value)?;
79
+                writeln!(s, ":{}", value)?;
70 80
             }
71 81
         }
72
-        writeln!(fmt, "END:{}", self.name)?;
73
-        Ok(())
82
+        Ok(s)
74 83
     }
75 84
 }
76 85
 

+ 2
- 3
ticker-update/src/main.rs Bestand weergeven

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

Laden…
Annuleren
Opslaan