diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2018-12-18 11:31:06 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2018-12-18 11:31:06 +0100 |
commit | 0e8a887e09b4065e6ed7f1c5ab547e3b17a96742 (patch) | |
tree | 727c265f4dcf4f8411346cef78b2172e18d1e88b /src | |
parent | a38d4b19e3b9d8791773b6fd7171cfb07773f9ea (diff) | |
download | icaltool-0e8a887e09b4065e6ed7f1c5ab547e3b17a96742.tar.gz icaltool-0e8a887e09b4065e6ed7f1c5ab547e3b17a96742.tar.bz2 icaltool-0e8a887e09b4065e6ed7f1c5ab547e3b17a96742.zip |
Move printing of event to a function.
This also removes the need for the local Event struct.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 43 |
1 files changed, 21 insertions, 22 deletions
diff --git a/src/main.rs b/src/main.rs index a4728a1..43cdc5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,13 +19,6 @@ use chrono::{DateTime, TimeZone, Utc}; use std::fs::File; use std::io::BufReader; -#[derive(Default)] -struct Event { - start: Option<DateTime<Utc>>, - end: Option<DateTime<Utc>>, - summary: String, -} - fn parse_datetime(datetime: Option<String>) -> Option<DateTime<Utc>> { if let Some(mut dt) = datetime { if dt.is_empty() { @@ -42,6 +35,26 @@ fn parse_datetime(datetime: Option<String>) -> Option<DateTime<Utc>> { } } +fn print_event(event: &ical::parser::ical::component::IcalEvent) { + let mut start: Option<DateTime<Utc>> = None; + let mut end: Option<DateTime<Utc>> = None; + let mut summary = String::new(); + + for ref p in &event.properties { + match p.name.as_ref() { + "DTSTART" => start = parse_datetime(p.value.clone()), + "DTEND" => end = parse_datetime(p.value.clone()), + "SUMMARY" => summary = p.value.clone().unwrap_or("".to_string()), + _ => (), + } + } + + println!("---> {} - {}: {}", + start.unwrap_or(Utc::now()), + end.unwrap_or(Utc::now()), + summary); +} + fn main() { let buf = BufReader::new(File::open("test.ics").unwrap()); let calendars: Vec<_> = ical::IcalParser::new(buf) @@ -51,21 +64,7 @@ fn main() { for c in calendars { for e in c.events { - let mut ev = Event::default(); - - for p in e.properties { - match p.name.as_ref() { - "DTSTART" => ev.start = parse_datetime(p.value), - "DTEND" => ev.end = parse_datetime(p.value), - "SUMMARY" => ev.summary = p.value.unwrap_or("".to_string()), - _ => (), - } - } - - println!("---> {} - {}: {}", - ev.start.unwrap_or(Utc::now()), - ev.end.unwrap_or(Utc::now()), - ev.summary); + print_event(&e); } } } |