nlcalc 0.1.0

Plugin-based natural language calculator for Rust and the command line
Documentation
use chrono::{Datelike, Days, NaiveDate};
/// Additional explicitly named calendars for extended expressions.
/// Dates identify the occasion itself, without weekend substitute-day rules.
pub(crate) fn extended_holiday(name: &str, year: i32) -> Option<NaiveDate> {
    if let Some(date) = holiday(name, year) {
        return Some(date);
    }
    let name = name.trim_start_matches("us ");
    let (month, day) = match name {
        "thanksgiving" | "thanksgiving day" => return holiday("thanksgiving", year),
        "veterans day" => (11, 11),
        "juneteenth" | "juneteenth national independence day" => (6, 19),
        "independence day" => (7, 4),
        "australia day" => (1, 26),
        "anzac day" => (4, 25),
        "labor day" => return weekday_in_month(year, 9, 0, 1),
        "columbus day" => return weekday_in_month(year, 10, 0, 2),
        "nsw labour day" | "australian labour day in nsw" | "labour day in nsw" => {
            return weekday_in_month(year, 10, 0, 1);
        }
        "martin luther king day" | "martin luther king jr day" => {
            return weekday_in_month(year, 1, 0, 3);
        }
        "presidents day" | "presidents' day" | "washington's birthday" => {
            return weekday_in_month(year, 2, 0, 3);
        }
        "memorial day" => {
            let end = NaiveDate::from_ymd_opt(year, 5, 31)?;
            return end.checked_sub_days(Days::new(end.weekday().num_days_from_monday() as u64));
        }
        _ => return None,
    };
    NaiveDate::from_ymd_opt(year, month, day)
}

/// Observed dates for the usual Monday–Friday US federal calendar.
pub(crate) fn next_us_federal_holiday(after: NaiveDate) -> Option<NaiveDate> {
    let mut next = None;
    for year in after.year()..=after.year().checked_add(2)? {
        for name in [
            "new year's day",
            "martin luther king day",
            "presidents day",
            "memorial day",
            "juneteenth",
            "independence day",
            "labor day",
            "columbus day",
            "veterans day",
            "thanksgiving",
            "christmas",
        ] {
            let date = extended_holiday(name, year)?;
            let date = match date.weekday() {
                chrono::Weekday::Sat => date.checked_sub_days(Days::new(1))?,
                chrono::Weekday::Sun => date.checked_add_days(Days::new(1))?,
                _ => date,
            };
            if date > after && next.is_none_or(|current| date < current) {
                next = Some(date);
            }
        }
    }
    next
}

fn weekday_in_month(year: i32, month: u32, weekday: u32, ordinal: u32) -> Option<NaiveDate> {
    let first = NaiveDate::from_ymd_opt(year, month, 1)?;
    let offset = (weekday + 7 - first.weekday().num_days_from_monday()) % 7 + 7 * (ordinal - 1);
    first.checked_add_days(Days::new(offset as u64))
}

/// Resolve annual holidays and their common names without tying them to the capture year.
pub(crate) fn holiday(name: &str, year: i32) -> Option<NaiveDate> {
    let fixed = match name {
        "christmas" | "christmas day" | "xmas" => (12, 25),
        "christmas eve" => (12, 24),
        "boxing day" => (12, 26),
        "new year's day" | "new years" | "new years day" => (1, 1),
        "nye" | "new years eve" | "new year's eve" => (12, 31),
        "valentine's day" | "saint valentine's day" | "valentines day" => (2, 14),
        "halloween" => (10, 31),
        "easter" | "easter sunday" => return easter(year),
        "good friday" | "holy friday" | "easter friday" => {
            return easter(year)?.checked_sub_days(Days::new(2));
        }
        "thanksgiving" | "black friday" => {
            let first = NaiveDate::from_ymd_opt(year, 11, 1)?;
            let delta = (3 + 7 - first.weekday().num_days_from_monday()) % 7
                + 21
                + u32::from(name == "black friday");
            return first.checked_add_days(Days::new(delta as u64));
        }
        "chinese new year" | "spring festival" | "lunar new year" => {
            let d = icu_calendar::Date::try_new_chinese_traditional(
                year,
                icu_calendar::types::Month::new(1),
                1,
            )
            .ok()?
            .to_calendar(icu_calendar::cal::Iso);
            return NaiveDate::from_ymd_opt(
                d.year().extended_year(),
                d.month().ordinal as u32,
                d.day_of_month().0 as u32,
            );
        }
        "ramadan" | "ramadan start" | "start of ramadan" | "ramadan end" | "end of ramadan" => {
            use icu_calendar::{
                Date,
                cal::{Hijri, Iso},
            };
            let y = Date::try_new_iso(year, 1, 1)
                .ok()?
                .to_calendar(Hijri::new_umm_al_qura())
                .year()
                .extended_year();
            for hy in y..=y + 1 {
                let d = Date::try_new_hijri_with_calendar(
                    hy,
                    if name.contains("end") { 10 } else { 9 },
                    1,
                    Hijri::new_umm_al_qura(),
                )
                .ok()?
                .to_calendar(Iso);
                let d = NaiveDate::from_ymd_opt(
                    d.year().extended_year(),
                    d.month().ordinal as u32,
                    d.day_of_month().0 as u32,
                )?
                .checked_sub_days(Days::new(1))?;
                if d.year() == year {
                    return Some(d);
                }
            }
            return None;
        }
        _ => return None,
    };
    NaiveDate::from_ymd_opt(year, fixed.0, fixed.1)
}
fn easter(year: i32) -> Option<NaiveDate> {
    if year < 1583 {
        return None;
    }
    let a = year % 19;
    let b = year / 100;
    let c = year % 100;
    let d = b / 4;
    let e = b % 4;
    let f = (b + 8) / 25;
    let g = (b - f + 1) / 3;
    let h = (19 * a + b - d - g + 15) % 30;
    let i = c / 4;
    let k = c % 4;
    let l = (32 + 2 * e + 2 * i - h - k) % 7;
    let m = (a + 11 * h + 22 * l) / 451;
    let n = h + l - 7 * m + 114;
    NaiveDate::from_ymd_opt(year, (n / 31) as u32, (n % 31 + 1) as u32)
}