kitchen_fridge/calendar/
mod.rs

1//! Various objects that implement Calendar-related traits
2
3pub mod cached_calendar;
4pub mod remote_calendar;
5
6use std::convert::TryFrom;
7use std::error::Error;
8
9use serde::{Deserialize, Serialize};
10
11use bitflags::bitflags;
12
13bitflags! {
14    #[derive(Serialize, Deserialize)]
15    pub struct SupportedComponents: u8 {
16        /// An event, such as a calendar meeting
17        const EVENT = 1;
18        /// A to-do item, such as a reminder
19        const TODO = 2;
20    }
21}
22
23impl SupportedComponents {
24    pub fn to_xml_string(&self) -> String {
25        format!(r#"
26            <B:supported-calendar-component-set>
27                {} {}
28            </B:supported-calendar-component-set>
29            "#,
30            if self.contains(Self::EVENT) { "<B:comp name=\"VEVENT\"/>" } else { "" },
31            if self.contains(Self::TODO)  { "<B:comp name=\"VTODO\"/>"  } else { "" },
32        )
33    }
34}
35
36impl TryFrom<minidom::Element> for SupportedComponents {
37    type Error = Box<dyn Error>;
38
39    /// Create an instance from an XML <supported-calendar-component-set> element
40    fn try_from(element: minidom::Element) -> Result<Self, Self::Error> {
41        if element.name() != "supported-calendar-component-set" {
42            return Err("Element must be a <supported-calendar-component-set>".into());
43        }
44
45        let mut flags = Self::empty();
46        for child in element.children() {
47            match child.attr("name") {
48                None => continue,
49                Some("VEVENT") => flags.insert(Self::EVENT),
50                Some("VTODO") => flags.insert(Self::TODO),
51                Some(other) => {
52                    log::warn!("Unimplemented supported component type: {:?}. Ignoring it", other);
53                    continue
54                },
55            };
56        }
57
58        Ok(flags)
59    }
60}
61
62
63/// Flags to tell which events should be retrieved
64pub enum SearchFilter {
65    /// Return all items
66    All,
67    /// Return only tasks
68    Tasks,
69    // /// Return only completed tasks
70    // CompletedTasks,
71    // /// Return only calendar events
72    // Events,
73}
74
75impl Default for SearchFilter {
76    fn default() -> Self {
77        SearchFilter::All
78    }
79}