aboutsummaryrefslogtreecommitdiff
path: root/src/event.rs
blob: db32b22fe9698d51b61bc94cf9c1ffcf9f8b3366 (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
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
use std::fmt;
use std::str::FromStr;

use chrono::Duration;

use date::Date;
use errors::EventError;


#[derive(Debug, Clone)]
pub struct Event {
    pub start: Date,
    pub end: End,
    pub summary: String,
    pub location: String,
    pub description: String,
    pub status: Status,
}

#[derive(Debug, Copy, Clone)]
pub enum Status {
    Confirmed,
    Tentative,
    Canceled,
}

#[derive(Debug, Copy, Clone)]
pub enum End {
    Date(Date),
    Duration(Duration),
}


impl Event {
    pub fn new() -> Event {
        return Event {
            summary: "".to_string(),
            location: "".to_string(),
            description: "".to_string(),
            status: Status::Confirmed,
            start: Date::new(),
            end: End::Date(Date::new()),
        };
    }

    pub fn end_date(&self) -> Date {
        match self.end {
            End::Date(date) => date,
            End::Duration(duration) => self.start + duration,
        }
    }
}

impl fmt::Display for Event {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{:?}-{:?}: {}",
            self.start,
            self.end_date(),
            self.summary
        )?;
        if !self.location.is_empty() {
            write!(f, " ({})", self.location)?;
        }
        if !self.description.is_empty() {
            write!(f, "\n\t{}", self.description)?;
        }
        Ok(())
    }
}

impl FromStr for Status {
    type Err = EventError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "CONFIRMED" => Ok(Status::Confirmed),
            "TENTATIVE" => Ok(Status::Tentative),
            "CANCELED" => Ok(Status::Canceled),
            _ => Err(EventError::StatusError),
        }
    }
}


pub fn get<'a>(events: &'a Vec<Event>, first: &Date, last: &Date) -> &'a [Event] {
    match events.iter().position(|ref e| e.start >= *first) {
        None => &[],
        Some(i) => {
            let j = events
                .iter()
                .position(|ref e| e.end_date() > *last)
                .unwrap_or(events.len());
            &events[i..j]
        }
    }
}