use serde::{Deserialize, Serialize}; use super::dates::add_days; use crate::constants::LineDirection; /// Whether a money row describes money that moved or money that is expected. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum MoneyRowStatus { Actual, Expected, } /// How firm an expected row is. Actual rows deliberately have no certainty: /// they happened and need no forecast qualifier. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum ExpectedCertainty { Fixed, Expected, Estimate, } /// A repeating expectation. The store materialises a new row from this rule; /// this enum contains no clock or database policy. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum MoneyRepeat { None, Monthly { day: u8 }, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct MoneyRow { pub id: String, pub occurred_on: String, pub account_id: Option, pub amount_minor: Option, pub amount_known: bool, pub direction: LineDirection, pub moves_money: bool, pub status: MoneyRowStatus, pub certainty: Option, pub repeat: MoneyRepeat, } #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] pub enum MoneyRowError { #[error("an actual row cannot carry expected certainty")] ActualHasCertainty, #[error("an expected row needs a certainty")] ExpectedNeedsCertainty, #[error("a known amount must be a positive number of minor units")] InvalidKnownAmount, #[error("an unknown amount cannot have an amount")] UnknownAmountHasValue, #[error("a monthly repeat needs a day from 1 to 31")] InvalidRepeatDay, #[error("an actual row that moves money needs an account")] MissingAccount, #[error("a money row needs a calendar date")] InvalidDate, #[error("an actual row must have a known amount")] ActualAmountUnknown, } impl MoneyRow { pub fn validate(&self) -> Result<(), MoneyRowError> { if add_days(&self.occurred_on, 0).is_err() { return Err(MoneyRowError::InvalidDate); } if self.status == MoneyRowStatus::Actual && self.moves_money && self .account_id .as_deref() .is_none_or(|id| id.trim().is_empty()) { return Err(MoneyRowError::MissingAccount); } match (self.status, self.certainty) { (MoneyRowStatus::Actual, Some(_)) => return Err(MoneyRowError::ActualHasCertainty), (MoneyRowStatus::Expected, None) => return Err(MoneyRowError::ExpectedNeedsCertainty), _ => {} } match (self.amount_known, self.amount_minor) { (true, Some(amount)) if amount > 0 => {} (true, _) => return Err(MoneyRowError::InvalidKnownAmount), (false, None) => {} (false, Some(_)) => return Err(MoneyRowError::UnknownAmountHasValue), } if self.status == MoneyRowStatus::Actual && !self.amount_known { return Err(MoneyRowError::ActualAmountUnknown); } if let MoneyRepeat::Monthly { day } = self.repeat { if !(1..=31).contains(&day) { return Err(MoneyRowError::InvalidRepeatDay); } } Ok(()) } } #[cfg(test)] mod tests { use super::*; /// RULE: A row is either actual or expected; a moved amount must never also become a forecast, because totals would count it twice. #[test] fn actual_rows_reject_expected_certainty() { let row = MoneyRow { id: "row".into(), occurred_on: "2026-10-17".into(), account_id: Some("account".into()), amount_minor: Some(100), amount_known: true, direction: LineDirection::Out, moves_money: true, status: MoneyRowStatus::Actual, certainty: Some(ExpectedCertainty::Expected), repeat: MoneyRepeat::None, }; assert!(row.validate().is_err()); } }