| 1 | use chrono::DateTime; |
| 2 | |
| 3 | use super::rrule_iter::WasLimited; |
| 4 | use super::{rrule_iter::RRuleIter, MAX_ITER_LOOP}; |
| 5 | use crate::RRuleSet; |
| 6 | use crate::{RRuleError, Tz}; |
| 7 | use std::collections::BTreeSet; |
| 8 | use std::str::FromStr; |
| 9 | use std::{collections::HashMap, iter::Iterator}; |
| 10 | |
| 11 | #[derive(Debug, Clone)] |
| 12 | /// Iterator over all the dates in an [`RRuleSet`]. |
| 13 | pub struct RRuleSetIter { |
| 14 | queue: HashMap<usize, DateTime<Tz>>, |
| 15 | limited: bool, |
| 16 | rrule_iters: Vec<RRuleIter>, |
| 17 | exrules: Vec<RRuleIter>, |
| 18 | exdates: BTreeSet<i64>, |
| 19 | /// Sorted additional dates in descending order |
| 20 | rdates: Vec<DateTime<Tz>>, |
| 21 | was_limited: bool, |
| 22 | } |
| 23 | |
| 24 | impl RRuleSetIter { |
| 25 | fn generate_date( |
| 26 | dates: &mut Vec<DateTime<Tz>>, |
| 27 | exrules: &mut [RRuleIter], |
| 28 | exdates: &mut BTreeSet<i64>, |
| 29 | limited: bool, |
| 30 | ) -> (Option<DateTime<Tz>>, bool) { |
| 31 | if dates.is_empty() { |
| 32 | return (None, false); |
| 33 | } |
| 34 | |
| 35 | let mut date = dates.remove(dates.len() - 1); |
| 36 | let mut loop_counter: u32 = 0; |
| 37 | while Self::is_date_excluded(&date, exrules, exdates) { |
| 38 | if dates.is_empty() { |
| 39 | return (None, false); |
| 40 | } |
| 41 | // Prevent infinite loops |
| 42 | if limited { |
| 43 | loop_counter += 1; |
| 44 | if loop_counter >= MAX_ITER_LOOP { |
| 45 | log::warn!( |
| 46 | "Reached max loop counter (`{}`). \ |
| 47 | See 'validator limits' in docs for more info.", |
| 48 | MAX_ITER_LOOP |
| 49 | ); |
| 50 | return (None, true); |
| 51 | } |
| 52 | } |
| 53 | date = dates.remove(dates.len() - 1); |
| 54 | } |
| 55 | |
| 56 | (Some(date), false) |
| 57 | } |
| 58 | |
| 59 | fn generate( |
| 60 | rrule_iter: &mut RRuleIter, |
| 61 | exrules: &mut [RRuleIter], |
| 62 | exdates: &mut BTreeSet<i64>, |
| 63 | limited: bool, |
| 64 | ) -> (Option<DateTime<Tz>>, bool) { |
| 65 | let mut date = match rrule_iter.next() { |
| 66 | Some(d) => d, |
| 67 | None => return (None, false), |
| 68 | }; |
| 69 | let mut loop_counter: u32 = 0; |
| 70 | while Self::is_date_excluded(&date, exrules, exdates) { |
| 71 | // Prevent infinite loops |
| 72 | if limited { |
| 73 | loop_counter += 1; |
| 74 | if loop_counter >= MAX_ITER_LOOP { |
| 75 | log::warn!( |
| 76 | "Reached max loop counter (`{}`). \ |
| 77 | See 'validator limits' in docs for more info.", |
| 78 | MAX_ITER_LOOP |
| 79 | ); |
| 80 | return (None, true); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | date = match rrule_iter.next() { |
| 85 | Some(d) => d, |
| 86 | None => return (None, false), |
| 87 | }; |
| 88 | } |
| 89 | |
| 90 | (Some(date), false) |
| 91 | } |
| 92 | |
| 93 | fn is_date_excluded( |
| 94 | date: &DateTime<Tz>, |
| 95 | exrules: &mut [RRuleIter], |
| 96 | exdates: &mut BTreeSet<i64>, |
| 97 | ) -> bool { |
| 98 | for exrule in exrules { |
| 99 | for exdate in exrule { |
| 100 | exdates.insert(exdate.timestamp()); |
| 101 | if exdate > *date { |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | exdates.contains(&date.timestamp()) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | impl Iterator for RRuleSetIter { |
| 112 | type Item = DateTime<Tz>; |
| 113 | |
| 114 | fn next(&mut self) -> Option<Self::Item> { |
| 115 | let mut next_date: Option<(usize, DateTime<Tz>)> = None; |
| 116 | |
| 117 | // If there already was an error, return the error again. |
| 118 | if self.was_limited { |
| 119 | return None; |
| 120 | } |
| 121 | |
| 122 | for (i, rrule_iter) in self.rrule_iters.iter_mut().enumerate() { |
| 123 | let rrule_queue = self.queue.remove(&i); |
| 124 | let next_rrule_date = if let Some(d) = rrule_queue { |
| 125 | Some(d) |
| 126 | } else { |
| 127 | // should be method on self |
| 128 | let (date, was_limited) = Self::generate( |
| 129 | rrule_iter, |
| 130 | &mut self.exrules, |
| 131 | &mut self.exdates, |
| 132 | self.limited, |
| 133 | ); |
| 134 | |
| 135 | if was_limited { |
| 136 | self.was_limited = true; |
| 137 | return None; |
| 138 | } |
| 139 | |
| 140 | date |
| 141 | }; |
| 142 | |
| 143 | if let Some(next_rrule_date) = next_rrule_date { |
| 144 | match next_date { |
| 145 | None => next_date = Some((i, next_rrule_date)), |
| 146 | Some((idx, date)) => { |
| 147 | if date >= next_rrule_date { |
| 148 | // Add previous date to its rrule queue |
| 149 | self.queue.insert(idx, date); |
| 150 | |
| 151 | // Update next_date |
| 152 | next_date = Some((i, next_rrule_date)); |
| 153 | } else { |
| 154 | // Store for next iterations |
| 155 | self.queue.insert(i, next_rrule_date); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | let (generated_date, was_limited) = Self::generate_date( |
| 163 | &mut self.rdates, |
| 164 | &mut self.exrules, |
| 165 | &mut self.exdates, |
| 166 | self.limited, |
| 167 | ); |
| 168 | if was_limited { |
| 169 | self.was_limited = true; |
| 170 | return None; |
| 171 | } |
| 172 | |
| 173 | match generated_date { |
| 174 | Some(first_rdate) => { |
| 175 | let next_date = match next_date { |
| 176 | Some(next_date) => { |
| 177 | if next_date.1 >= first_rdate { |
| 178 | // Add previous date to its rrule queue |
| 179 | self.queue.insert(next_date.0, next_date.1); |
| 180 | |
| 181 | first_rdate |
| 182 | } else { |
| 183 | // add rdate back |
| 184 | self.rdates.push(first_rdate); |
| 185 | |
| 186 | next_date.1 |
| 187 | } |
| 188 | } |
| 189 | None => first_rdate, |
| 190 | }; |
| 191 | Some(next_date) |
| 192 | } |
| 193 | None => next_date.map(|d| d.1), |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | impl IntoIterator for &RRuleSet { |
| 199 | type Item = DateTime<Tz>; |
| 200 | |
| 201 | type IntoIter = RRuleSetIter; |
| 202 | |
| 203 | fn into_iter(self) -> Self::IntoIter { |
| 204 | // Sort in decreasing order |
| 205 | let mut rdates_sorted = self.rdate.clone(); |
| 206 | rdates_sorted |
| 207 | .sort_by(|d1, d2| d2.partial_cmp(d1).expect("Could not order dates correctly")); |
| 208 | |
| 209 | let limited = self.limited; |
| 210 | |
| 211 | RRuleSetIter { |
| 212 | queue: HashMap::new(), |
| 213 | limited, |
| 214 | rrule_iters: self |
| 215 | .rrule |
| 216 | .iter() |
| 217 | .map(|rrule| rrule.iter_with_ctx(self.dt_start, limited)) |
| 218 | .collect(), |
| 219 | rdates: rdates_sorted, |
| 220 | exrules: self |
| 221 | .exrule |
| 222 | .iter() |
| 223 | .map(|exrule| exrule.iter_with_ctx(self.dt_start, limited)) |
| 224 | .collect(), |
| 225 | exdates: self.exdate.iter().map(DateTime::timestamp).collect(), |
| 226 | was_limited: false, |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | impl WasLimited for RRuleSetIter { |
| 232 | fn was_limited(&self) -> bool { |
| 233 | self.was_limited |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | impl FromStr for RRuleSetIter { |
| 238 | type Err = RRuleError; |
| 239 | |
| 240 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 241 | Ok(RRuleSet::from_str(s)?.into_iter()) |
| 242 | } |
| 243 | } |