aboutsummaryrefslogtreecommitdiff
path: root/src/event.rs
blob: f58519a719d86be28189bd26f64cfc489b4570c7 (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
99
100
101
102
103
104
105
106
107
use std::cmp::{Ordering, Ord};
use std::fmt;
use std::str::FromStr;

use chrono::Duration;

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


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

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

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


impl Event {
    pub fn new(calendar: &Option<String>) -> Event {
        return Event {
            summary: "".to_string(),
            location: "".to_string(),
            description: "".to_string(),
            status: Status::Confirmed,
            start: Date::new(),
            end: End::Date(Date::new()),
            calendar: match calendar {
                None => "[unknown calendar]".to_string(),
                Some(a) => a.to_string()
            }
        };
    }

    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 Ord for Event {
    fn cmp(&self, other: &Self) -> Ordering {
        let ord = self.start.cmp(&other.start);
        if ord == Ordering::Equal {
            self.end_date().cmp(&other.end_date())
        } else {
            ord
        }
    }
}

impl PartialOrd for Event {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

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),
            "CANCELLED" => Ok(Status::Canceled),
            _ => Err(EventError::StatusError),
        }
    }
}