Debian Codegraph @

src/iter/iterinfo.rs

raw symbols SCIP
1 use super::counter_date::DateTimeIter;
2 #[cfg(feature = "by-easter")]
3 use super::easter::easter;
4 use super::{monthinfo::MonthInfo, yearinfo::YearInfo};
5 use crate::core::get_month;
6 use crate::{Frequency, NWeekday, RRule, Tz};
7 use chrono::{Datelike, NaiveTime, TimeZone};
8
9 #[derive(Debug, Clone)]
10 pub(crate) struct IterInfo {
11 year_info: YearInfo,
12 month_info: Option<MonthInfo>,
13 easter_mask: Option<Vec<i32>>,
14 rrule: RRule,
15 }
16
17 impl IterInfo {
18 pub fn new(rrule: &RRule, dt_start: &chrono::DateTime<Tz>) -> Self {
19 let year = dt_start.year();
20 let month = get_month(dt_start);
21
22 let year_info = YearInfo::new(year, rrule);
23 let mut ii = Self {
24 rrule: rrule.clone(),
25 year_info,
26 month_info: None,
27 easter_mask: None,
28 };
29 ii.rebuild_inner(year, month, true);
30
31 ii
32 }
33
34 fn rebuild_inner(&mut self, year: i32, month: u8, skip_year_info: bool) {
35 if !skip_year_info
36 && !matches!(&self.month_info, Some(month_info) if month_info.last_year == year)
37 {
38 self.year_info = YearInfo::new(year, &self.rrule);
39 }
40
41 let contains_nth_by_weekday = self
42 .rrule
43 .by_weekday
44 .iter()
45 .any(|by_weekday| matches!(by_weekday, NWeekday::Nth(_, _)));
46
47 if contains_nth_by_weekday
48 && !(matches!(&self.month_info, Some(month_info) if month_info.last_month == month && month_info.last_year == year))
49 {
50 let new_month_info = MonthInfo::new(&self.year_info, month, &self.rrule);
51 self.month_info = Some(new_month_info);
52 }
53
54 #[cfg(feature = "by-easter")]
55 {
56 self.easter_mask = self
57 .rrule
58 .by_easter
59 .map(|by_easter| vec![easter(year, by_easter)]);
60 }
61 }
62
63 pub fn rebuild(&mut self, counter_date: &DateTimeIter) {
64 let month = u8::try_from(counter_date.month).expect("range 1-12 is covered by u8");
65 self.rebuild_inner(counter_date.year, month, false);
66 }
67
68 pub fn year_len(&self) -> u16 {
69 self.year_info.year_len
70 }
71
72 pub fn year_ordinal(&self) -> i64 {
73 self.year_info.year_ordinal
74 }
75
76 pub fn month_range(&self) -> &[u16] {
77 self.year_info.month_range
78 }
79
80 pub fn easter_mask(&self) -> Option<&Vec<i32>> {
81 self.easter_mask.as_ref()
82 }
83
84 pub fn weekday_mask(&self) -> &[u32] {
85 self.year_info.weekday_mask
86 }
87
88 pub fn month_mask(&self) -> &[u8] {
89 self.year_info.month_mask
90 }
91
92 pub fn week_no_mask(&self) -> Option<&Vec<u8>> {
93 self.year_info.week_no_mask.as_ref()
94 }
95
96 pub fn neg_weekday_mask(&self) -> Option<&Vec<u8>> {
97 self.month_info.as_ref().map(|info| &info.neg_weekday_mask)
98 }
99
100 pub fn next_year_len(&self) -> u16 {
101 self.year_info.next_year_len
102 }
103
104 pub fn month_day_mask(&self) -> &[i8] {
105 self.year_info.month_day_mask
106 }
107
108 pub fn neg_month_day_mask(&self) -> &[i8] {
109 self.year_info.neg_month_day_mask
110 }
111
112 pub fn year_dayset(&self) -> Vec<usize> {
113 let year_len = usize::from(self.year_len());
114 (0..year_len).collect()
115 }
116
117 pub fn month_dayset(&self, month: u32) -> Vec<usize> {
118 let month_range = self.month_range();
119 let month = usize::try_from(month).expect("target arch should have at least 32 bits");
120 let start = usize::from(month_range[month - 1]);
121 let end = usize::from(month_range[month]);
122 (start..end).collect()
123 }
124
125 pub fn weekday_set(&self, year: i32, month: u32, day: u32) -> Vec<usize> {
126 let set_len = usize::from(self.year_len() + 7);
127
128 let mut date_ordinal = usize::try_from(
129 chrono::Utc
130 .with_ymd_and_hms(year, month, day, 0, 0, 0)
131 .unwrap()
132 .ordinal0(),
133 )
134 .expect("target arch should have at least 32 bits");
135
136 let mut set = vec![];
137
138 let week_start_num_days_from_monday = self.rrule.week_start.num_days_from_monday();
139
140 for _ in 0..7 {
141 if date_ordinal >= set_len {
142 break;
143 }
144 set.push(date_ordinal);
145 date_ordinal += 1;
146 if self.weekday_mask()[date_ordinal] == week_start_num_days_from_monday {
147 break;
148 }
149 }
150
151 set
152 }
153
154 pub fn day_dayset(year: i32, month: u32, day: u32) -> Vec<usize> {
155 let date_ordinal = chrono::Utc
156 .with_ymd_and_hms(year, month, day, 0, 0, 0)
157 .unwrap()
158 .ordinal0();
159
160 vec![usize::try_from(date_ordinal).expect("target arch should have at least 32 bits")]
161 }
162
163 pub fn hour_timeset(&self, hour: u8) -> Vec<NaiveTime> {
164 self.rrule
165 .by_minute
166 .iter()
167 .flat_map(|minute| self.min_timeset(hour, *minute))
168 .collect()
169 }
170
171 pub fn min_timeset(&self, hour: u8, minute: u8) -> Vec<NaiveTime> {
172 self.rrule
173 .by_second
174 .iter()
175 .filter_map(|second| {
176 NaiveTime::from_hms_opt(u32::from(hour), u32::from(minute), u32::from(*second))
177 })
178 .collect()
179 }
180
181 pub fn sec_timeset(hour: u8, minute: u8, second: u8) -> Vec<NaiveTime> {
182 if let Some(time) =
183 NaiveTime::from_hms_opt(u32::from(hour), u32::from(minute), u32::from(second))
184 {
185 vec![time]
186 } else {
187 vec![]
188 }
189 }
190
191 pub fn get_dayset(&self, freq: Frequency, year: i32, month: u32, day: u32) -> Vec<usize> {
192 let mut dayset = match freq {
193 Frequency::Yearly => self.year_dayset(),
194 Frequency::Monthly => self.month_dayset(month),
195 Frequency::Weekly => self.weekday_set(year, month, day),
196 _ => Self::day_dayset(year, month, day),
197 };
198
199 // Filter out days according to the RRule filters.
200 dayset.retain(|day| !super::filters::is_filtered(self, *day));
201
202 dayset
203 }
204
205 /// Gets a timeset without checking if the hour, minute and second are valid, according
206 /// to the `RRule`.
207 ///
208 /// This is usually called after calling the `increment_counter_date` where we know
209 /// that we get a valid `DateTime` back, and there is no need to do any duplicate
210 /// validation.
211 pub fn get_timeset_unchecked(&self, hour: u8, minute: u8, second: u8) -> Vec<NaiveTime> {
212 match self.rrule.freq {
213 Frequency::Hourly => self.hour_timeset(hour),
214 Frequency::Minutely => self.min_timeset(hour, minute),
215 Frequency::Secondly => Self::sec_timeset(hour, minute, second),
216 _ => unreachable!(
217 "This method is never called with an invalid frequency and is not publicly exposed"
218 ),
219 }
220 }
221
222 /// Gets a timeset.
223 ///
224 /// An empty set is returned if the hour, minute and second aren't valid,
225 /// according to the `RRule`.
226 pub fn get_timeset(&self, hour: u8, minute: u8, second: u8) -> Vec<NaiveTime> {
227 match self.rrule.freq {
228 Frequency::Hourly | Frequency::Minutely | Frequency::Secondly => {
229 let incorrect_hour = self.rrule.freq >= Frequency::Hourly
230 && !self.rrule.by_hour.is_empty()
231 && !self.rrule.by_hour.contains(&hour);
232 let incorrect_minute = self.rrule.freq >= Frequency::Minutely
233 && !self.rrule.by_minute.is_empty()
234 && !self.rrule.by_minute.contains(&minute);
235 let incorrect_second = self.rrule.freq >= Frequency::Secondly
236 && !self.rrule.by_second.is_empty()
237 && !self.rrule.by_second.contains(&second);
238 let date_is_not_a_candidate =
239 incorrect_hour || incorrect_minute || incorrect_second;
240
241 // If date is not a potential candidate, then we return an empty timeset.
242 if date_is_not_a_candidate {
243 return vec![];
244 }
245
246 self.get_timeset_unchecked(hour, minute, second)
247 }
248 _ => {
249 let timeset = self
250 .rrule
251 .by_hour
252 .iter()
253 .flat_map(|hour| {
254 self.rrule.by_minute.iter().flat_map(move |minute| {
255 self.rrule.by_second.iter().filter_map(move |second| {
256 NaiveTime::from_hms_opt(
257 u32::from(*hour),
258 u32::from(*minute),
259 u32::from(*second),
260 )
261 })
262 })
263 })
264 .collect();
265
266 timeset
267 }
268 }
269 }
270
271 pub fn rrule(&self) -> &RRule {
272 &self.rrule
273 }
274 }