Skip to main content

dial_ecology/
error.rs

1use std::fmt;
2
3/// Errors that can occur in ecological simulations.
4#[derive(Debug)]
5pub enum EcologyError {
6    EmptyTraditions,
7    MatrixDimensionMismatch { expected: usize, got: usize },
8    DialDimensionMismatch { name: String, expected: usize, got: usize },
9    InvalidGrowthRate { name: String, value: f64 },
10    NegativePopulation { name: String, value: f64 },
11    InvalidCarryingCapacity { name: String, value: f64 },
12    InvalidTimeStep { value: f64 },
13    Diverged { step: usize },
14    NoEquilibriumFound { iterations: usize },
15}
16
17impl fmt::Display for EcologyError {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        match self {
20            EcologyError::EmptyTraditions => write!(f, "tradition list is empty"),
21            EcologyError::MatrixDimensionMismatch { expected, got } => {
22                write!(f, "competition matrix dimension mismatch: expected {expected}x{expected}, got {got}x{got}")
23            }
24            EcologyError::DialDimensionMismatch { name, expected, got } => {
25                write!(f, "dial position dimension mismatch for tradition '{name}': expected {expected}, got {got}")
26            }
27            EcologyError::InvalidGrowthRate { name, value } => {
28                write!(f, "invalid growth rate for tradition '{name}': {value}")
29            }
30            EcologyError::NegativePopulation { name, value } => {
31                write!(f, "negative population for tradition '{name}': {value}")
32            }
33            EcologyError::InvalidCarryingCapacity { name, value } => {
34                write!(f, "non-positive carrying capacity for tradition '{name}': {value}")
35            }
36            EcologyError::InvalidTimeStep { value } => {
37                write!(f, "time step dt must be positive, got {value}")
38            }
39            EcologyError::Diverged { step } => {
40                write!(f, "solver diverged: population exceeded bounds at step {step}")
41            }
42            EcologyError::NoEquilibriumFound { iterations } => {
43                write!(f, "no equilibrium found after {iterations} iterations")
44            }
45        }
46    }
47}
48
49impl std::error::Error for EcologyError {}