ical_rust 0.8.6

A lightweight, efficient, and robust iCalendar (ics) parser and recurrence expander library
Documentation
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use chrono::{Datelike, Duration, NaiveDate, TimeZone, Timelike, Utc};
use std::ops::Range;

use crate::{VEvent, date_or_date_time::DateOrDateTime, frequency::Frequency, rrule::RRule};

#[derive(Debug, Clone)]
pub struct VEventIterator<'a> {
    event: &'a VEvent,
    last_occurrence: Option<DateOrDateTime>,
    count: u32,
    rdate_idx: usize,
    peeked_rrule: Option<DateOrDateTime>,
    rrule_exhausted: bool,
}

impl<'a> VEventIterator<'a> {
    pub(crate) fn new(event: &'a VEvent) -> Self {
        Self {
            event,
            last_occurrence: None,
            count: 0,
            rdate_idx: 0,
            peeked_rrule: None,
            rrule_exhausted: false,
        }
    }

    fn to_wall_clock(dt: DateOrDateTime) -> DateOrDateTime {
        match dt {
            DateOrDateTime::DateTime { utc, tz: Some(tz) } => {
                let local = utc.with_timezone(&tz).naive_local();
                DateOrDateTime::DateTime {
                    utc: Utc.from_utc_datetime(&local),
                    tz: Some(tz),
                }
            }
            _ => dt,
        }
    }

    fn from_wall_clock(dt: DateOrDateTime) -> DateOrDateTime {
        match dt {
            DateOrDateTime::DateTime {
                utc: wall_dt,
                tz: Some(tz),
            } => {
                let naive = wall_dt.naive_utc();
                let utc_dt = tz
                    .from_local_datetime(&naive)
                    .earliest()
                    .map(|d| d.with_timezone(&Utc))
                    .unwrap_or(wall_dt);
                DateOrDateTime::DateTime {
                    utc: utc_dt,
                    tz: Some(tz),
                }
            }
            _ => dt,
        }
    }

    fn with_year_month_clamped_day(
        base: DateOrDateTime,
        year: i32,
        month: u32,
        month_day: i8,
        default_day: u32,
    ) -> DateOrDateTime {
        let mut max_day = 31;
        while NaiveDate::from_ymd_opt(year, month, max_day).is_none() && max_day > 1 {
            max_day -= 1;
        }
        let d = if month_day == 0 {
            default_day.min(max_day)
        } else if month_day > 0 {
            (month_day as u32).min(max_day)
        } else {
            (max_day as i32 + 1 + month_day as i32).max(1) as u32
        };
        let target_date = NaiveDate::from_ymd_opt(year, month, d).unwrap();
        match base {
            DateOrDateTime::WholeDay(_) => DateOrDateTime::WholeDay(target_date),
            DateOrDateTime::DateTime { utc, tz } => DateOrDateTime::DateTime {
                utc: target_date
                    .and_hms_opt(utc.hour(), utc.minute(), utc.second())
                    .unwrap()
                    .and_utc(),
                tz,
            },
        }
    }

    fn apply_by_set_pos(
        candidates: Vec<DateOrDateTime>,
        by_set_pos: &[i16],
    ) -> Vec<DateOrDateTime> {
        if by_set_pos.is_empty() {
            return candidates;
        }
        let len = candidates.len() as i32;
        let mut result = Vec::new();
        for &pos in by_set_pos {
            let idx = if pos > 0 {
                pos as i32 - 1
            } else {
                len + pos as i32
            };
            if idx >= 0 && idx < len {
                result.push(candidates[idx as usize]);
            }
        }
        result.sort_unstable();
        result.dedup();
        result
    }

    fn candidates_in_month(
        base: DateOrDateTime,
        year: i32,
        month: u32,
        rrule: &RRule,
        default_day: u32,
    ) -> Vec<DateOrDateTime> {
        let month_day_candidates: Option<Vec<DateOrDateTime>> = if rrule.by_month_day.is_empty() {
            None
        } else {
            let mut v: Vec<DateOrDateTime> = rrule
                .by_month_day
                .iter()
                .map(|&md| Self::with_year_month_clamped_day(base, year, month, md, default_day))
                .collect();
            v.sort_unstable();
            v.dedup();
            Some(v)
        };

        let by_day_candidates: Option<Vec<DateOrDateTime>> = if rrule.by_day.is_empty() {
            None
        } else {
            Some(base.matching_days_in_month(year, month, &rrule.by_day))
        };

        match (month_day_candidates, by_day_candidates) {
            (Some(md), Some(bd)) => md.into_iter().filter(|d| bd.contains(d)).collect(),
            (Some(md), None) => md,
            (None, Some(bd)) => bd,
            (None, None) => vec![Self::with_year_month_clamped_day(
                base,
                year,
                month,
                0,
                default_day,
            )],
        }
    }

    fn candidates_in_year(
        base: DateOrDateTime,
        year: i32,
        rrule: &RRule,
        default_month: u32,
        default_day: u32,
    ) -> Vec<DateOrDateTime> {
        let mut months: Vec<u32> = if rrule.by_month.is_empty() {
            vec![default_month]
        } else {
            rrule.by_month.iter().map(|&m| m as u32).collect()
        };
        months.sort_unstable();
        months.dedup();

        let mut all = Vec::new();
        for m in months {
            all.extend(Self::candidates_in_month(base, year, m, rrule, default_day));
        }
        all.sort_unstable();
        all.dedup();
        Self::apply_by_set_pos(all, &rrule.by_set_pos)
    }

    fn get_next_occurrence_according_to_rule(
        &mut self,
        last_occurrence: DateOrDateTime,
        rrule: &RRule,
    ) -> Option<DateOrDateTime> {
        let last_occurrence = Self::to_wall_clock(last_occurrence);
        let dt_start_wall = Self::to_wall_clock(self.event.dt_start);
        let interval = rrule.interval.max(1);

        let wall_next = match rrule.frequency {
            Frequency::Yearly => {
                let current_year_candidates = Self::candidates_in_year(
                    last_occurrence,
                    last_occurrence.year(),
                    rrule,
                    dt_start_wall.month(),
                    dt_start_wall.day(),
                );
                if let Some(&next_in_year) = current_year_candidates
                    .iter()
                    .find(|&&d| d > last_occurrence)
                {
                    next_in_year
                } else {
                    let mut year = last_occurrence.year() + interval as i32;
                    loop {
                        let year_candidates = Self::candidates_in_year(
                            last_occurrence,
                            year,
                            rrule,
                            dt_start_wall.month(),
                            dt_start_wall.day(),
                        );
                        if let Some(&first) = year_candidates.first() {
                            break first;
                        }
                        year += interval as i32;
                    }
                }
            }

            Frequency::Monthly => {
                let current_month_ok = rrule.by_month.is_empty()
                    || rrule.by_month.contains(&(last_occurrence.month() as u8));
                let current_month_candidates = if current_month_ok {
                    Self::apply_by_set_pos(
                        Self::candidates_in_month(
                            last_occurrence,
                            last_occurrence.year(),
                            last_occurrence.month(),
                            rrule,
                            dt_start_wall.day(),
                        ),
                        &rrule.by_set_pos,
                    )
                } else {
                    Vec::new()
                };

                if let Some(&next_in_month) = current_month_candidates
                    .iter()
                    .find(|&&d| d > last_occurrence)
                {
                    next_in_month
                } else {
                    let mut step_months = interval;
                    loop {
                        let total_months = (last_occurrence.month() - 1) + step_months;
                        let year = last_occurrence.year() + (total_months / 12) as i32;
                        let month = (total_months % 12) + 1;
                        if rrule.by_month.is_empty() || rrule.by_month.contains(&(month as u8)) {
                            let month_candidates = Self::apply_by_set_pos(
                                Self::candidates_in_month(
                                    last_occurrence,
                                    year,
                                    month,
                                    rrule,
                                    dt_start_wall.day(),
                                ),
                                &rrule.by_set_pos,
                            );
                            if let Some(&first) = month_candidates.first() {
                                break first;
                            }
                        }
                        step_months += interval;
                    }
                }
            }

            Frequency::Weekly => {
                if !rrule.by_day.is_empty() {
                    let interval_i64 = interval as i64;
                    let wkst = rrule.wkst.unwrap_or(chrono::Weekday::Mon);
                    let days_since_wkst = |wd: chrono::Weekday| -> i64 {
                        ((wd.num_days_from_monday() + 7 - wkst.num_days_from_monday()) % 7) as i64
                    };
                    let dt_start_week_start = dt_start_wall.date_naive()
                        - Duration::days(days_since_wkst(dt_start_wall.date_naive().weekday()));
                    let mut next_occurrence = last_occurrence.next_by_day(&rrule.by_day);

                    loop {
                        let next_week_start = next_occurrence.date_naive()
                            - Duration::days(days_since_wkst(
                                next_occurrence.date_naive().weekday(),
                            ));
                        let diff_days = (next_week_start - dt_start_week_start).num_days();
                        let diff_weeks = diff_days / 7;

                        if diff_weeks % interval_i64 == 0 {
                            break;
                        }
                        next_occurrence = next_occurrence.next_by_day(&rrule.by_day);
                    }

                    next_occurrence
                } else {
                    last_occurrence + Duration::days(7 * interval as i64)
                }
            }

            Frequency::Daily => last_occurrence + Duration::days(interval as i64),
        };

        let next_occurrence = Self::from_wall_clock(wall_next);
        if !rrule.is_expired(next_occurrence) {
            self.last_occurrence = Some(next_occurrence);
            self.last_occurrence
        } else {
            None
        }
    }

    fn get_next_occurrence_according_to_rule_and_iterations(&mut self) -> Option<DateOrDateTime> {
        if let Some(last_occurrence) = self.last_occurrence {
            self.event.rrule.as_ref().and_then(|rrule| {
                if rrule.is_out_of_count(self.count) {
                    return None;
                }
                self.get_next_occurrence_according_to_rule(last_occurrence, rrule)
            })
        } else {
            if let Some(rrule) = &self.event.rrule
                && (rrule.is_out_of_count(self.count) || rrule.is_expired(self.event.dt_start))
            {
                return None;
            }
            self.last_occurrence = Some(self.event.dt_start);
            Some(self.event.dt_start)
        }
    }

    fn is_excluded(&self, dt: DateOrDateTime) -> bool {
        self.event
            .exdates
            .iter()
            .any(|exdate| dt.date_naive() == exdate.date_naive())
    }

    fn is_overridden(&self, dt: DateOrDateTime) -> bool {
        self.event
            .overridden_dates
            .iter()
            .any(|ov| dt.date_naive() == ov.date_naive())
    }

    fn next_valid_rrule_occurrence(&mut self) -> Option<DateOrDateTime> {
        if self.rrule_exhausted {
            return None;
        }
        loop {
            match self.get_next_occurrence_according_to_rule_and_iterations() {
                Some(candidate) => {
                    if !self.is_excluded(candidate) {
                        self.count += 1;
                        if !self.is_overridden(candidate) {
                            return Some(candidate);
                        }
                    }
                }
                None => {
                    self.rrule_exhausted = true;
                    return None;
                }
            }
        }
    }

    fn next_valid_rdate(&mut self) -> Option<DateOrDateTime> {
        while let Some(&rdate) = self.event.rdates.get(self.rdate_idx) {
            if self.is_excluded(rdate) || self.is_overridden(rdate) {
                self.rdate_idx += 1;
            } else {
                return Some(rdate);
            }
        }
        None
    }
}

impl<'a> Iterator for VEventIterator<'a> {
    type Item = Range<DateOrDateTime>;

    fn next(&mut self) -> Option<Self::Item> {
        log::trace!("function next({:?}) called", self);

        if self.peeked_rrule.is_none() && !self.rrule_exhausted {
            self.peeked_rrule = self.next_valid_rrule_occurrence();
        }

        let rdate_candidate = self.next_valid_rdate();

        let next_start = match (self.peeked_rrule, rdate_candidate) {
            (Some(rrule_dt), Some(rdate_dt)) => {
                if rrule_dt < rdate_dt {
                    self.peeked_rrule.take()
                } else if rdate_dt < rrule_dt {
                    self.rdate_idx += 1;
                    Some(rdate_dt)
                } else {
                    self.rdate_idx += 1;
                    self.peeked_rrule.take()
                }
            }
            (Some(_), None) => self.peeked_rrule.take(),
            (None, Some(rdate_dt)) => {
                self.rdate_idx += 1;
                Some(rdate_dt)
            }
            (None, None) => None,
        }?;

        let delta = self.event.dt_end - self.event.dt_start;
        let next_end = next_start + delta;
        Some(Range {
            start: next_start,
            end: next_end,
        })
    }
}