summaryrefslogtreecommitdiff
path: root/src/parse_options.rs
blob: 795388710300c1f5a8d8591217780ae3f9c0ea0c (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
use crate::options::{Frequenzy, NWeekday, NWeekdayIdentifier, Options, ParsedOptions, RRuleParseError};
use crate::utils::is_some_and_not_empty;
use chrono::prelude::*;
use chrono_tz::{Tz, UTC};

// TODO: More validation here
pub fn parse_options(options: &Options) -> Result<ParsedOptions, RRuleParseError> {
    let mut default_partial_options = Options::new();
    default_partial_options.interval = Some(1);
    default_partial_options.freq = Some(Frequenzy::Yearly);
    default_partial_options.wkst = Some(0);

    let tzid: Tz = if options.tzid.is_some() {
        options.tzid.clone().unwrap()
    } else {
        UTC
    };

    let mut bynweekday: Vec<Vec<isize>> = vec![];
    let mut bynmonthday: Vec<isize> = vec![];

    let mut partial_options = Options::concat(&default_partial_options, options);

    if partial_options.byeaster.is_some() {
        partial_options.freq = Some(Frequenzy::Yearly);
    }
    let freq = partial_options.freq.unwrap_or(Frequenzy::Daily);

    if partial_options.dtstart.is_none() {
        return Err(RRuleParseError(String::from("Dtstart can not be None")));
    }

    if partial_options.wkst.is_none() {
        partial_options.wkst = Some(0);
    }

    if let Some(bysetpos) = &partial_options.bysetpos {
        for pos in bysetpos {
            if *pos == 0 || !(*pos >= -366 && *pos <= 366) {
                return Err(RRuleParseError(String::from(
                    "Bysetpos must be between 1 and 366, or between -366 and -1",
                )));
            }
        }
    }

    let dtstart = if partial_options.dtstart.is_some() {
        partial_options.dtstart.unwrap()
    } else {
        return Err(RRuleParseError(String::from("Dtstart was not specified")));
    };

    if !(partial_options.byweekno.is_some()
        || is_some_and_not_empty(&partial_options.byweekno)
        || is_some_and_not_empty(&partial_options.byyearday)
        || partial_options.bymonthday.is_some()
        || is_some_and_not_empty(&partial_options.bymonthday)
        || partial_options.byweekday.is_some()
        || partial_options.byeaster.is_some())
    {
        match &freq {
            Frequenzy::Yearly => {
                if partial_options.bymonth.is_none() {
                    partial_options.bymonth = Some(vec![dtstart.month() as usize]);
                }
                partial_options.bymonthday = Some(vec![dtstart.day() as isize]);
            }
            Frequenzy::Monthly => {
                partial_options.bymonthday = Some(vec![dtstart.day() as isize]);
            }
            Frequenzy::Weekly => {
                partial_options.byweekday =
                    Some(vec![NWeekday::new(dtstart.weekday() as usize, NWeekdayIdentifier::Every)]);
            }
            _ => (),
        };
    }

    match &partial_options.bymonthday {
        None => bynmonthday = vec![],
        Some(opts_bymonthday) => {
            let mut bymonthday = vec![];

            for v in opts_bymonthday {
                if *v > 0 {
                    bymonthday.push(*v);
                } else if *v < 0 {
                    bynmonthday.push(*v);
                }
            }

            partial_options.bymonthday = Some(bymonthday);
        }
    }

    let mut byweekday = vec![];
    let mut bynweekday: Vec<Vec<isize>> = vec![];
    // byweekday / bynweekday // ! more to do here

    if let Some(opts_byweekday) = partial_options.byweekday {
        for wday in opts_byweekday {
            match wday.n {
                NWeekdayIdentifier::Every => {
                byweekday.push(wday.weekday)
                }
                NWeekdayIdentifier::Identifier(n) => {
                    bynweekday.push(vec![wday.weekday as isize, n]);
                }
            }
            // if wday.n ==  {
            //     byweekday.push(wday.weekday);
            // } else {
            //     bynweekday.push(vec![wday.weekday as isize, wday.n]);
            // }
        }
    }

    // byhour
    if partial_options.byhour.is_none() && freq < Frequenzy::Hourly {
        partial_options.byhour = Some(vec![dtstart.hour() as usize]);
    }

    // byminute
    if partial_options.byminute.is_none() && freq < Frequenzy::Minutely {
        partial_options.byminute = Some(vec![dtstart.minute() as usize]);
    }

    // bysecond
    if partial_options.bysecond.is_none() && freq < Frequenzy::Secondly {
        partial_options.bysecond = Some(vec![dtstart.second() as usize]);
    }

    Ok(ParsedOptions {
        freq,
        interval: partial_options.interval.unwrap(),
        count: partial_options.count,
        until: partial_options.until,
        tzid,
        dtstart,
        wkst: partial_options.wkst.unwrap(),
        bysetpos: partial_options.bysetpos.unwrap_or(vec![]),
        bymonth: partial_options.bymonth.unwrap_or(vec![]),
        bymonthday: partial_options.bymonthday.unwrap_or(vec![]),
        bynmonthday,
        byyearday: partial_options.byyearday.unwrap_or(vec![]),
        byweekno: partial_options.byweekno.unwrap_or(vec![]),
        byweekday,
        bynweekday,
        byhour: partial_options.byhour.unwrap_or(vec![]),
        byminute: partial_options.byminute.unwrap_or(vec![]),
        bysecond: partial_options.bysecond.unwrap_or(vec![]),
        byeaster: partial_options.byeaster,
    })
}