diff --git a/src/formatters.rs b/src/formatters.rs index 58d8af9..293d899 100644 --- a/src/formatters.rs +++ b/src/formatters.rs @@ -11,6 +11,7 @@ pub mod text; pub mod csv; pub mod json; pub mod ids; +pub mod ical; #[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] @@ -19,6 +20,7 @@ pub enum Formatter { Csv, Json, Ids, + Ical, Custom(String), } @@ -41,6 +43,7 @@ impl Formatter { Formatter::Csv => csv::print_formatted(entries, out, ids)?, Formatter::Json => json::print_formatted(entries, out)?, Formatter::Ids => ids::print_formatted(entries, out)?, + Formatter::Ical => ical::print_formatted(entries, out)?, Formatter::Custom(name) => { panic!("attempted custom formatter with name {} which is not implemented", name); } @@ -61,6 +64,7 @@ impl FromStr for Formatter { "csv" => Formatter::Csv, "json" => Formatter::Json, "ids" => Formatter::Ids, + "ical" => Formatter::Ical, custom_format => Formatter::Custom(custom_format.into()), }) } diff --git a/src/formatters/ical.rs b/src/formatters/ical.rs new file mode 100644 index 0000000..75ab4fe --- /dev/null +++ b/src/formatters/ical.rs @@ -0,0 +1,50 @@ +use std::io::Write; + +use crate::models::Entry; +use crate::error::Result; + +pub fn print_formatted(entries: Vec, out: &mut W) -> Result<()> { + unimplemented!() +} + +#[cfg(test)] +mod tests { + use pretty_assertions::assert_eq; + use chrono::{Utc, Duration}; + + use crate::test_utils::PrettyString; + + use super::*; + + #[test] + fn ical_format() { + let now = Utc::now(); + 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), + ]; + + 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\ + "))); + } +}