From 183abd3316532531d5510e4ea7b87aeb1404a4a2 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 18 Dec 2018 14:08:17 +0100 Subject: Match events by simple pattern on given key. --- src/main.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 28575d5..0a52dc3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,8 +62,18 @@ fn read_icalendar_from_file(filename: &str) -> std::io::Result bool { - true +fn match_event(event: &ical::parser::ical::component::IcalEvent, pattern: &str) -> bool { + if pattern == "*" { + true + } + else { + let parts: Vec<&str> = pattern.split(':').collect(); + let (key, pat) = (parts[0].to_uppercase(), parts[1]); + + event.properties.iter() + .filter(|ref p| p.name == key && &p.value.clone().unwrap().matches(&pat).count() > &0) + .count() > 0 + } } #[test] @@ -74,6 +84,14 @@ fn match_all_events() { assert_eq!(num_events, events.len()); } +#[test] +fn match_events_by_summary() { + let cal = read_icalendar_from_file("test/fixtures/events.ics").unwrap(); + let num_events = cal[0].events.len(); + let events:Vec<_> = cal[0].events.iter().filter(|ref event| match_event(&event, "summary:All")).collect(); + assert_eq!(num_events - 1, events.len()); +} + fn main() { let calendars = read_icalendar_from_file("test.ics").unwrap(); println!("Found {} calendard in file...", calendars.len()); -- cgit v1.2.3