Parsing calendars from server

This commit is contained in:
daladim 2021-02-21 00:16:40 +01:00
parent 0f081f78b4
commit ca0d07f16a
6 changed files with 218 additions and 21 deletions

View file

@ -1,14 +1,67 @@
use std::convert::TryFrom;
use std::error::Error;
use url::Url;
use crate::data::Task;
use crate::data::task::TaskId;
use bitflags::bitflags;
bitflags! {
pub struct SupportedComponents: u8 {
/// An event, such as a calendar meeting
const Event = 1;
/// A to-do item, such as a reminder
const Todo = 2;
}
}
impl TryFrom<minidom::Element> for SupportedComponents {
type Error = Box<dyn Error>;
/// Create an instance from an XML <supported-calendar-component-set> element
fn try_from(element: minidom::Element) -> Result<Self, Self::Error> {
if element.name() != "supported-calendar-component-set" {
return Err("Element must be a <supported-calendar-component-set>".into());
}
let mut flags = Self::empty();
for child in element.children() {
match child.attr("name") {
None => continue,
Some("VEVENT") => flags.insert(Self::Event),
Some("VTODO") => flags.insert(Self::Todo),
Some(other) => {
log::warn!("Unimplemented supported component type: {:?}. Ignoring it", other);
continue
},
};
}
Ok(flags)
}
}
/// A Caldav Calendar
#[derive(Clone, Debug)]
pub struct Calendar {
name: String,
url: Url,
supported_components: SupportedComponents,
tasks: Vec<Task>,
}
impl Calendar {
pub fn new(name: String, url: Url, supported_components: SupportedComponents) -> Self {
Self {
name, url, supported_components,
tasks: Vec::new(),
}
}
pub fn name(&self) -> &str {
&self.name
}