Parsing calendars from server
This commit is contained in:
parent
0f081f78b4
commit
ca0d07f16a
6 changed files with 218 additions and 21 deletions
102
Cargo.lock
generated
102
Cargo.lock
generated
|
@ -1,5 +1,25 @@
|
||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
|
[[package]]
|
||||||
|
name = "aho-corasick"
|
||||||
|
version = "0.7.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "atty"
|
||||||
|
version = "0.2.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||||
|
dependencies = [
|
||||||
|
"hermit-abi",
|
||||||
|
"libc",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "autocfg"
|
name = "autocfg"
|
||||||
version = "1.0.1"
|
version = "1.0.1"
|
||||||
|
@ -67,6 +87,19 @@ dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "env_logger"
|
||||||
|
version = "0.8.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "17392a012ea30ef05a610aa97dfb49496e71c9f676b27879922ea5bdf60d9d3f"
|
||||||
|
dependencies = [
|
||||||
|
"atty",
|
||||||
|
"humantime",
|
||||||
|
"log",
|
||||||
|
"regex",
|
||||||
|
"termcolor",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fnv"
|
name = "fnv"
|
||||||
version = "1.0.7"
|
version = "1.0.7"
|
||||||
|
@ -174,6 +207,15 @@ version = "0.9.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
|
checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hermit-abi"
|
||||||
|
version = "0.1.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "http"
|
name = "http"
|
||||||
version = "0.2.3"
|
version = "0.2.3"
|
||||||
|
@ -207,6 +249,12 @@ version = "0.3.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47"
|
checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "humantime"
|
||||||
|
version = "2.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hyper"
|
name = "hyper"
|
||||||
version = "0.14.4"
|
version = "0.14.4"
|
||||||
|
@ -361,6 +409,9 @@ dependencies = [
|
||||||
name = "my-tasks"
|
name = "my-tasks"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"bitflags",
|
||||||
|
"env_logger",
|
||||||
|
"log",
|
||||||
"minidom",
|
"minidom",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
@ -395,6 +446,12 @@ dependencies = [
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "once_cell"
|
||||||
|
version = "1.5.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "openssl"
|
name = "openssl"
|
||||||
version = "0.10.32"
|
version = "0.10.32"
|
||||||
|
@ -554,6 +611,24 @@ dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex"
|
||||||
|
version = "1.4.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d9251239e129e16308e70d853559389de218ac275b515068abc96829d05b948a"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"memchr",
|
||||||
|
"regex-syntax",
|
||||||
|
"thread_local",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-syntax"
|
||||||
|
version = "0.6.22"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b5eb417147ba9860a96cfe72a0b93bf88fee1744b5636ec99ab20c1aa9376581"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "remove_dir_all"
|
name = "remove_dir_all"
|
||||||
version = "0.5.3"
|
version = "0.5.3"
|
||||||
|
@ -707,6 +782,24 @@ dependencies = [
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "termcolor"
|
||||||
|
version = "1.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thread_local"
|
||||||
|
version = "1.1.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8018d24e04c95ac8790716a5987d0fec4f8b27249ffa0f7d33f1369bdfb88cbd"
|
||||||
|
dependencies = [
|
||||||
|
"once_cell",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tinyvec"
|
name = "tinyvec"
|
||||||
version = "1.1.1"
|
version = "1.1.1"
|
||||||
|
@ -972,6 +1065,15 @@ version = "0.4.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-util"
|
||||||
|
version = "0.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
|
||||||
|
dependencies = [
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi-x86_64-pc-windows-gnu"
|
name = "winapi-x86_64-pc-windows-gnu"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
|
|
|
@ -14,3 +14,4 @@ uuid = "0.8"
|
||||||
reqwest = "0.11"
|
reqwest = "0.11"
|
||||||
minidom = "0.13"
|
minidom = "0.13"
|
||||||
url = "2.2"
|
url = "2.2"
|
||||||
|
bitflags = "1.2"
|
||||||
|
|
|
@ -1,14 +1,67 @@
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
use crate::data::Task;
|
use crate::data::Task;
|
||||||
use crate::data::task::TaskId;
|
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
|
/// A Caldav Calendar
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
pub struct Calendar {
|
pub struct Calendar {
|
||||||
name: String,
|
name: String,
|
||||||
|
url: Url,
|
||||||
|
supported_components: SupportedComponents,
|
||||||
|
|
||||||
tasks: Vec<Task>,
|
tasks: Vec<Task>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Calendar {
|
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 {
|
pub fn name(&self) -> &str {
|
||||||
&self.name
|
&self.name
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,12 +3,15 @@
|
||||||
//! Some of it comes from https://github.com/marshalshi/caldav-client-rust.git
|
//! Some of it comes from https://github.com/marshalshi/caldav-client-rust.git
|
||||||
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
use reqwest::Method;
|
use reqwest::Method;
|
||||||
use reqwest::header::CONTENT_TYPE;
|
use reqwest::header::CONTENT_TYPE;
|
||||||
use minidom::Element;
|
use minidom::Element;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
|
use crate::data::Calendar;
|
||||||
|
|
||||||
static DAVCLIENT_BODY: &str = r#"
|
static DAVCLIENT_BODY: &str = r#"
|
||||||
<d:propfind xmlns:d="DAV:">
|
<d:propfind xmlns:d="DAV:">
|
||||||
<d:prop>
|
<d:prop>
|
||||||
|
@ -43,7 +46,7 @@ pub struct Client {
|
||||||
|
|
||||||
principal: Option<Url>,
|
principal: Option<Url>,
|
||||||
calendar_home_set: Option<Url>,
|
calendar_home_set: Option<Url>,
|
||||||
calendars_url: Option<Vec<Url>>,
|
calendars: Option<Vec<Calendar>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
|
@ -57,7 +60,7 @@ impl Client {
|
||||||
password: password.to_string(),
|
password: password.to_string(),
|
||||||
principal: None,
|
principal: None,
|
||||||
calendar_home_set: None,
|
calendar_home_set: None,
|
||||||
calendars_url: None,
|
calendars: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,38 +125,70 @@ impl Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the list of calendars, or fetch from server if not known yet
|
/// Return the list of calendars, or fetch from server if not known yet
|
||||||
pub async fn get_calendars_url(&mut self) -> Result<Vec<Url>, Box<dyn Error>> {
|
pub async fn get_calendars(&mut self) -> Result<Vec<Calendar>, Box<dyn Error>> {
|
||||||
if let Some(c) = &self.calendars_url {
|
if let Some(c) = &self.calendars {
|
||||||
return Ok(c.clone());
|
return Ok(c.to_vec());
|
||||||
}
|
}
|
||||||
let cal_home_set = self.get_cal_home_set().await?;
|
let cal_home_set = self.get_cal_home_set().await?;
|
||||||
|
|
||||||
let text = self.sub_request(&cal_home_set, CAL_BODY.into(), 1).await?;
|
let text = self.sub_request(&cal_home_set, CAL_BODY.into(), 1).await?;
|
||||||
|
|
||||||
let root: Element = text.parse().unwrap();
|
let root: Element = text.parse().unwrap();
|
||||||
let reps = find_elems(&root, "response".to_string());
|
let reps = find_elems(&root, "response".to_string());
|
||||||
let mut calendars = Vec::new();
|
let mut calendars = Vec::new();
|
||||||
for rep in reps {
|
for rep in reps {
|
||||||
// TODO checking `displayname` here but may there are better way
|
let display_name = find_elem(rep, "displayname".to_string()).map(|e| e.text()).unwrap_or("<no name>".to_string());
|
||||||
let displayname = find_elem(rep, "displayname".to_string())
|
log::debug!("Considering calendar {}", display_name);
|
||||||
.unwrap()
|
|
||||||
.text();
|
// We filter out non-calendar items
|
||||||
if displayname == "" {
|
let resource_types = match find_elem(rep, "resourcetype".to_string()) {
|
||||||
|
None => continue,
|
||||||
|
Some(rt) => rt,
|
||||||
|
};
|
||||||
|
let mut found_calendar_type = false;
|
||||||
|
for resource_type in resource_types.children() {
|
||||||
|
if resource_type.name() == "calendar" {
|
||||||
|
found_calendar_type = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if found_calendar_type == false {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: filter by:
|
// We filter out the root calendar collection, that has an empty supported-calendar-component-set
|
||||||
// <cal:supported-calendar-component-set>
|
let el_supported_comps = match find_elem(rep, "supported-calendar-component-set".to_string()) {
|
||||||
// <cal:comp name=\"VEVENT\"/>
|
None => continue,
|
||||||
// <cal:comp name=\"VTODO\"/>
|
Some(comps) => comps,
|
||||||
// </cal:supported-calendar-component-set>
|
};
|
||||||
|
if el_supported_comps.children().count() == 0 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
let href = find_elem(rep, "href".to_string()).unwrap();
|
let calendar_href = match find_elem(rep, "href".to_string()) {
|
||||||
let href_text = href.text();
|
None => {
|
||||||
|
log::warn!("Calendar {} has no URL! Ignoring it.", display_name);
|
||||||
|
continue;
|
||||||
|
},
|
||||||
|
Some(h) => h.text(),
|
||||||
|
};
|
||||||
|
|
||||||
let mut this_calendar_url = self.url.clone();
|
let mut this_calendar_url = self.url.clone();
|
||||||
this_calendar_url.set_path(&href_text);
|
this_calendar_url.set_path(&calendar_href);
|
||||||
calendars.push(this_calendar_url);
|
|
||||||
|
let supported_components = match crate::data::calendar::SupportedComponents::try_from(el_supported_comps.clone()) {
|
||||||
|
Err(err) => {
|
||||||
|
log::warn!("Calendar {} has invalid supported components ({})! Ignoring it.", display_name, err);
|
||||||
|
continue;
|
||||||
|
},
|
||||||
|
Ok(sc) => sc,
|
||||||
|
};
|
||||||
|
let this_calendar = Calendar::new(display_name, this_calendar_url, supported_components);
|
||||||
|
log::info!("Found calendar {}", this_calendar.name());
|
||||||
|
calendars.push(this_calendar);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.calendars = Some(calendars.clone());
|
||||||
Ok(calendars)
|
Ok(calendars)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -208,6 +243,11 @@ mod test {
|
||||||
let _ = env_logger::builder().is_test(true).try_init();
|
let _ = env_logger::builder().is_test(true).try_init();
|
||||||
|
|
||||||
let mut client = Client::new(URL, USERNAME, PASSWORD).unwrap();
|
let mut client = Client::new(URL, USERNAME, PASSWORD).unwrap();
|
||||||
client.get_calendars_url().await.unwrap();
|
let calendars = client.get_calendars().await.unwrap();
|
||||||
|
|
||||||
|
println!("Calendars:");
|
||||||
|
calendars.iter()
|
||||||
|
.map(|cal| println!(" {}", cal.name()))
|
||||||
|
.collect::<()>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
mod calendar;
|
pub mod calendar;
|
||||||
mod task;
|
mod task;
|
||||||
mod client;
|
mod client;
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ use uuid::Uuid;
|
||||||
pub type TaskId = Uuid;
|
pub type TaskId = Uuid;
|
||||||
|
|
||||||
/// A to-do task
|
/// A to-do task
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
pub struct Task {
|
pub struct Task {
|
||||||
id: TaskId,
|
id: TaskId,
|
||||||
name: String,
|
name: String,
|
||||||
|
|
Loading…
Add table
Reference in a new issue