| 1 | use crate::RRuleResult; |
| 2 | use crate::{iter::rrule_iter::WasLimited, Tz}; |
| 3 | use std::ops::{ |
| 4 | Bound::{Excluded, Unbounded}, |
| 5 | RangeBounds, |
| 6 | }; |
| 7 | |
| 8 | /// Helper function to collect dates given some filters. |
| 9 | /// |
| 10 | /// In the case where the iterator ended with errors, the error will be included, |
| 11 | /// otherwise the second value of the return tuple will be `None`. |
| 12 | pub(super) fn collect_with_error<T>( |
| 13 | mut iterator: T, |
| 14 | start: &Option<chrono::DateTime<Tz>>, |
| 15 | end: &Option<chrono::DateTime<Tz>>, |
| 16 | inclusive: bool, |
| 17 | limit: Option<u16>, |
| 18 | ) -> RRuleResult |
| 19 | where |
| 20 | T: Iterator<Item = chrono::DateTime<Tz>> + WasLimited, |
| 21 | { |
| 22 | let mut list = vec![]; |
| 23 | let mut was_limited = false; |
| 24 | // This loop should always end because `.next()` has build in limits |
| 25 | // Once a limit is tripped it will break in the `None` case. |
| 26 | while limit.is_none() || matches!(limit, Some(limit) if usize::from(limit) > list.len()) { |
| 27 | if let Some(value) = iterator.next() { |
| 28 | if is_in_range(&value, start, end, inclusive) { |
| 29 | list.push(value); |
| 30 | } |
| 31 | if has_reached_the_end(&value, end, inclusive) { |
| 32 | // Date is after end date, so can stop iterating |
| 33 | break; |
| 34 | } |
| 35 | } else { |
| 36 | was_limited = iterator.was_limited(); |
| 37 | break; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | was_limited = was_limited || matches!(limit, Some(limit) if usize::from(limit) == list.len()); |
| 42 | |
| 43 | RRuleResult { |
| 44 | dates: list, |
| 45 | limited: was_limited, |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// Checks if `date` is after `end`. |
| 50 | fn has_reached_the_end( |
| 51 | date: &chrono::DateTime<Tz>, |
| 52 | end: &Option<chrono::DateTime<Tz>>, |
| 53 | inclusive: bool, |
| 54 | ) -> bool { |
| 55 | if inclusive { |
| 56 | match end { |
| 57 | Some(end) => !(..=end).contains(&date), |
| 58 | None => false, |
| 59 | } |
| 60 | } else { |
| 61 | match end { |
| 62 | Some(end) => !(Unbounded, Excluded(end)).contains(date), |
| 63 | None => false, |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// Helper function to determine if a date is within a given range. |
| 69 | pub(super) fn is_in_range( |
| 70 | date: &chrono::DateTime<Tz>, |
| 71 | start: &Option<chrono::DateTime<Tz>>, |
| 72 | end: &Option<chrono::DateTime<Tz>>, |
| 73 | inclusive: bool, |
| 74 | ) -> bool { |
| 75 | // Should it include or not include the start and/or end date? |
| 76 | if inclusive { |
| 77 | match (start, end) { |
| 78 | (Some(start), Some(end)) => (start..=end).contains(&date), |
| 79 | (Some(start), None) => (start..).contains(&date), |
| 80 | (None, Some(end)) => (..=end).contains(&date), |
| 81 | (None, None) => true, |
| 82 | } |
| 83 | } else { |
| 84 | match (start, end) { |
| 85 | (Some(start), Some(end)) => (Excluded(start), Excluded(end)).contains(date), |
| 86 | (Some(start), None) => (Excluded(start), Unbounded).contains(date), |
| 87 | (None, Some(end)) => (Unbounded, Excluded(end)).contains(date), |
| 88 | (None, None) => true, |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #[cfg(test)] |
| 94 | mod tests { |
| 95 | use crate::core::Tz; |
| 96 | |
| 97 | use super::*; |
| 98 | use chrono::TimeZone; |
| 99 | |
| 100 | const UTC: Tz = Tz::UTC; |
| 101 | |
| 102 | #[test] |
| 103 | fn in_range_exclusive_start_to_end() { |
| 104 | let inclusive = false; |
| 105 | let start = UTC.with_ymd_and_hms(2021, 10, 1, 8, 0, 0).unwrap(); |
| 106 | let end = UTC.with_ymd_and_hms(2021, 10, 1, 10, 0, 0).unwrap(); |
| 107 | |
| 108 | // In middle |
| 109 | assert!(is_in_range( |
| 110 | &UTC.with_ymd_and_hms(2021, 10, 1, 9, 0, 0).unwrap(), |
| 111 | &Some(start), |
| 112 | &Some(end), |
| 113 | inclusive, |
| 114 | )); |
| 115 | // To small |
| 116 | assert!(!is_in_range( |
| 117 | &UTC.with_ymd_and_hms(2021, 10, 1, 7, 0, 0).unwrap(), |
| 118 | &Some(start), |
| 119 | &Some(end), |
| 120 | inclusive, |
| 121 | )); |
| 122 | // To big |
| 123 | assert!(!is_in_range( |
| 124 | &UTC.with_ymd_and_hms(2021, 10, 1, 11, 0, 0).unwrap(), |
| 125 | &Some(start), |
| 126 | &Some(end), |
| 127 | inclusive, |
| 128 | )); |
| 129 | // Equal to end |
| 130 | assert!(!is_in_range(&end, &Some(start), &Some(end), inclusive)); |
| 131 | // Equal to start |
| 132 | assert!(!is_in_range(&start, &Some(start), &Some(end), inclusive)); |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn in_range_exclusive_start() { |
| 137 | let inclusive = false; |
| 138 | let start = UTC.with_ymd_and_hms(2021, 10, 1, 8, 0, 0).unwrap(); |
| 139 | |
| 140 | // Just after |
| 141 | assert!(is_in_range( |
| 142 | &UTC.with_ymd_and_hms(2021, 10, 1, 9, 0, 0).unwrap(), |
| 143 | &Some(start), |
| 144 | &None, |
| 145 | inclusive, |
| 146 | )); |
| 147 | // To small |
| 148 | assert!(!is_in_range( |
| 149 | &UTC.with_ymd_and_hms(2021, 10, 1, 7, 0, 0).unwrap(), |
| 150 | &Some(start), |
| 151 | &None, |
| 152 | inclusive, |
| 153 | )); |
| 154 | // Bigger |
| 155 | assert!(is_in_range( |
| 156 | &UTC.with_ymd_and_hms(2021, 10, 2, 8, 0, 0).unwrap(), |
| 157 | &Some(start), |
| 158 | &None, |
| 159 | inclusive, |
| 160 | )); |
| 161 | // Equal to start |
| 162 | assert!(!is_in_range(&start, &Some(start), &None, inclusive)); |
| 163 | } |
| 164 | |
| 165 | #[test] |
| 166 | fn in_range_exclusive_end() { |
| 167 | let inclusive = false; |
| 168 | let end = UTC.with_ymd_and_hms(2021, 10, 1, 10, 0, 0).unwrap(); |
| 169 | |
| 170 | // Just before |
| 171 | assert!(is_in_range( |
| 172 | &UTC.with_ymd_and_hms(2021, 10, 1, 9, 0, 0).unwrap(), |
| 173 | &None, |
| 174 | &Some(end), |
| 175 | inclusive, |
| 176 | )); |
| 177 | // Smaller |
| 178 | assert!(is_in_range( |
| 179 | &UTC.with_ymd_and_hms(2021, 9, 20, 10, 0, 0).unwrap(), |
| 180 | &None, |
| 181 | &Some(end), |
| 182 | inclusive, |
| 183 | )); |
| 184 | // Bigger |
| 185 | assert!(!is_in_range( |
| 186 | &UTC.with_ymd_and_hms(2021, 10, 2, 8, 0, 0).unwrap(), |
| 187 | &None, |
| 188 | &Some(end), |
| 189 | inclusive, |
| 190 | )); |
| 191 | // Equal to end |
| 192 | assert!(!is_in_range(&end, &None, &Some(end), inclusive)); |
| 193 | } |
| 194 | |
| 195 | #[test] |
| 196 | fn in_range_exclusive_all() { |
| 197 | let inclusive = false; |
| 198 | |
| 199 | // Some date |
| 200 | assert!(is_in_range( |
| 201 | &UTC.with_ymd_and_hms(2021, 10, 1, 9, 0, 0).unwrap(), |
| 202 | &None, |
| 203 | &None, |
| 204 | inclusive, |
| 205 | )); |
| 206 | // Smaller |
| 207 | assert!(is_in_range( |
| 208 | &UTC.with_ymd_and_hms(2021, 9, 20, 10, 0, 0).unwrap(), |
| 209 | &None, |
| 210 | &None, |
| 211 | inclusive, |
| 212 | )); |
| 213 | // Bigger |
| 214 | assert!(is_in_range( |
| 215 | &UTC.with_ymd_and_hms(2021, 10, 2, 8, 0, 0).unwrap(), |
| 216 | &None, |
| 217 | &None, |
| 218 | inclusive, |
| 219 | )); |
| 220 | } |
| 221 | |
| 222 | // ---------------- inclusive ----------------------- |
| 223 | |
| 224 | #[test] |
| 225 | fn in_range_inclusive_start_to_end() { |
| 226 | let inclusive = true; |
| 227 | let start = UTC.with_ymd_and_hms(2021, 10, 1, 8, 0, 0).unwrap(); |
| 228 | let end = UTC.with_ymd_and_hms(2021, 10, 1, 10, 0, 0).unwrap(); |
| 229 | |
| 230 | // In middle |
| 231 | assert!(is_in_range( |
| 232 | &UTC.with_ymd_and_hms(2021, 10, 1, 9, 0, 0).unwrap(), |
| 233 | &Some(start), |
| 234 | &Some(end), |
| 235 | inclusive, |
| 236 | )); |
| 237 | // To small |
| 238 | assert!(!is_in_range( |
| 239 | &UTC.with_ymd_and_hms(2021, 10, 1, 7, 0, 0).unwrap(), |
| 240 | &Some(start), |
| 241 | &Some(end), |
| 242 | inclusive, |
| 243 | )); |
| 244 | // To big |
| 245 | assert!(!is_in_range( |
| 246 | &UTC.with_ymd_and_hms(2021, 10, 1, 11, 0, 0).unwrap(), |
| 247 | &Some(start), |
| 248 | &Some(end), |
| 249 | inclusive, |
| 250 | )); |
| 251 | // Equal to end |
| 252 | assert!(is_in_range(&end, &Some(start), &Some(end), inclusive)); |
| 253 | // Equal to start |
| 254 | assert!(is_in_range(&start, &Some(start), &Some(end), inclusive)); |
| 255 | } |
| 256 | |
| 257 | #[test] |
| 258 | fn in_range_inclusive_start() { |
| 259 | let inclusive = true; |
| 260 | let start = UTC.with_ymd_and_hms(2021, 10, 1, 8, 0, 0).unwrap(); |
| 261 | |
| 262 | // Just after |
| 263 | assert!(is_in_range( |
| 264 | &UTC.with_ymd_and_hms(2021, 10, 1, 9, 0, 0).unwrap(), |
| 265 | &Some(start), |
| 266 | &None, |
| 267 | inclusive, |
| 268 | )); |
| 269 | // To small |
| 270 | assert!(!is_in_range( |
| 271 | &UTC.with_ymd_and_hms(2021, 10, 1, 7, 0, 0).unwrap(), |
| 272 | &Some(start), |
| 273 | &None, |
| 274 | inclusive, |
| 275 | )); |
| 276 | // Bigger |
| 277 | assert!(is_in_range( |
| 278 | &UTC.with_ymd_and_hms(2021, 10, 2, 8, 0, 0).unwrap(), |
| 279 | &Some(start), |
| 280 | &None, |
| 281 | inclusive, |
| 282 | )); |
| 283 | // Equal to start |
| 284 | assert!(is_in_range(&start, &Some(start), &None, inclusive)); |
| 285 | } |
| 286 | |
| 287 | #[test] |
| 288 | fn in_range_inclusive_end() { |
| 289 | let inclusive = true; |
| 290 | let end = UTC.with_ymd_and_hms(2021, 10, 1, 10, 0, 0).unwrap(); |
| 291 | |
| 292 | // Just before |
| 293 | assert!(is_in_range( |
| 294 | &UTC.with_ymd_and_hms(2021, 10, 1, 9, 0, 0).unwrap(), |
| 295 | &None, |
| 296 | &Some(end), |
| 297 | inclusive, |
| 298 | )); |
| 299 | // Smaller |
| 300 | assert!(is_in_range( |
| 301 | &UTC.with_ymd_and_hms(2021, 9, 20, 10, 0, 0).unwrap(), |
| 302 | &None, |
| 303 | &Some(end), |
| 304 | inclusive, |
| 305 | )); |
| 306 | // Bigger |
| 307 | assert!(!is_in_range( |
| 308 | &UTC.with_ymd_and_hms(2021, 10, 2, 8, 0, 0).unwrap(), |
| 309 | &None, |
| 310 | &Some(end), |
| 311 | inclusive, |
| 312 | )); |
| 313 | // Equal to end |
| 314 | assert!(is_in_range(&end, &None, &Some(end), inclusive)); |
| 315 | } |
| 316 | |
| 317 | #[test] |
| 318 | fn in_range_inclusive_all() { |
| 319 | let inclusive = true; |
| 320 | |
| 321 | // Some date |
| 322 | assert!(is_in_range( |
| 323 | &UTC.with_ymd_and_hms(2021, 10, 1, 9, 0, 0).unwrap(), |
| 324 | &None, |
| 325 | &None, |
| 326 | inclusive, |
| 327 | )); |
| 328 | // Smaller |
| 329 | assert!(is_in_range( |
| 330 | &UTC.with_ymd_and_hms(2021, 9, 20, 10, 0, 0).unwrap(), |
| 331 | &None, |
| 332 | &None, |
| 333 | inclusive, |
| 334 | )); |
| 335 | // Bigger |
| 336 | assert!(is_in_range( |
| 337 | &UTC.with_ymd_and_hms(2021, 10, 2, 8, 0, 0).unwrap(), |
| 338 | &None, |
| 339 | &None, |
| 340 | inclusive, |
| 341 | )); |
| 342 | } |
| 343 | } |