amortize_rs/
error.rs

1use std::fmt;
2
3#[derive(Debug)]
4pub enum AmortizationError {
5    InvalidPeriods(u32),
6    InvalidInterestRate(f64),
7    InvalidLoanAmount(f64),
8    CalculationError(String),
9}
10
11impl std::error::Error for AmortizationError {}
12
13impl fmt::Display for AmortizationError {
14    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15        match self {
16            AmortizationError::InvalidPeriods(p) => write!(f, "Number of periods must be greater than 0, got {}", p),
17            AmortizationError::InvalidInterestRate(r) => write!(f, "Interest rate must be greater than 0, got {}", r),
18            AmortizationError::InvalidLoanAmount(a) => write!(f, "Loan amount must be greater than 0, got {}", a),
19            AmortizationError::CalculationError(msg) => write!(f, "Calculation error: {}", msg),
20        }
21    }
22}