Implemented iCal file creation
This commit is contained in:
parent
0fe487c988
commit
f32fe121aa
4 changed files with 77 additions and 1 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -312,6 +312,12 @@ dependencies = [
|
||||||
"thiserror",
|
"thiserror",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ics"
|
||||||
|
version = "0.5.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9fd80257c65d91b3eb7ef6b5c29af82d738ce0b06e2a864b6a875d04831c5af3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "idna"
|
name = "idna"
|
||||||
version = "0.2.2"
|
version = "0.2.2"
|
||||||
|
@ -433,6 +439,7 @@ dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"ical",
|
"ical",
|
||||||
|
"ics",
|
||||||
"log",
|
"log",
|
||||||
"minidom",
|
"minidom",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
|
|
|
@ -24,3 +24,4 @@ async-trait = "0.1"
|
||||||
uuid = { version = "0.8", features = ["v4"] }
|
uuid = { version = "0.8", features = ["v4"] }
|
||||||
sanitize-filename = "0.3"
|
sanitize-filename = "0.3"
|
||||||
ical = "0.7"
|
ical = "0.7"
|
||||||
|
ics = "0.5"
|
||||||
|
|
|
@ -1,5 +1,71 @@
|
||||||
//! A module to build ICal files
|
//! 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<String, Box<dyn Error>> {
|
||||||
|
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!();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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_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 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";
|
||||||
|
|
Loading…
Add table
Reference in a new issue