68 lines · 2.1 KB · RustRaw
//! A booked statement line creates exactly one actual money row.use cotton_store::protocol::js::is_js_whitespace;use super::row::{MoneyRepeat, MoneyRow, MoneyRowStatus};use crate::constants::LineDirection;#[derive(Debug, Clone, PartialEq, Eq)]pub struct StatementBookingInput { pub row_id: String, pub occurred_on: String, pub account_id: String, pub amount_minor: i64, pub direction: LineDirection, pub text: String,}#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]pub enum StatementBookingError { #[error("a statement line needs text before it can be booked")] EmptyText, #[error("a statement line needs a positive amount in minor units")] InvalidAmount,}/// Builds the row for one statement line. Evidence is deliberately stored by/// the caller: this pure function cannot delete, detach, or replace a line.pub fn book_statement_line( input: &StatementBookingInput,) -> Result<MoneyRow, StatementBookingError> { if input.text.trim_matches(is_js_whitespace).is_empty() { return Err(StatementBookingError::EmptyText); } if input.amount_minor <= 0 { return Err(StatementBookingError::InvalidAmount); } Ok(MoneyRow { id: input.row_id.clone(), occurred_on: input.occurred_on.clone(), account_id: Some(input.account_id.clone()), amount_minor: Some(input.amount_minor), amount_known: true, direction: input.direction, moves_money: true, status: MoneyRowStatus::Actual, certainty: None, repeat: MoneyRepeat::None, })}#[cfg(test)]mod tests { use super::*; #[test] fn a_statement_line_becomes_one_actual_row() { let row = book_statement_line(&StatementBookingInput { row_id: "row".into(), occurred_on: "2026-10-17".into(), account_id: "account".into(), amount_minor: 100, direction: LineDirection::Out, text: "Lunch".into(), }) .unwrap(); assert_eq!(row.status, MoneyRowStatus::Actual); assert!(row.validate().is_ok()); }}