Browse Source

ticker-update: feed serialized event to rrule

master
Astro 3 months ago
parent
commit
1407c5f47a
2 changed files with 26 additions and 29 deletions
  1. 17
    0
      libticker/src/ics/mod.rs
  2. 9
    29
      ticker-update/src/main.rs

+ 17
- 0
libticker/src/ics/mod.rs View File

@@ -57,6 +57,23 @@ impl<'a> GetValue<'a, Timestamp> for Object {
57 57
     }
58 58
 }
59 59
 
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)?;
63
+        for (name, values) in &self.content {
64
+            for (props, value) in values {
65
+                write!(fmt, "{}", name)?;
66
+                for (key, prop) in props {
67
+                    write!(fmt, ";{}={}", key, prop)?;
68
+                }
69
+                writeln!(fmt, ":{}", value)?;
70
+            }
71
+        }
72
+        writeln!(fmt, "END:{}", self.name)?;
73
+        Ok(())
74
+    }
75
+}
76
+
60 77
 #[derive(Debug, Clone)]
61 78
 pub enum Timestamp {
62 79
     Date(NaiveDate),

+ 9
- 29
ticker-update/src/main.rs View File

@@ -33,9 +33,9 @@ fn obj_to_events(calendar: String, obj: &Object) -> Vec<Event> {
33 33
         return vec![];
34 34
     }
35 35
 
36
-    let (dtstart, dtstart_str): (Timestamp, &str) = match (obj.get("DTSTART"), obj.get("DTSTART")) {
37
-        (Some(dtstart), Some(dtstart_str)) => (dtstart, dtstart_str),
38
-        _ => return vec![],
36
+    let dtstart: Timestamp = match obj.get("DTSTART") {
37
+        Some(dtstart) => dtstart,
38
+        None => return vec![],
39 39
     };
40 40
     let dtstart = dtstart.or_hms(0, 0, 0);
41 41
     let dtend: Option<Timestamp> = obj.get("DTEND");
@@ -63,30 +63,8 @@ fn obj_to_events(calendar: String, obj: &Object) -> Vec<Event> {
63 63
         }
64 64
     };
65 65
 
66
-    let rrule_str: Option<&str> = obj.get("RRULE");
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))
86
-            .ok()
87
-    );
88
-    match rrule {
89
-        Some(rrule) => {
66
+    match RRule::from_str(&format!("{}", obj)) {
67
+        Ok(rrule) => {
90 68
             let now = Utc::now();
91 69
             let start = now - Duration::days(RRULE_LOOKBACK);
92 70
             let end = now + Duration::days(RRULE_LOOKAHEAD);
@@ -97,8 +75,10 @@ fn obj_to_events(calendar: String, obj: &Object) -> Vec<Event> {
97 75
                 .map(|dtstart| generate_event(dtstart, true))
98 76
                 .collect()
99 77
         }
100
-        None =>
101
-            vec![generate_event(dtstart, false)],
78
+        Err(e) => {
79
+            println!("Error parsing RRULE: {}", e);
80
+            vec![generate_event(dtstart, false)]
81
+        }
102 82
     }
103 83
 }
104 84
 

Loading…
Cancel
Save