aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 50f3eaac449f27d9aabda5609cf9d20339ca905a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// icaltool - a tool to get information out of ical/ics files.
// Copyright (C) 2018  Harald Eilertsen <haraldei@anduin.net>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use ical;
use chrono::{DateTime, TimeZone, Utc};
use std::fs::File;
use std::io::BufReader;

fn parse_datetime(datetime: Option<String>) -> Option<DateTime<Utc>> {
    if let Some(mut dt) = datetime {
        if dt.is_empty() {
            return None;
        }

        if !dt.contains('T') {
            dt += "T000000";
        }
        Utc.datetime_from_str(&dt, "%Y%m%dT%H%M%S").ok()
    }
    else {
        None
    }
}

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 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_else(String::new),
            _ => (),
        }
    }

    println!("---> {} - {}: {}",
        start.unwrap_or_else(Utc::now),
        end.unwrap_or_else(Utc::now),
        summary);
}

fn read_icalendar_from_file(filename: &str) -> std::io::Result<Vec<ical::parser::ical::component::IcalCalendar>> {
    let buf = BufReader::new(File::open(filename)?);
    Ok(ical::IcalParser::new(buf)
        .filter_map(|c| c.ok())
        .collect())
}

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
    }
}

#[cfg(test)]
fn test_match_event_helper(pattern: &str) -> Vec<ical::parser::ical::component::IcalEvent> {
    let cal = read_icalendar_from_file("test/fixtures/events.ics").unwrap();
    cal[0].events.iter()
        .filter_map(|event| {
            if match_event(&event, pattern) {
                Some(event.clone())
            }
            else {
                None
            }
        })
        .collect()
}

#[test]
fn match_all_events() {
    let events:Vec<_> = test_match_event_helper("*");
    assert_eq!(3, events.len());
}

#[test]
fn match_events_by_summary() {
    let events:Vec<_> = test_match_event_helper("summary:All");
    assert_eq!(2, events.len());
}

fn main() {
    let mut args = std::env::args().skip(1);
    let filename = args.next().unwrap();
    let pattern = args.next().unwrap();
    let calendars = read_icalendar_from_file(&filename).unwrap();
    println!("Found {} calendard in file...", calendars.len());

    for c in calendars {
        for e in c.events.iter().filter(|e| match_event(&e, &pattern)) {
            print_event(&e);
        }
    }
}