start the ical formatter

This commit is contained in:
Abraham Toriz 2021-07-29 11:02:00 -05:00
parent 9fb45502a8
commit a6ee7aea5f
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
2 changed files with 54 additions and 0 deletions

View File

@ -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()),
})
}

50
src/formatters/ical.rs Normal file
View File

@ -0,0 +1,50 @@
use std::io::Write;
use crate::models::Entry;
use crate::error::Result;
pub fn print_formatted<W: Write>(entries: Vec<Entry>, 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\
")));
}
}