//! A repeating item's rhythm in words ("Every Monday"), and the folding a //! search matches by. PURE: the rule and the first day are handed in. use unicode_normalization::UnicodeNormalization; use super::weekday_monday_first; use super::words::{fill, Words}; use crate::zoned_clock::civil_from_days; const DAY_CODES: [&str; 7] = ["MO", "TU", "WE", "TH", "FR", "SA", "SU"]; /// The rule (`FREQ=WEEKLY;BYDAY=MO`, with or without `RRULE:`) in words, its /// first occurrence on `first` (a day number) naming what the rule leaves /// out (a weekly rule's weekday, a monthly one's date). pub fn rhythm(rrule: &str, first: i64, words: &Words) -> String { let rule = rrule.trim().trim_start_matches("RRULE:"); let part = |name: &str| { rule.split(';').find_map(|piece| { let (key, value) = piece.split_once('=')?; key.trim().eq_ignore_ascii_case(name).then(|| value.trim().to_ascii_uppercase()) }) }; let interval = part("INTERVAL").and_then(|n| n.parse::().ok()).filter(|n| *n > 1).unwrap_or(1); let n = words.count(interval); let (_, month, date) = civil_from_days(first); match part("FREQ").as_deref() { Some("DAILY") if interval == 1 => words.every_day.clone(), Some("DAILY") => fill(&words.every_n_days, &[("n", &n)]), Some("WEEKLY") => { let mut days: Vec = match part("BYDAY") { Some(list) => list.split(',').filter_map(|code| DAY_CODES.iter().position(|c| code.trim().ends_with(c))).collect(), None => vec![weekday_monday_first(first)], }; days.sort_unstable(); days.dedup(); if interval == 1 && days == [0, 1, 2, 3, 4] { return words.every_weekday.clone(); } let said = match days.as_slice() { [one] => words.weekdays[*one].clone(), [] => words.weekdays[weekday_monday_first(first)].clone(), many => { let short: Vec<&str> = many.iter().map(|d| words.weekdays_short[*d].as_str()).collect(); let (last, rest) = short.split_last().expect("many has a last"); format!("{}{}{last}", rest.join(", "), words.and) } }; if interval == 1 { fill(&words.every_week_on, &[("days", &said)]) } else { fill(&words.every_n_weeks_on, &[("n", &n), ("days", &said)]) } } Some("MONTHLY") => { let on = part("BYMONTHDAY").and_then(|d| d.parse::().ok()).filter(|d| (1..=31).contains(d)).unwrap_or(date); let nth = words.ordinals.get((on - 1) as usize).cloned().unwrap_or_else(|| on.to_string()); if part("BYDAY").is_some() { return if interval == 1 { words.every_month.clone() } else { fill(&words.every_n_months, &[("n", &n)]) }; } if interval == 1 { fill(&words.every_month_on, &[("nth", &nth)]) } else { fill(&words.every_n_months_on, &[("n", &n), ("nth", &nth)]) } } Some("YEARLY") => { let said = format!("{date} {}", words.months_short[(month - 1) as usize]); if interval == 1 { fill(&words.every_year_on, &[("date", &said)]) } else { fill(&words.every_n_years_on, &[("n", &n), ("date", &said)]) } } _ => words.repeats.clone(), } } /// Text as a search compares it: lower case, and the Latin, Greek and /// Cyrillic accents let go (é is e), so "cafe" finds "Café". Other scripts' /// marks (a Devanagari vowel sign) are part of the letter and stay. pub fn fold(text: &str) -> String { text.nfd().filter(|c| !('\u{0300}'..='\u{036F}').contains(c)).collect::().to_lowercase() } /// Whether every word of `asked` (already [`fold`]ed, split on spaces) is /// found somewhere in `within` (already folded). pub fn holds_all(within: &str, asked: &[String]) -> bool { !asked.is_empty() && asked.iter().all(|word| within.contains(word.as_str())) }