finish ical format

This commit is contained in:
Abraham Toriz 2021-07-30 12:01:44 -05:00
parent a6ee7aea5f
commit 744d07b174
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
3 changed files with 69 additions and 19 deletions

18
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -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<W: Write>(entries: Vec<Entry>, 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)));
}
}