From 0e8a887e09b4065e6ed7f1c5ab547e3b17a96742 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 18 Dec 2018 11:31:06 +0100 Subject: Move printing of event to a function. This also removes the need for the local Event struct. --- src/main.rs | 43 +++++++++++++++++++++---------------------- 1 file 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>, - end: Option>, - summary: String, -} - fn parse_datetime(datetime: Option) -> Option> { if let Some(mut dt) = datetime { if dt.is_empty() { @@ -42,6 +35,26 @@ fn parse_datetime(datetime: Option) -> Option> { } } +fn print_event(event: &ical::parser::ical::component::IcalEvent) { + let mut start: Option> = None; + let mut end: Option> = 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); } } } -- cgit v1.2.3