summaryrefslogtreecommitdiff
path: root/src/rrule_iter.rs
blob: ad87576c24dff132694c78c230aeb1bf0619940f (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
use crate::iter::{
    build_poslist, increment_counter_date, make_timeset, remove_filtered_days, IterInfo,
};
use crate::{datetime::from_ordinal, RRule};
use crate::{datetime::Time, Frequenzy};
use chrono::prelude::*;
use chrono_tz::Tz;

pub struct RRuleIter {
    pub counter_date: DateTime<Tz>,
    pub ii: IterInfo,
    pub timeset: Vec<Time>,
    pub remain: Vec<DateTime<Tz>>,
    pub finished: bool,
}

impl Iterator for RRuleIter {
    type Item = DateTime<Tz>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.finished {
            return None;
        }

        if !self.remain.is_empty() {
            return Some(self.remain.remove(0));
        }

        generate(self);

        // println!("Done generating: {:?}", self.remain);

        if self.remain.is_empty() {
            self.finished = true;
            None
        } else {
            Some(self.remain.remove(0))
        }
    }
}

pub fn generate(iter: &mut RRuleIter) {
    let options = iter.ii.options.clone();

    match options.count {
        Some(count) if count == 0 => return,
        _ => (),
    };

    while iter.remain.is_empty() {
        let (dayset, start, end) = iter.ii.getdayset(
            &iter.ii.options.freq,
            iter.counter_date.year() as isize,
            iter.counter_date.month() as usize,
            iter.counter_date.day() as usize,
        );

        let mut dayset = dayset
            .into_iter()
            .map(|s| Some(s as isize))
            .collect::<Vec<Option<isize>>>();

        let filtered = remove_filtered_days(&mut dayset, start, end, &iter.ii);

        if options.bysetpos.len() > 0 {
            let poslist = build_poslist(
                &options.bysetpos,
                &iter.timeset,
                start,
                end,
                &iter.ii,
                &dayset,
                &options.tzid,
            );

            for j in 0..poslist.len() {
                let res = poslist[j];
                if options.until.is_some() && res > options.until.unwrap() {
                    // return iter_result.get_value();
                    continue; // or break ?
                }

                if res >= options.dtstart {
                    iter.remain.push(res);

                    if let Some(count) = iter.ii.options.count {
                        if count > 0 {
                            iter.ii.options.count = Some(count - 1);
                        }
                        // This means that the real count is 0, because of the decrement above
                        if count == 1 {
                            return;
                        }
                    }
                }
            }
        } else {
            for j in start..end {
                let current_day = dayset[j];
                if current_day.is_none() {
                    continue;
                }

                let current_day = current_day.unwrap();
                let date =
                    from_ordinal(iter.ii.yearordinal().unwrap() + current_day, &options.tzid);
                for k in 0..iter.timeset.len() {
                    let res = options
                        .tzid
                        .ymd(date.year(), date.month(), date.day())
                        .and_hms(
                            iter.timeset[k].hour as u32,
                            iter.timeset[k].minute as u32,
                            iter.timeset[k].second as u32,
                        );
                    if options.until.is_some() && res > options.until.unwrap() {
                        return;
                    }
                    if res >= options.dtstart {
                        iter.remain.push(res);

                        if let Some(count) = iter.ii.options.count {
                            if count > 0 {
                                iter.ii.options.count = Some(count - 1);
                            }
                            // This means that the real count is 0, because of the decrement above
                            if count == 1 {
                                return;
                            }
                        }
                    }
                }
            }
        }

        if options.interval == 0 {
            return;
        }

        // Handle frequency and interval
        iter.counter_date = increment_counter_date(iter.counter_date, &options, filtered);

        if iter.counter_date.year() > 2200 {
            return;
        }

        if options.freq == Frequenzy::Hourly
            || options.freq == Frequenzy::Minutely
            || options.freq == Frequenzy::Secondly
        {
            iter.timeset = iter.ii.gettimeset(
                &options.freq,
                iter.counter_date.hour() as usize,
                iter.counter_date.minute() as usize,
                iter.counter_date.second() as usize,
                0,
            );
        }

        let year = iter.counter_date.year();
        let month = iter.counter_date.month();

        iter.ii.rebuild(year as isize, month as usize);
    }
}
impl IntoIterator for RRule {
    type Item = DateTime<Tz>;

    type IntoIter = RRuleIter;

    fn into_iter(self) -> Self::IntoIter {
        let mut ii = IterInfo::new(self.options);
        let counter_date = ii.options.dtstart;
        ii.rebuild(counter_date.year() as isize, counter_date.month() as usize);

        let timeset = make_timeset(&ii, &counter_date, &ii.options);

        RRuleIter {
            counter_date,
            ii,
            timeset,
            remain: vec![],
            finished: false,
        }
    }
}