use crate::RRuleResult;
use crate::{iter::rrule_iter::WasLimited, Tz};
use std::ops::{
Bound::{Excluded, Included, Unbounded},
RangeBounds,
};
pub(super) fn collect_with_error<T>(
mut iterator: T,
start: &Option<chrono::DateTime<Tz>>,
end: &Option<chrono::DateTime<Tz>>,
inclusive_start: bool,
inclusive_end: bool,
limit: Option<u16>,
) -> RRuleResult
where
T: Iterator<Item = chrono::DateTime<Tz>> + WasLimited,
{
let mut list = vec![];
let mut was_limited = false;
let mut has_more = false;
loop {
if matches!(limit, Some(limit) if usize::from(limit) == list.len()) {
has_more = iterator.next().is_some();
if !has_more {
was_limited = iterator.was_limited();
}
break;
}
match iterator.next() {
Some(value) => {
if is_in_range(&value, start, end, inclusive_start, inclusive_end) {
list.push(value);
}
if has_reached_the_end(&value, end, inclusive_end) {
break;
}
}
None => {
was_limited = iterator.was_limited();
break;
}
}
}
RRuleResult {
dates: list,
limited: was_limited,
has_more,
}
}
fn has_reached_the_end(
date: &chrono::DateTime<Tz>,
end: &Option<chrono::DateTime<Tz>>,
inclusive_end: bool,
) -> bool {
match end {
Some(end) if inclusive_end => date > end,
Some(end) => date >= end,
None => false,
}
}
pub(super) fn is_in_range(
date: &chrono::DateTime<Tz>,
start: &Option<chrono::DateTime<Tz>>,
end: &Option<chrono::DateTime<Tz>>,
inclusive_start: bool,
inclusive_end: bool,
) -> bool {
let start_bound = match (start, inclusive_start) {
(Some(start), true) => Included(*start),
(Some(start), false) => Excluded(*start),
(None, _) => Unbounded,
};
let end_bound = match (end, inclusive_end) {
(Some(end), true) => Included(*end),
(Some(end), false) => Excluded(*end),
(None, _) => Unbounded,
};
(start_bound, end_bound).contains(date)
}
#[cfg(test)]
mod tests {
use crate::core::Tz;
use super::*;
use chrono::TimeZone;
const UTC: Tz = Tz::UTC;
#[test]
fn in_range_exclusive_start_to_end() {
let inclusive = false;
let start = UTC.with_ymd_and_hms(2021, 10, 1, 8, 0, 0).unwrap();
let end = UTC.with_ymd_and_hms(2021, 10, 1, 10, 0, 0).unwrap();
assert!(is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 1, 9, 0, 0).unwrap(),
&Some(start),
&Some(end),
inclusive,
inclusive,
));
assert!(!is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 1, 7, 0, 0).unwrap(),
&Some(start),
&Some(end),
inclusive,
inclusive,
));
assert!(!is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 1, 11, 0, 0).unwrap(),
&Some(start),
&Some(end),
inclusive,
inclusive,
));
assert!(!is_in_range(&end, &Some(start), &Some(end), inclusive, inclusive));
assert!(!is_in_range(&start, &Some(start), &Some(end), inclusive, inclusive));
}
#[test]
fn in_range_exclusive_start() {
let inclusive = false;
let start = UTC.with_ymd_and_hms(2021, 10, 1, 8, 0, 0).unwrap();
assert!(is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 1, 9, 0, 0).unwrap(),
&Some(start),
&None,
inclusive,
inclusive,
));
assert!(!is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 1, 7, 0, 0).unwrap(),
&Some(start),
&None,
inclusive,
inclusive,
));
assert!(is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 2, 8, 0, 0).unwrap(),
&Some(start),
&None,
inclusive,
inclusive,
));
assert!(!is_in_range(&start, &Some(start), &None, inclusive, inclusive));
}
#[test]
fn in_range_exclusive_end() {
let inclusive = false;
let end = UTC.with_ymd_and_hms(2021, 10, 1, 10, 0, 0).unwrap();
assert!(is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 1, 9, 0, 0).unwrap(),
&None,
&Some(end),
inclusive,
inclusive,
));
assert!(is_in_range(
&UTC.with_ymd_and_hms(2021, 9, 20, 10, 0, 0).unwrap(),
&None,
&Some(end),
inclusive,
inclusive,
));
assert!(!is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 2, 8, 0, 0).unwrap(),
&None,
&Some(end),
inclusive,
inclusive,
));
assert!(!is_in_range(&end, &None, &Some(end), inclusive, inclusive));
}
#[test]
fn in_range_exclusive_all() {
let inclusive = false;
assert!(is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 1, 9, 0, 0).unwrap(),
&None,
&None,
inclusive,
inclusive,
));
assert!(is_in_range(
&UTC.with_ymd_and_hms(2021, 9, 20, 10, 0, 0).unwrap(),
&None,
&None,
inclusive,
inclusive,
));
assert!(is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 2, 8, 0, 0).unwrap(),
&None,
&None,
inclusive,
inclusive,
));
}
#[test]
fn in_range_inclusive_start_to_end() {
let inclusive = true;
let start = UTC.with_ymd_and_hms(2021, 10, 1, 8, 0, 0).unwrap();
let end = UTC.with_ymd_and_hms(2021, 10, 1, 10, 0, 0).unwrap();
assert!(is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 1, 9, 0, 0).unwrap(),
&Some(start),
&Some(end),
inclusive,
inclusive,
));
assert!(!is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 1, 7, 0, 0).unwrap(),
&Some(start),
&Some(end),
inclusive,
inclusive,
));
assert!(!is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 1, 11, 0, 0).unwrap(),
&Some(start),
&Some(end),
inclusive,
inclusive,
));
assert!(is_in_range(&end, &Some(start), &Some(end), inclusive, inclusive));
assert!(is_in_range(&start, &Some(start), &Some(end), inclusive, inclusive));
}
#[test]
fn in_range_inclusive_start() {
let inclusive = true;
let start = UTC.with_ymd_and_hms(2021, 10, 1, 8, 0, 0).unwrap();
assert!(is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 1, 9, 0, 0).unwrap(),
&Some(start),
&None,
inclusive,
inclusive,
));
assert!(!is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 1, 7, 0, 0).unwrap(),
&Some(start),
&None,
inclusive,
inclusive,
));
assert!(is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 2, 8, 0, 0).unwrap(),
&Some(start),
&None,
inclusive,
inclusive,
));
assert!(is_in_range(&start, &Some(start), &None, inclusive, inclusive));
}
#[test]
fn in_range_inclusive_end() {
let inclusive = true;
let end = UTC.with_ymd_and_hms(2021, 10, 1, 10, 0, 0).unwrap();
assert!(is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 1, 9, 0, 0).unwrap(),
&None,
&Some(end),
inclusive,
inclusive,
));
assert!(is_in_range(
&UTC.with_ymd_and_hms(2021, 9, 20, 10, 0, 0).unwrap(),
&None,
&Some(end),
inclusive,
inclusive,
));
assert!(!is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 2, 8, 0, 0).unwrap(),
&None,
&Some(end),
inclusive,
inclusive,
));
assert!(is_in_range(&end, &None, &Some(end), inclusive, inclusive));
}
#[test]
fn in_range_inclusive_all() {
let inclusive = true;
assert!(is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 1, 9, 0, 0).unwrap(),
&None,
&None,
inclusive,
inclusive,
));
assert!(is_in_range(
&UTC.with_ymd_and_hms(2021, 9, 20, 10, 0, 0).unwrap(),
&None,
&None,
inclusive,
inclusive,
));
assert!(is_in_range(
&UTC.with_ymd_and_hms(2021, 10, 2, 8, 0, 0).unwrap(),
&None,
&None,
inclusive,
inclusive,
));
}
}