| 1 | use crate::{NWeekday, RRule}; |
| 2 | |
| 3 | use super::iterinfo::IterInfo; |
| 4 | |
| 5 | type RRuleFilter = &'static dyn Fn(&IterInfo, usize, &RRule) -> bool; |
| 6 | |
| 7 | const FILTERS: [RRuleFilter; 7] = [ |
| 8 | &is_filtered_by_month, |
| 9 | &is_filtered_by_week_number, |
| 10 | &is_filtered_by_weekday, |
| 11 | &is_filtered_by_neg_weekday, |
| 12 | &is_filtered_by_easter, |
| 13 | &is_filtered_by_month_day, |
| 14 | &is_filtered_by_year_day, |
| 15 | ]; |
| 16 | |
| 17 | pub(crate) fn is_filtered(ii: &IterInfo, current_day: usize) -> bool { |
| 18 | let rrule = ii.rrule(); |
| 19 | FILTERS |
| 20 | .into_iter() |
| 21 | .any(|filter| filter(ii, current_day, rrule)) |
| 22 | } |
| 23 | |
| 24 | fn is_filtered_by_month(ii: &IterInfo, current_day: usize, rrule: &RRule) -> bool { |
| 25 | if rrule.by_month.is_empty() { |
| 26 | return false; |
| 27 | } |
| 28 | |
| 29 | let current_month = ii.month_mask()[current_day]; |
| 30 | !rrule.by_month.contains(¤t_month) |
| 31 | } |
| 32 | |
| 33 | fn is_filtered_by_week_number(ii: &IterInfo, current_day: usize, rrule: &RRule) -> bool { |
| 34 | if rrule.by_week_no.is_empty() { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | matches!(ii.week_no_mask(), Some(week_no_mask) if week_no_mask[current_day] == 0) |
| 39 | } |
| 40 | |
| 41 | fn is_filtered_by_weekday(ii: &IterInfo, current_day: usize, rrule: &RRule) -> bool { |
| 42 | let mut by_weekday_every_week_only = rrule |
| 43 | .by_weekday |
| 44 | .iter() |
| 45 | .filter_map(|by_weekday| match by_weekday { |
| 46 | // Get only `Every` occurrences. |
| 47 | NWeekday::Every(weekday) => Some(weekday.num_days_from_monday()), |
| 48 | NWeekday::Nth(_, _) => None, |
| 49 | }) |
| 50 | .peekable(); |
| 51 | |
| 52 | // Check if empty |
| 53 | if by_weekday_every_week_only.peek().is_none() { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | let current_weekday = ii.weekday_mask()[current_day]; |
| 58 | !by_weekday_every_week_only.any(|el| el == current_weekday) |
| 59 | } |
| 60 | |
| 61 | fn is_filtered_by_neg_weekday(ii: &IterInfo, current_day: usize, _rrule: &RRule) -> bool { |
| 62 | if let Some(neg_weekday_mask) = ii.neg_weekday_mask() { |
| 63 | if neg_weekday_mask.is_empty() { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | let current_neg_weekday = neg_weekday_mask[current_day]; |
| 68 | current_neg_weekday == 0 |
| 69 | } else { |
| 70 | false |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | fn is_filtered_by_easter(ii: &IterInfo, current_day: usize, rrule: &RRule) -> bool { |
| 75 | if cfg!(feature = "by-easter") { |
| 76 | if rrule.by_easter.is_none() { |
| 77 | return false; |
| 78 | } |
| 79 | match i32::try_from(current_day) { |
| 80 | Ok(current_day) => { |
| 81 | !matches!(ii.easter_mask(), Some(easter_mask) if easter_mask.contains(¤t_day)) |
| 82 | } |
| 83 | _ => true, |
| 84 | } |
| 85 | } else { |
| 86 | false |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | fn is_filtered_by_month_day(ii: &IterInfo, current_day: usize, rrule: &RRule) -> bool { |
| 91 | if rrule.by_month_day.is_empty() && rrule.by_n_month_day.is_empty() { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | let current_month_day = ii.month_day_mask()[current_day]; |
| 96 | let current_n_month_day = ii.neg_month_day_mask()[current_day]; |
| 97 | let filtered_by_month_day = !rrule.by_month_day.contains(¤t_month_day); |
| 98 | let filtered_by_n_month_day = !rrule.by_n_month_day.contains(¤t_n_month_day); |
| 99 | |
| 100 | filtered_by_month_day && filtered_by_n_month_day |
| 101 | } |
| 102 | |
| 103 | fn is_filtered_by_year_day(ii: &IterInfo, current_day: usize, rrule: &RRule) -> bool { |
| 104 | if rrule.by_year_day.is_empty() { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | let current_day = match i16::try_from(current_day) { |
| 109 | Ok(current_day) => current_day, |
| 110 | _ => return true, |
| 111 | }; |
| 112 | |
| 113 | let year_len = i16::try_from(ii.year_len()).expect("year length is always within range of i16"); |
| 114 | let next_year_len = |
| 115 | i16::try_from(ii.next_year_len()).expect("year length is always within range of i16"); |
| 116 | |
| 117 | if current_day < year_len { |
| 118 | !rrule.by_year_day.contains(&(current_day + 1)) |
| 119 | && !rrule.by_year_day.contains(&(current_day - year_len)) |
| 120 | } else { |
| 121 | !rrule.by_year_day.contains(&(current_day + 1 - year_len)) |
| 122 | && !rrule |
| 123 | .by_year_day |
| 124 | .contains(&(current_day - next_year_len - year_len)) |
| 125 | } |
| 126 | } |