82 lines
2.3 KiB
Rust
82 lines
2.3 KiB
Rust
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<()> {
|
|
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)]
|
|
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(2, an_hour_ago, None),
|
|
];
|
|
|
|
let mut out = Vec::new();
|
|
|
|
print_formatted(entries, &mut out).unwrap();
|
|
|
|
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)));
|
|
}
|
|
}
|