Debian Codegraph @

src/iter/rrule_iter.rs

raw symbols SCIP
1 use super::counter_date::DateTimeIter;
2 use super::utils::add_time_to_date;
3 use super::{build_pos_list, utils::date_from_ordinal, IterInfo, MAX_ITER_LOOP};
4 use crate::core::{get_hour, get_minute, get_second};
5 use crate::{Frequency, RRule, Tz};
6 use chrono::NaiveTime;
7 use std::collections::VecDeque;
8
9 #[derive(Debug, Clone)]
10 pub(crate) struct RRuleIter {
11 /// Date the iterator is currently at.
12 pub(crate) counter_date: DateTimeIter,
13 pub(crate) ii: IterInfo,
14 pub(crate) timeset: Vec<NaiveTime>,
15 pub(crate) dt_start: chrono::DateTime<Tz>,
16 /// Buffer of datetimes is not yet yielded
17 pub(crate) buffer: VecDeque<chrono::DateTime<Tz>>,
18 /// Indicate of iterator should not return more items.
19 /// Once set `true` is will always return `None`.
20 pub(crate) finished: bool,
21 /// Number of events that should still be generated before the end.
22 /// Counter always goes down after each iteration.
23 pub(crate) count: Option<u32>,
24 /// If the iterator should be using iterator limits.
25 pub(crate) limited: bool,
26 /// If the iterator has been stopped by the iterator limits.
27 pub(crate) was_limited: bool,
28 }
29
30 impl RRuleIter {
31 pub(crate) fn new(rrule: &RRule, dt_start: &chrono::DateTime<Tz>, limited: bool) -> Self {
32 let ii = IterInfo::new(rrule, dt_start);
33
34 let hour = get_hour(dt_start);
35 let minute = get_minute(dt_start);
36 let second = get_second(dt_start);
37 let timeset = ii.get_timeset(hour, minute, second);
38 let count = ii.rrule().count;
39
40 Self {
41 counter_date: dt_start.into(),
42 ii,
43 timeset,
44 dt_start: *dt_start,
45 buffer: VecDeque::new(),
46 finished: false,
47 count,
48 limited,
49 was_limited: false,
50 }
51 }
52
53 /// Attempts to add a date to the result. Returns `true` if we should
54 /// terminate the iteration.
55 fn try_add_datetime(
56 dt: chrono::DateTime<Tz>,
57 rrule: &RRule,
58 count: &mut Option<u32>,
59 buffer: &mut VecDeque<chrono::DateTime<Tz>>,
60 dt_start: &chrono::DateTime<Tz>,
61 ) -> bool {
62 if matches!(rrule.until, Some(until) if dt > until) {
63 // We can break because `pos_list` is sorted and
64 // all the next dates will only be larger than `until`.
65 return true;
66 }
67
68 if dt >= *dt_start {
69 buffer.push_back(dt);
70
71 if let Some(count) = count {
72 *count -= 1;
73
74 if *count == 0 {
75 return true;
76 }
77 }
78 }
79 false
80 }
81
82 /// Generates a list of dates that will be added to the buffer.
83 /// Returns true if finished, no more items should/can be returned.
84 fn generate(&mut self) -> bool {
85 // Do early check if done (if known)
86 if self.finished {
87 return true;
88 }
89 // Check if the count is set, and if 0
90 if matches!(self.count, Some(count) if count == 0) {
91 return true;
92 }
93
94 let rrule = self.ii.rrule();
95
96 if rrule.interval == 0 {
97 return true;
98 }
99
100 let mut loop_counter: u32 = 0;
101 // Loop until there is at least 1 item in the buffer.
102 while self.buffer.is_empty() {
103 // Prevent infinite loops
104 if self.limited {
105 loop_counter += 1;
106 if loop_counter >= MAX_ITER_LOOP {
107 self.finished = true;
108 self.was_limited = true;
109 log::warn!(
110 "Reached max loop counter (`{}`). \
111 See 'validator limits' in docs for more info.",
112 MAX_ITER_LOOP
113 );
114 return true;
115 }
116 }
117 let rrule = self.ii.rrule();
118
119 let dayset = self.ii.get_dayset(
120 rrule.freq,
121 self.counter_date.year,
122 self.counter_date.month,
123 self.counter_date.day,
124 );
125
126 let tz = self.dt_start.timezone();
127
128 if rrule.by_set_pos.is_empty() {
129 // Loop over `start..end`
130 for current_day in &dayset {
131 let current_day = i64::try_from(*current_day).expect(
132 "We control the dayset, and we know that it will always fit within an i64",
133 );
134 let year_ordinal = self.ii.year_ordinal();
135 // Ordinal conversion uses UTC: if we apply local-TZ here, then
136 // just below we'll end up double-applying.
137 let date = date_from_ordinal(year_ordinal + current_day);
138 for time in &self.timeset {
139 let Some(dt) = add_time_to_date(tz, date, *time) else {
140 continue;
141 };
142 if Self::try_add_datetime(
143 dt,
144 rrule,
145 &mut self.count,
146 &mut self.buffer,
147 &self.dt_start,
148 ) {
149 return true;
150 }
151 }
152 }
153 } else {
154 let pos_list = build_pos_list(
155 &rrule.by_set_pos,
156 &dayset,
157 &self.timeset,
158 self.ii.year_ordinal(),
159 self.dt_start.timezone(),
160 );
161 for dt in pos_list {
162 if Self::try_add_datetime(
163 dt,
164 rrule,
165 &mut self.count,
166 &mut self.buffer,
167 &self.dt_start,
168 ) {
169 return true;
170 }
171 }
172 }
173
174 let increment_day = dayset.is_empty();
175 if self.counter_date.increment(rrule, increment_day).is_err() {
176 self.finished = true;
177 return true;
178 }
179
180 if matches!(
181 rrule.freq,
182 Frequency::Hourly | Frequency::Minutely | Frequency::Secondly
183 ) {
184 let hour =
185 u8::try_from(self.counter_date.hour).expect("range 0-23 is covered by u8");
186 let minute =
187 u8::try_from(self.counter_date.minute).expect("range 0-59 is covered by u8");
188 let second =
189 u8::try_from(self.counter_date.second).expect("range 0-59 is covered by u8");
190 self.timeset = self.ii.get_timeset_unchecked(hour, minute, second);
191 }
192
193 self.ii.rebuild(&self.counter_date);
194 }
195
196 // Indicate that there might be more items on the next iteration.
197 false
198 }
199 }
200
201 impl Iterator for RRuleIter {
202 type Item = chrono::DateTime<Tz>;
203
204 fn next(&mut self) -> Option<Self::Item> {
205 if !self.buffer.is_empty() {
206 return self.buffer.pop_front();
207 }
208
209 if self.finished {
210 return None;
211 }
212
213 self.finished = self.generate();
214
215 if self.buffer.is_empty() {
216 self.finished = true;
217 }
218 self.buffer.pop_front()
219 }
220 }
221
222 pub(crate) trait WasLimited {
223 fn was_limited(&self) -> bool;
224 }
225
226 impl WasLimited for RRuleIter {
227 fn was_limited(&self) -> bool {
228 self.was_limited
229 }
230 }