aboutsummaryrefslogtreecommitdiff
path: root/src/events.rs
blob: a78a286cfb3b7047d99b4c7c21e1b68cd5411f73 (plain)
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
use std::io::BufRead;
use std::fmt;
use ical::IcalParser;

use event::{Event, Date};
use errors::EventError;

pub struct Events {
    single: Vec<Event>,
}

impl Events {
    pub fn parse<B: BufRead>(buf: B) -> Result<Self, EventError> {
        let reader = IcalParser::new(buf);
        let mut single = Vec::new();

        for line in reader {
            for ev in line?.events {
                let mut event = Event::new();
                for property in ev.properties {
                    let value = property.value.unwrap_or("".to_string());
                    let mut time_zone = "".to_string();

                    for (param, value) in property.params.unwrap_or(vec![]) {
                        if param == "TZID" && value.len() > 0 {
                            time_zone = value[0].clone();
                        }
                    }

                    match property.name.as_ref() {
                        "SUMMARY" => event.summary = value,
                        "LOCATION" => event.location = value,
                        "DESCRIPTION" => event.description = value,
                        "STATUS" => event.status = value.parse()?,
                        "DTSTART" => event.start = Date::parse(&value, &time_zone)?,
                        "DTEND" => event.end = Date::parse(&value, &time_zone)?,
                        _ => (),
                    };
                }
                single.push(event);
            }
        }

        single.sort_by(|a, b| a.start.cmp(&b.start));
        Ok(Events { single })
    }
}

impl fmt::Display for Events {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for event in &self.single {
            writeln!(f, "{}", event)?;
        }
        Ok(())
    }
}