From 744d07b174e5693ea449420a31c7691901390130 Mon Sep 17 00:00:00 2001 From: Abraham Toriz Date: Fri, 30 Jul 2021 12:01:44 -0500 Subject: [PATCH] finish ical format --- Cargo.lock | 18 +++++++++++ Cargo.toml | 1 + src/formatters/ical.rs | 69 ++++++++++++++++++++++++++++++------------ 3 files changed, 69 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index db5a593..86a816d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -236,6 +236,17 @@ dependencies = [ "libc", ] +[[package]] +name = "hostname" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c731c3e10504cc8ed35cfe2f1db4c9274c3d35fa486e3b31df46f068ef3e867" +dependencies = [ + "libc", + "match_cfg", + "winapi", +] + [[package]] name = "itertools" version = "0.10.1" @@ -280,6 +291,12 @@ version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" +[[package]] +name = "match_cfg" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" + [[package]] name = "memchr" version = "2.4.0" @@ -610,6 +627,7 @@ dependencies = [ "clap", "csv", "directories", + "hostname", "itertools", "lazy_static", "pretty_assertions", diff --git a/Cargo.toml b/Cargo.toml index 39ce10b..80b8c2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ regex = "1.5" lazy_static = "1.4" tempfile = "3" serde_json = "1.0" +hostname = "*" [dev-dependencies] pretty_assertions = "0.7.2" diff --git a/src/formatters/ical.rs b/src/formatters/ical.rs index 75ab4fe..2d7d33f 100644 --- a/src/formatters/ical.rs +++ b/src/formatters/ical.rs @@ -3,8 +3,36 @@ use std::io::Write; use crate::models::Entry; use crate::error::Result; +const ICAL_TIME_FORMAT: &'static str = "%Y%m%dT%H%M%SZ"; + pub fn print_formatted(entries: Vec, out: &mut W) -> Result<()> { - unimplemented!() + writeln!(out, "BEGIN:VCALENDAR")?; + writeln!(out, "CALSCALE:GREGORIAN")?; + writeln!(out, "METHOD:PUBLISH")?; + writeln!(out, "PRODID:tiempo-rs")?; + writeln!(out, "VERSION:2.0")?; + + let hostname = hostname::get()?; + let host = hostname.to_string_lossy(); + + for entry in entries.into_iter().filter(|e| e.end.is_some()) { + let uid = format!("tiempo-{id}", id=entry.id); + let note = entry.note.unwrap_or("".into()); + + writeln!(out, "BEGIN:VEVENT")?; + writeln!(out, "DESCRIPTION:{note}", note=note)?; + writeln!(out, "DTEND:{end}", end=entry.end.unwrap().format(ICAL_TIME_FORMAT).to_string())?; + writeln!(out, "DTSTAMP:{start}", start=entry.start.format(ICAL_TIME_FORMAT).to_string())?; + writeln!(out, "DTSTART:{start}", start=entry.start.format(ICAL_TIME_FORMAT).to_string())?; + writeln!(out, "SEQUENCE:0")?; + writeln!(out, "SUMMARY:{note}", note=note)?; + writeln!(out, "UID:{uid}@{host}", uid=uid, host=host)?; + writeln!(out, "END:VEVENT")?; + } + + writeln!(out, "END:VCALENDAR")?; + + Ok(()) } #[cfg(test)] @@ -22,29 +50,32 @@ mod tests { let an_hour_ago = now - Duration::hours(1); let entries = vec![ Entry::new_sample(1, an_hour_ago, Some(now)), - Entry::new_sample(1, an_hour_ago, None), + Entry::new_sample(2, an_hour_ago, None), ]; let mut out = Vec::new(); print_formatted(entries, &mut out).unwrap(); - assert_eq!(PrettyString(&String::from_utf8_lossy(&out)), PrettyString(&format!("\ - BEGIN:VCALENDAR\ - CALSCALE:GREGORIAN\ - METHOD:PUBLISH\ - PRODID:tiempo-rs\ - VERSION:2.0\ - BEGIN:VEVENT\ - DESCRIPTION:{note}\ - DTEND:{end}\ - DTSTAMP:{now}\ - DTSTART:{start}\ - SEQUENCE:0\ - SUMMARY:{note}\ - UID:{uid}@{host}\ - END:VEVENT\ - END:VCALENDAR\ - "))); + let host = hostname::get().unwrap(); + let host = host.to_string_lossy(); + + assert_eq!(PrettyString(&String::from_utf8_lossy(&out)), PrettyString(&format!("BEGIN:VCALENDAR +CALSCALE:GREGORIAN +METHOD:PUBLISH +PRODID:tiempo-rs +VERSION:2.0 +BEGIN:VEVENT +DESCRIPTION:{note} +DTEND:{end} +DTSTAMP:{start} +DTSTART:{start} +SEQUENCE:0 +SUMMARY:{note} +UID:tiempo-{id}@{host} +END:VEVENT +END:VCALENDAR +", note="entry 1", end=now.format(ICAL_TIME_FORMAT).to_string(), + start=an_hour_ago.format(ICAL_TIME_FORMAT).to_string(), id=1, host=host))); } }