From a8e5bfbc63e7a1ff4b0e8e1d7d67ea57e87163e7 Mon Sep 17 00:00:00 2001 From: daladim Date: Tue, 23 Nov 2021 23:31:51 +0100 Subject: [PATCH] Added color support for calendar creation --- examples/provider-sync.rs | 2 +- src/calendar/mod.rs | 8 ++++---- src/client.rs | 29 +++++++++++++++++------------ 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/examples/provider-sync.rs b/examples/provider-sync.rs index c2c9586..cbc0b65 100644 --- a/examples/provider-sync.rs +++ b/examples/provider-sync.rs @@ -84,7 +84,7 @@ async fn add_items_and_sync_again(provider: &mut CalDavProvider) let new_calendar_url: Url = EXAMPLE_CREATED_CALENDAR_URL.parse().unwrap(); let new_calendar_name = "A brave new calendar".to_string(); if let Err(_err) = provider.local_mut() - .create_calendar(new_calendar_url.clone(), new_calendar_name.clone(), SupportedComponents::TODO, None) + .create_calendar(new_calendar_url.clone(), new_calendar_name.clone(), SupportedComponents::TODO, Some("#ff8000".parse().unwrap())) .await { println!("Unable to add calendar, maybe it exists already. We're not adding it after all."); } diff --git a/src/calendar/mod.rs b/src/calendar/mod.rs index 9ccce9b..ef1a5c6 100644 --- a/src/calendar/mod.rs +++ b/src/calendar/mod.rs @@ -23,12 +23,12 @@ bitflags! { impl SupportedComponents { pub fn to_xml_string(&self) -> String { format!(r#" - + {} {} - + "#, - if self.contains(Self::EVENT) { "" } else { "" }, - if self.contains(Self::TODO) { "" } else { "" }, + if self.contains(Self::EVENT) { "" } else { "" }, + if self.contains(Self::TODO) { "" } else { "" }, ) } } diff --git a/src/client.rs b/src/client.rs index a4d843e..993bec1 100644 --- a/src/client.rs +++ b/src/client.rs @@ -263,7 +263,7 @@ impl CalDavSource for Client { }, } - let creation_body = calendar_body(name, supported_components); + let creation_body = calendar_body(name, supported_components, color); let response = reqwest::Client::new() .request(Method::from_bytes(b"MKCALENDAR").unwrap(), url.clone()) @@ -282,21 +282,26 @@ impl CalDavSource for Client { } } -fn calendar_body(name: String, supported_components: SupportedComponents) -> String { +fn calendar_body(name: String, supported_components: SupportedComponents, color: Option) -> String { + let color_property = match color { + None => "".to_string(), + Some(color) => format!("{}FF", color.to_hex_string().to_ascii_uppercase()), + }; + // This is taken from https://tools.ietf.org/html/rfc4791#page-24 format!(r#" - - - - {} - {} - - - + + + + {} + {} + {} + + + "#, name, + color_property, supported_components.to_xml_string(), ) } -