summaryrefslogtreecommitdiff
path: root/src/options.rs
blob: 2cb017b4d22ff7985766479bdc507047c6653a13 (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
use chrono::prelude::*;
use chrono_tz::Tz;
use crate::datetime::{DTime, get_weekday_val};
use crate::parse_options::parse_options;

#[derive(Debug, PartialEq, PartialOrd, Clone)]
pub enum Frequenzy {
    Yearly = 0,
    Monthly = 1,
    Weekly = 2,
    Daily = 3,
    Hourly = 4,
    Minutely = 5,
    Secondly = 6,
}

#[derive(Debug, Clone)]
pub struct ParsedOptions {
    pub freq: Frequenzy,
    pub interval: usize,
    pub count: Option<u32>,
    pub until: Option<DTime>,
    pub tzid: Tz,
    pub dtstart: DTime,
    pub wkst: usize,
    pub bysetpos: Vec<isize>,
    pub bymonth: Vec<usize>,
    pub bymonthday: Vec<isize>,
    pub bynmonthday: Vec<isize>,
    pub byyearday: Vec<isize>,
    pub byweekno: Vec<isize>,
    pub byweekday: Vec<usize>,
    pub byhour: Vec<usize>,
    pub byminute: Vec<usize>,
    pub bysecond: Vec<usize>,
    pub bynweekday: Vec<Vec<isize>>,
    pub byeaster: Option<isize>,
}

// TODO: PartialOptions shouldnt have all of these fields
#[derive(Debug, Clone)]
pub struct Options {
    pub freq: Option<Frequenzy>,
    pub interval: Option<usize>,
    pub count: Option<u32>,
    pub until: Option<DTime>,
    pub tzid: Option<Tz>,
    pub dtstart: Option<DTime>,
    pub wkst: Option<usize>,
    pub bysetpos: Option<Vec<isize>>,
    pub bymonth: Option<Vec<usize>>,
    pub bymonthday: Option<Vec<isize>>,
    pub bynmonthday: Option<Vec<isize>>,
    pub byyearday: Option<Vec<isize>>,
    pub byweekno: Option<Vec<isize>>,
    pub byweekday: Option<Vec<usize>>,
    pub byhour: Option<Vec<usize>>,
    pub byminute: Option<Vec<usize>>,
    pub bysecond: Option<Vec<usize>>,
    pub bynweekday: Option<Vec<Vec<isize>>>,
    pub byeaster: Option<isize>,
}

impl Options {
    pub fn new() -> Self {
        Self {
            freq: None,
            interval: None,
            count: None,
            until: None,
            tzid: None,
            dtstart: None,
            wkst: None,
            bysetpos: None,
            bymonth: None,
            bymonthday: None,
            bynmonthday: None,
            byyearday: None,
            byweekno: None,
            byweekday: None,
            byhour: None,
            byminute: None,
            bysecond: None,
            bynweekday: None,
            byeaster: None,
        }
    }

    // TODO: better name
    fn is_some_or_none<'a, T>(prop1: &'a Option<T>, prop2: &'a Option<T>) -> &'a Option<T> {
        if prop2.is_some() {
            return prop2;
        } 
        prop1
    }

    pub fn concat(opt1: &Self, opt2: &Self) -> Self {
        Self {
            freq: Self::is_some_or_none(&opt1.freq, &opt2.freq).clone(),
            interval: Self::is_some_or_none(&opt1.interval, &opt2.interval).clone(),
            count: Self::is_some_or_none(&opt1.count, &opt2.count).clone(),
            until: Self::is_some_or_none(&opt1.until, &opt2.until).clone(),
            tzid: Self::is_some_or_none(&opt1.tzid, &opt2.tzid).clone(),
            dtstart: Self::is_some_or_none(&opt1.dtstart, &opt2.dtstart).clone(),
            wkst: Self::is_some_or_none(&opt1.wkst, &opt2.wkst).clone(),
            bysetpos: Self::is_some_or_none(&opt1.bysetpos, &opt2.bysetpos).clone(),
            bymonth: Self::is_some_or_none(&opt1.bymonth, &opt2.bymonth).clone(),
            bymonthday: Self::is_some_or_none(&opt1.bymonthday, &opt2.bymonthday).clone(),
            bynmonthday: Self::is_some_or_none(&opt1.bynmonthday, &opt2.bynmonthday).clone(),
            byyearday: Self::is_some_or_none(&opt1.byyearday, &opt2.byyearday).clone(),
            byweekno: Self::is_some_or_none(&opt1.byweekno, &opt2.byweekno).clone(),
            byweekday: Self::is_some_or_none(&opt1.byweekday, &opt2.byweekday).clone(),
            byhour: Self::is_some_or_none(&opt1.byhour, &opt2.byhour).clone(),
            byminute: Self::is_some_or_none(&opt1.byminute, &opt2.byminute).clone(),
            bysecond: Self::is_some_or_none(&opt1.bysecond, &opt2.bysecond).clone(),
            bynweekday: Self::is_some_or_none(&opt1.bynweekday, &opt2.bynweekday).clone(),
            byeaster: Self::is_some_or_none(&opt1.byeaster, &opt2.byeaster).clone(),
        }
    }

    pub fn freq(mut self, freq: Frequenzy) -> Self {
        self.freq = Some(freq);
        self
    }

    pub fn interval(mut self, interval: usize) -> Self {
        self.interval = Some(interval);
        self
    }

    pub fn count(mut self, count: u32) -> Self {
        self.count = Some(count);
        self
    }

    pub fn until(mut self, until: DTime) -> Self {
        self.until = Some(until);
        self
    }

    pub fn dtstart(mut self, dtstart: DTime) -> Self {
        self.dtstart = Some(dtstart);
        self.tzid = Some(dtstart.timezone());
        self
    }

    pub fn wkst(mut self, wkst: Weekday) -> Self {
        self.wkst = Some(get_weekday_val(&wkst));
        self
    }

    pub fn bysetpos(mut self, bysetpos: Vec<isize>) -> Self {
        self.bysetpos = Some(bysetpos);
        self
    }

    pub fn bymonth(mut self, bymonth: Vec<usize>) -> Self {
        self.bymonth = Some(bymonth);
        self
    }

    pub fn bymonthday(mut self, bymonthday: Vec<isize>) -> Self {
        self.bymonthday = Some(bymonthday);
        self
    }

    pub fn byyearday(mut self, byyearday: Vec<isize>) -> Self {
        self.byyearday = Some(byyearday);
        self
    }

    pub fn byweekno(mut self, byweekno: Vec<isize>) -> Self {
        self.byweekno = Some(byweekno);
        self
    }

    pub fn byweekday(mut self, byweekday: Vec<Weekday>) -> Self {
        let byweekday = byweekday.iter().map(|w| get_weekday_val(w)).collect(); 
        self.byweekday = Some(byweekday);
        self
    }

    pub fn byhour(mut self, byhour: Vec<usize>) -> Self {
        self.byhour = Some(byhour);
        self
    }

    pub fn byminute(mut self, byminute: Vec<usize>) -> Self {
        self.byminute = Some(byminute);
        self
    }

    pub fn bysecond(mut self, bysecond: Vec<usize>) -> Self {
        self.bysecond = Some(bysecond);
        self
    }

    pub fn byeaster(mut self, byeaster: isize) -> Self {
        self.byeaster = Some(byeaster);
        self
    }

    pub fn build(self) -> ParsedOptions {
        parse_options(&self)
    }
}