//! Pure monthly recurrence calculation; the store owns materialisation. //! The calendar is `zoned_clock`'s: one month length and one clamp for every //! tool, so a money repeat and a yearly day cannot disagree on a short month. use super::dates::{add_days, DatesError}; use super::row::MoneyRepeat; use crate::transforms::zoned_clock::yearly_in_year; /// Returns the next expected date after `occurred_on` for a monthly rule. /// A day beyond the next month's length becomes that month's last day. pub fn next_occurrence( occurred_on: &str, repeat: MoneyRepeat, ) -> Result, DatesError> { let MoneyRepeat::Monthly { day } = repeat else { return Ok(None); }; if !(1..=31).contains(&day) { return Ok(None); } // Validate and normalise first: a malformed date is the same refusal as // every other money calculation, never a panic on a short string. let occurred_on = add_days(occurred_on, 0)?; let (year, month) = (occurred_on[..4].parse::(), occurred_on[5..7].parse::()); let (Ok(year), Ok(month)) = (year, month) else { return Err(DatesError::Unparseable(occurred_on)); }; let (year, month) = if month == 12 { (year + 1, 1) } else { (year, month + 1) }; let (year, month, day) = yearly_in_year(month, i64::from(day), year); Ok(Some(format!("{year:04}-{month:02}-{day:02}"))) }