aboutsummaryrefslogtreecommitdiff
path: root/src/periodic.rs
blob: 0c52a9ba6b586a3681144d2b6f5f6b8e00020164 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use std::fmt;
use std::str::FromStr;

use chrono::Duration;

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

#[derive(Debug)]
pub struct Periodic {
    pub event: Event,
    pub freq: Freq,
    pub interval: i64,
    pub count: Option<i64>,
    pub until: Option<Date>,
}

#[derive(Debug)]
pub enum Freq {
    Secondly,
    Minutely,
    Hourly,
    Daily,
    Weekly,
    Monthly,
    Yearly,
}

impl Periodic {
    pub fn new() -> Self {
        Self {
            event: Event::new(),
            freq: Freq::Secondly,
            interval: 1,
            count: None,
            until: None,
        }
    }

    pub fn set_param(&mut self, param: &str, value: &str) -> Result<(), EventError> {
        match param {
            "FREQ" => self.freq = value.parse()?,
            "INTERVAL" => self.interval = value.parse()?,
            "COUNT" => self.count = Some(value.parse()?),
            "UNTIL" => self.until = Some(Date::parse(&value, "")?),
            _ => (),
        }
        Ok(())
    }

    pub fn get(&self, first: &Date, last: &Date) -> Vec<Event> {
        let mut start = self.event.start;
        let mut end = self.event.end_date();
        let mut events = Vec::new();
        let mut count = 0;
        while start <= *last {
            if (self.until.is_some() && start <= self.until.unwrap()) ||
                (self.count.is_some() && count >= self.count.unwrap())
            {
                break;
            }

            if start >= *first {
                let mut e = self.event.clone();
                e.start = start;
                e.end = End::Date(end);
                events.push(e);
                count += count;
            }
            start = self.freq.next_date(start, self.interval);
            end = self.freq.next_date(end, self.interval);
        }
        events
    }
}

impl fmt::Display for Periodic {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self.freq)?;
        if self.interval != 1 {
            write!(f, "({})", self.interval)?;
        }
        write!(f, ": {}", self.event)?;
        Ok(())
    }
}

impl Freq {
    pub fn next_date(&self, date: Date, count: i64) -> Date {
        match self {
            Freq::Secondly => date + Duration::seconds(count),
            Freq::Minutely => date + Duration::minutes(count),
            Freq::Hourly => date + Duration::hours(count),
            Freq::Daily => date + Duration::days(count),
            Freq::Weekly => date + Duration::weeks(count),
            Freq::Monthly => {
                let month = date.month();
                if month == 12 {
                    let date = date.with_month(1).unwrap();
                    date.with_year(date.year() + 1).unwrap()
                } else {
                    date.with_month(month + 1).unwrap()
                }
            }
            Freq::Yearly => date.with_year(date.year() + 1).unwrap(),
        }
    }
}

impl FromStr for Freq {
    type Err = EventError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "SECONDLY" => Ok(Freq::Secondly),
            "MINUTELY" => Ok(Freq::Minutely),
            "HOURLY" => Ok(Freq::Hourly),
            "DAILY" => Ok(Freq::Daily),
            "WEEKLY" => Ok(Freq::Weekly),
            "MONTHLY" => Ok(Freq::Monthly),
            "YEARLY" => Ok(Freq::Yearly),
            _ => Err(EventError::FreqError),
        }
    }
}