summaryrefslogtreecommitdiff
path: root/src/iter/iter_v2.rs
blob: 609206d638a5a3990859ea52b2db61f932908d15 (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
use crate::{datetime::Time, Frequenzy};
use chrono::prelude::*;
use chrono_tz::Tz;

use super::{build_poslist, from_ordinal, increment_counter_date, remove_filtered_days, IterInfo};

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 {
            generate(self);
        }

        if self.remain.is_empty() {
            return None;
        }

        Some(self.remain.remove(0))
    }
}

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

    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 count > 0 {
                        count -= 1;
                        if count == 0 {
                            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);
                        // let (add, continue_iterator) = iter_result.accept(&res);
                        // if add {
                        //     iter.remain.push(res);
                        // }
                        // if !continue_iterator {
                        //     return;
                        // }

                        if count > 0 {
                            count -= 1;
                            if count == 0 {
                                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,
            );
        }

        iter.ii.rebuild(
            iter.counter_date.year() as isize,
            iter.counter_date.month() as usize,
        );
    }
}