From f32fe121aad2c3a22c73bfa88e05ae6a07dcf17f Mon Sep 17 00:00:00 2001 From: daladim Date: Mon, 12 Apr 2021 09:23:37 +0200 Subject: [PATCH] Implemented iCal file creation --- Cargo.lock | 7 +++++ Cargo.toml | 1 + src/ical/builder.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++- src/settings.rs | 2 ++ 4 files changed, 77 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 6bce024..5effa31 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -312,6 +312,12 @@ dependencies = [ "thiserror", ] +[[package]] +name = "ics" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fd80257c65d91b3eb7ef6b5c29af82d738ce0b06e2a864b6a875d04831c5af3" + [[package]] name = "idna" version = "0.2.2" @@ -433,6 +439,7 @@ dependencies = [ "bitflags", "env_logger", "ical", + "ics", "log", "minidom", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index 29a1581..e5bc36d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,3 +24,4 @@ async-trait = "0.1" uuid = { version = "0.8", features = ["v4"] } sanitize-filename = "0.3" ical = "0.7" +ics = "0.5" diff --git a/src/ical/builder.rs b/src/ical/builder.rs index b4f6329..275dafa 100644 --- a/src/ical/builder.rs +++ b/src/ical/builder.rs @@ -1,5 +1,71 @@ //! A module to build ICal files -pub fn build_from() { +use std::error::Error; +use ics::properties::{Comment, Status, Summary}; +use ics::{ICalendar, ToDo}; + +use crate::item::Item; +use crate::settings::{ORG_NAME, PRODUCT_NAME}; + +fn ical_product_id() -> String { + format!("-//{}//{}//EN", ORG_NAME, PRODUCT_NAME) +} + +/// Create an iCal item from a `crate::item::Item` +pub fn build_from(item: &Item) -> Result> { + let mut todo = ToDo::new(item.uid(), "20181021T190000"); + todo.push(Summary::new("Take pictures of squirrels (with ÜTF-8 chars)")); + todo.push(Comment::new("That's really something I'd like to do one day")); + + match item { + Item::Task(t) => { + let status = if t.completed() { Status::completed() } else { Status::needs_action() }; + todo.push(status); + }, + _ => { + unimplemented!() + }, + } + + let mut calendar = ICalendar::new("2.0", ical_product_id()); + calendar.add_todo(todo); + + Ok(calendar.to_string()) +} + + + +#[cfg(test)] +mod tests { + use super::*; + use crate::Task; + + #[test] + fn test_ical_from_task() { + let cal_id = "http://my.calend.ar/id".parse().unwrap(); + let task = Item::Task(Task::new( + String::from("This is a task"), true, &cal_id + )); + let expected_ical = format!("BEGIN:VCALENDAR\r\n\ + VERSION:2.0\r\n\ + PRODID:-//{}//{}//EN\r\n\ + BEGIN:VTODO\r\n\ + UID:{}\r\n\ + DTSTAMP:20181021T190000\r\n\ + SUMMARY:Take pictures of squirrels (with ÜTF-8 chars)\r\n\ + COMMENT:That's really something I'd like to do one day\r\n\ + STATUS:COMPLETED\r\n\ + END:VTODO\r\n\ + END:VCALENDAR\r\n", ORG_NAME, PRODUCT_NAME, task.uid()); + + let ical = build_from(&task); + assert_eq!(ical.unwrap(), expected_ical); + } + + #[test] + #[ignore] + fn test_ical_from_event() { + unimplemented!(); + } } diff --git a/src/settings.rs b/src/settings.rs index 8c3b01d..1c113d0 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -7,3 +7,5 @@ pub const PASSWORD: &str = "secret_password"; pub const EXAMPLE_TASK_URL: &str = "https://my.server.com/remote.php/dav/calendars/john/6121A0BE-C2E0-4F16-A3FA-658E54E7062A/74439558-CDFF-426C-92CD-ECDDACE971B0.ics"; pub const EXAMPLE_CALENDAR_URL: &str = "https://my.server.com/remote.php/dav/calendars/john/a_calendar_name/"; +pub const ORG_NAME: &str = "My organisation"; +pub const PRODUCT_NAME: &str = "My CalDAV client";