summaryrefslogtreecommitdiff
path: root/src/rruleset_iter.rs
blob: ba8e30509d1a3f2c7b86acab7b5695dd41e90ac1 (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
use crate::iter::{iter, IterResult};
use chrono::prelude::*;
use crate::rruleset::RRuleSet;
use crate::rrule_iter::*;
use std::collections::HashMap;
use chrono_tz::{Tz, UTC};

pub struct RRuleSetIter<'a> {
    exdate_hash: HashMap<i64, ()>,
    iter_res: RRuleIterRes,
    rrule_set: &'a mut RRuleSet,
}

impl<'a> RRuleSetIter<'a> {
    pub fn all(rrule_set: &'a mut RRuleSet) -> Self {
        let iter_args = IterArgs {
            inc: true,
            before: None,
            after: None,
            dt: None,
        };
        let iter_res = RRuleIterRes::new(QueryMethodTypes::All, iter_args);

        Self {
            exdate_hash: HashMap::new(),
            iter_res,
            rrule_set,
        }
    }

    pub fn before(rrule_set: &'a mut RRuleSet, dt: DateTime<Tz>, inc: bool) -> Self {
        let iter_args = IterArgs {
            inc,
            before: None,
            after: None,
            dt: Some(dt),
        };
        let iter_res = RRuleIterRes::new(QueryMethodTypes::Before, iter_args);

        Self {
            exdate_hash: HashMap::new(),
            iter_res,
            rrule_set,
        }
    }

    pub fn after(rrule_set: &'a mut RRuleSet, dt: DateTime<Tz>, inc: bool) -> Self {
        let iter_args = IterArgs {
            inc,
            before: None,
            after: None,
            dt: Some(dt),
        };
        let iter_res = RRuleIterRes::new(QueryMethodTypes::After, iter_args);

        Self {
            exdate_hash: HashMap::new(),
            iter_res,
            rrule_set,
        }
    }

    pub fn between(
        rrule_set: &'a mut RRuleSet,
        after: DateTime<Tz>,
        before: DateTime<Tz>,
        inc: bool,
    ) -> Self {
        let iter_args = IterArgs {
            inc,
            before: Some(before),
            after: Some(after),
            dt: None,
        };
        let iter_res = RRuleIterRes::new(QueryMethodTypes::Between, iter_args);

        Self {
            exdate_hash: HashMap::new(),
            iter_res,
            rrule_set,
        }
    }

    pub fn eval_exdate(&mut self, after: &DateTime<Tz>, before: &DateTime<Tz>) {
        for rrule in self.rrule_set.exrule.iter_mut() {
            for date in rrule.between(after.clone(), before.clone(), true) {
                self.exdate_hash.insert(date.timestamp(), ());
            }
        }
    }

    fn accept_when_unknown_bounds(&mut self, date: DateTime<Tz>) -> bool {
        let dt = date.timestamp();
        if !self.exdate_hash.contains_key(&dt) {
            self.eval_exdate(&date.timezone().timestamp(dt - 1, 0), &date.timezone().timestamp(dt + 1, 0));
            if !self.exdate_hash.contains_key(&dt) {
                self.exdate_hash.insert(dt, ());
                return self.iter_res.accept(date.clone());
            }
        }

        true
    }

    fn accept_when_known_bounds(&mut self, date: DateTime<Tz>) -> bool {
        let dt = date.timestamp();
        if !self.exdate_hash.contains_key(&dt) {
            self.exdate_hash.insert(dt, ());
            return self.iter_res.accept(date.clone());
        }

        true
    }

    pub fn iter(&mut self, tzid: Option<String>) -> Vec<DateTime<Tz>> {
        let tzid: Tz = tzid.unwrap_or(String::from("UTC")).parse().unwrap_or(UTC);

        for date in &self.rrule_set.exdate {
            let zoned_date = date.with_timezone(&tzid);
            self.exdate_hash.insert(zoned_date.timestamp(), ());
        }

        match &self.iter_res.method {
            QueryMethodTypes::Between => {
                self.eval_exdate(
                    &self.iter_res.args.after.unwrap().clone(),
                    &self.iter_res.args.before.unwrap().clone(),
                );
            }
            _ => (),
        };

        for date in &self.rrule_set.rdate.clone() {
            let zoned_date = date.with_timezone(&tzid);
            if !self.accept(zoned_date) {
                break;
            }
        }

        for rule in self.rrule_set.rrule.clone().iter_mut() {
            iter(self, &mut rule.options);
        }

        let mut res = self.iter_res.get_value();
        res.sort();
        res
    }
}

impl<'a> IterResult for RRuleSetIter<'a> {
    fn accept(&mut self, date: DateTime<Tz>) -> bool {
        match &self.iter_res.method {
            QueryMethodTypes::Between => self.accept_when_known_bounds(date),
            _ => self.accept_when_unknown_bounds(date),
        }
    }

    fn get_value(&self) -> Vec<DateTime<Tz>> {
        self.iter_res.get_value()
    }
}