aboutsummaryrefslogtreecommitdiff
path: root/src/periodic.rs
blob: 2891bedf3654e46e329d0de163dda827c656b3b6 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use std::fmt;
use std::str::FromStr;
use std::ops::Add;
use std::collections::HashMap;

use chrono::{Duration, Weekday};

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

pub type Byday = HashMap<Weekday, Vec<i32>>;

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

#[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,
            byday: None,
            bysetpos: 0,
            wkst: Weekday::Mon,
        }
    }

    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, "")?),
            "BYDAY" => self.byday = Some(parse_byday(value)?),
            "BYSETPOS" => self.bysetpos = value.parse()?,
            "WKST" => self.wkst = parse_weekday(value)?,
            _ => (),
        }
        Ok(())
    }

    pub fn iter<'a>(&'a self) -> Iter<'a> {
        Iter {
            periodic: self,
            start: self.event.start,
            end: self.event.end_date(),
            count: 0,
        }
    }
}

pub struct Iter<'a> {
    periodic: &'a Periodic,
    start: Date,
    end: Date,
    count: i64,
}

impl<'a> Iterator for Iter<'a> {
    type Item = Event;

    fn next(&mut self) -> Option<Self::Item> {
        let p = self.periodic;

        if (p.until.is_some() && self.start <= p.until.unwrap()) ||
            (p.count.is_some() && self.count >= p.count.unwrap())
        {
            return None;
        }

        let mut event = p.event.clone();
        event.start = self.start;
        event.end = End::Date(self.end);

        let duration = self.next_duration(self.start);
        self.start = self.start + duration;
        self.end = self.end + duration;
        self.count += 1;

        Some(event)
    }
}

impl<'a> Iter<'a> {
    fn next_duration(&self, date: Date) -> Duration {
        let p = self.periodic;
        match p.freq {
            Freq::Secondly => Duration::seconds(p.interval),
            Freq::Minutely => Duration::minutes(p.interval),
            Freq::Hourly => Duration::hours(p.interval),
            Freq::Daily => Duration::days(p.interval),
            Freq::Weekly => {
                match &p.byday {
                    None => Duration::weeks(p.interval),
                    Some(byday) => {
                        let mut weekday = date.weekday().succ();
                        let mut days = 1;
                        if weekday == p.wkst {
                            days += 7 * (p.interval - 1);
                        }
                        while !byday.contains_key(&weekday) {
                            weekday = weekday.succ();
                            days += 1;
                            if weekday == p.wkst {
                                days += 7 * (p.interval - 1);
                            }
                        }
                        Duration::days(days)
                    }
                }
            }
            Freq::Monthly => {
                match &p.byday {
                    Some(byday) => {
                        let mut next = date;
                        if p.interval > 1 {
                            next = next.with_day(1).unwrap();
                            for _ in 1..p.interval {
                                next = next.add(Duration::days(next.days_in_month().into()));
                            }
                        }
                        loop {
                            next = next.add(Duration::days(1));
                            let (week, neg_week) = next.week_of_month();

                            match byday.get(&next.weekday()) {
                                Some(occurrences) =>
                                    if p.bysetpos == week || p.bysetpos == neg_week
                                            || occurrences.contains(&0)
                                            || occurrences.contains(&week) || occurrences.contains(&neg_week) {
                                        break;
                                    }
                                None => {}
                            };
                        };
                        next - date
                    }
                    None => {
                        let new_date = if date.month() == 12 {
                            date.with_month(1)
                                .unwrap()
                                .with_year(date.year() + 1)
                                .unwrap()
                        } else {
                            date.with_month(date.month() + 1).unwrap()
                        };
                        new_date - date
                    }
                }
            }
            // TODO: byday...
            Freq::Yearly => date.with_year(date.year() + 1).unwrap() - date,
        }
    }
}

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

fn parse_byday(s: &str) -> Result<Byday, EventError> {
    let mut byday = Byday::new();
    for v in s.split(",") {
        let weekday = parse_weekday(&v[v.len() - 2..])?;
        let occurrence = if v.len() > 2 {
            v[..v.len() - 2].parse()?
        } else {
            0
        };
        match byday.get_mut(&weekday) {
            Some(occurrences) => occurrences.push(occurrence),
            None => {
                byday.insert(weekday, vec![occurrence]);
            }
        };
    }
    Ok(byday)
}

fn parse_weekday(s: &str) -> Result<Weekday, EventError> {
    match s {
        "MO" => Ok(Weekday::Mon),
        "TU" => Ok(Weekday::Tue),
        "WE" => Ok(Weekday::Wed),
        "TH" => Ok(Weekday::Thu),
        "FR" => Ok(Weekday::Fri),
        "SA" => Ok(Weekday::Sat),
        "SU" => Ok(Weekday::Sun),
        _ => Err(EventError::BydayError),
    }
}