finish ical format
This commit is contained in:
parent
a6ee7aea5f
commit
744d07b174
|
@ -236,6 +236,17 @@ dependencies = [
|
||||||
"libc",
|
"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]]
|
[[package]]
|
||||||
name = "itertools"
|
name = "itertools"
|
||||||
version = "0.10.1"
|
version = "0.10.1"
|
||||||
|
@ -280,6 +291,12 @@ version = "0.5.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
|
checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "match_cfg"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
version = "2.4.0"
|
version = "2.4.0"
|
||||||
|
@ -610,6 +627,7 @@ dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"csv",
|
"csv",
|
||||||
"directories",
|
"directories",
|
||||||
|
"hostname",
|
||||||
"itertools",
|
"itertools",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"pretty_assertions",
|
"pretty_assertions",
|
||||||
|
|
|
@ -27,6 +27,7 @@ regex = "1.5"
|
||||||
lazy_static = "1.4"
|
lazy_static = "1.4"
|
||||||
tempfile = "3"
|
tempfile = "3"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
hostname = "*"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
pretty_assertions = "0.7.2"
|
pretty_assertions = "0.7.2"
|
||||||
|
|
|
@ -3,8 +3,36 @@ use std::io::Write;
|
||||||
use crate::models::Entry;
|
use crate::models::Entry;
|
||||||
use crate::error::Result;
|
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<()> {
|
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)]
|
#[cfg(test)]
|
||||||
|
@ -22,29 +50,32 @@ mod tests {
|
||||||
let an_hour_ago = now - Duration::hours(1);
|
let an_hour_ago = now - Duration::hours(1);
|
||||||
let entries = vec![
|
let entries = vec![
|
||||||
Entry::new_sample(1, an_hour_ago, Some(now)),
|
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();
|
let mut out = Vec::new();
|
||||||
|
|
||||||
print_formatted(entries, &mut out).unwrap();
|
print_formatted(entries, &mut out).unwrap();
|
||||||
|
|
||||||
assert_eq!(PrettyString(&String::from_utf8_lossy(&out)), PrettyString(&format!("\
|
let host = hostname::get().unwrap();
|
||||||
BEGIN:VCALENDAR\
|
let host = host.to_string_lossy();
|
||||||
CALSCALE:GREGORIAN\
|
|
||||||
METHOD:PUBLISH\
|
assert_eq!(PrettyString(&String::from_utf8_lossy(&out)), PrettyString(&format!("BEGIN:VCALENDAR
|
||||||
PRODID:tiempo-rs\
|
CALSCALE:GREGORIAN
|
||||||
VERSION:2.0\
|
METHOD:PUBLISH
|
||||||
BEGIN:VEVENT\
|
PRODID:tiempo-rs
|
||||||
DESCRIPTION:{note}\
|
VERSION:2.0
|
||||||
DTEND:{end}\
|
BEGIN:VEVENT
|
||||||
DTSTAMP:{now}\
|
DESCRIPTION:{note}
|
||||||
DTSTART:{start}\
|
DTEND:{end}
|
||||||
SEQUENCE:0\
|
DTSTAMP:{start}
|
||||||
SUMMARY:{note}\
|
DTSTART:{start}
|
||||||
UID:{uid}@{host}\
|
SEQUENCE:0
|
||||||
END:VEVENT\
|
SUMMARY:{note}
|
||||||
END:VCALENDAR\
|
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)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue