Browse Source

ticker-update: feed EXDATE/EXRULE/RDATE to rrule

master
Astro 3 months ago
parent
commit
6f95611155
2 changed files with 34 additions and 5 deletions
  1. 15
    1
      libticker/src/ics/mod.rs
  2. 19
    4
      ticker-update/src/main.rs

+ 15
- 1
libticker/src/ics/mod.rs View File

@@ -22,7 +22,21 @@ pub struct Object {
22 22
 impl<'a> GetValue<'a, &'a str> for Object {
23 23
     fn get(&'a self, key: &'_ str) -> Option<&'a str> {
24 24
         self.content.get(key)
25
-            .and_then(|pairs| pairs.first().map(|(_props, ref value)| value.as_str()))
25
+            .and_then(|pairs| pairs.first().map(|(_props, value)| value.as_str()))
26
+    }
27
+}
28
+
29
+impl<'a> GetValue<'a, &'a (Props, String)> for Object {
30
+    fn get(&'a self, key: &'_ str) -> Option<&'a (Props, String)> {
31
+        self.content.get(key)
32
+            .and_then(|pairs| pairs.first())
33
+    }
34
+}
35
+
36
+impl<'a> GetValue<'a, &'a [(Props, String)]> for Object {
37
+    fn get(&'a self, key: &'_ str) -> Option<&'a [(Props, String)]> {
38
+        self.content.get(key)
39
+            .map(|data| data.as_slice())
26 40
     }
27 41
 }
28 42
 

+ 19
- 4
ticker-update/src/main.rs View File

@@ -64,10 +64,25 @@ fn obj_to_events(calendar: String, obj: &Object) -> Vec<Event> {
64 64
     };
65 65
 
66 66
     let rrule_str: Option<&str> = obj.get("RRULE");
67
-    let rrule = rrule_str.and_then(
68
-        |rrule_str| RRule::from_str(
69
-            &format!("DTSTART:{}\nRRULE:{}", dtstart_str, rrule_str)
70
-        ).map_err(|e| println!("Error parsing RRULE: {}", e))
67
+    let rrule = rrule_str.map(|rrule_str| 
68
+        format!("DTSTART:{}\nRRULE:{}", dtstart_str, rrule_str)
69
+    ).map(|mut s| {
70
+        for exdate in obj.get("EXDATE")
71
+            .unwrap_or(&[][..]) {
72
+                s = format!("{}\nEXDATE:{}", s, exdate.1);
73
+            }
74
+        for exrule in obj.get("EXRULE")
75
+            .unwrap_or(&[][..]) {
76
+                s = format!("{}\nEXRULE:{}", s, exrule.1);
77
+            }
78
+        for rdate in obj.get("RDATE")
79
+            .unwrap_or(&[][..]) {
80
+                s = format!("{}\nRDATE:{}", s, rdate.1);
81
+            }
82
+        s
83
+    }).and_then(|s|
84
+        RRule::from_str(&s)
85
+            .map_err(|e| println!("Error parsing RRULE: {}", e))
71 86
             .ok()
72 87
     );
73 88
     match rrule {

Loading…
Cancel
Save