|
| 1 | +//! Math error types returned from fallible operations. |
| 2 | +
|
| 3 | +use std::fmt; |
| 4 | + |
| 5 | +/// Errors returned by fallible math operations in `lambda-rs`. |
| 6 | +#[derive(Debug, Clone, PartialEq)] |
| 7 | +pub enum MathError { |
| 8 | + /// Cross product requires exactly 3 dimensions. |
| 9 | + CrossProductDimension { actual: usize }, |
| 10 | + /// Cross product requires both vectors to have the same dimension. |
| 11 | + MismatchedVectorDimensions { left: usize, right: usize }, |
| 12 | + /// Rotation axis must be a unit axis vector (one of `[1,0,0]`, `[0,1,0]`, |
| 13 | + /// `[0,0,1]`). A zero axis (`[0,0,0]`) is treated as "no rotation". |
| 14 | + InvalidRotationAxis { axis: [f32; 3] }, |
| 15 | + /// Rotation requires a 4x4 matrix. |
| 16 | + InvalidRotationMatrixSize { rows: usize, cols: usize }, |
| 17 | + /// Determinant requires a square matrix. |
| 18 | + NonSquareMatrix { rows: usize, cols: usize }, |
| 19 | + /// Determinant cannot be computed for an empty matrix. |
| 20 | + EmptyMatrix, |
| 21 | + /// Cannot normalize a zero-length vector. |
| 22 | + ZeroLengthVector, |
| 23 | +} |
| 24 | + |
| 25 | +impl fmt::Display for MathError { |
| 26 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 27 | + match self { |
| 28 | + MathError::CrossProductDimension { actual } => { |
| 29 | + return write!(f, "Cross product requires 3D vectors, got {}D", actual); |
| 30 | + } |
| 31 | + MathError::MismatchedVectorDimensions { left, right } => { |
| 32 | + return write!( |
| 33 | + f, |
| 34 | + "Vectors must have matching dimensions (left {}D, right {}D)", |
| 35 | + left, right |
| 36 | + ); |
| 37 | + } |
| 38 | + MathError::InvalidRotationAxis { axis } => { |
| 39 | + return write!(f, "Rotation axis {:?} is not a unit axis vector", axis); |
| 40 | + } |
| 41 | + MathError::InvalidRotationMatrixSize { rows, cols } => { |
| 42 | + return write!( |
| 43 | + f, |
| 44 | + "Rotation requires a 4x4 matrix, got {}x{}", |
| 45 | + rows, cols |
| 46 | + ); |
| 47 | + } |
| 48 | + MathError::NonSquareMatrix { rows, cols } => { |
| 49 | + return write!( |
| 50 | + f, |
| 51 | + "Determinant requires square matrix, got {}x{}", |
| 52 | + rows, cols |
| 53 | + ); |
| 54 | + } |
| 55 | + MathError::EmptyMatrix => { |
| 56 | + return write!(f, "Determinant requires a non-empty matrix"); |
| 57 | + } |
| 58 | + MathError::ZeroLengthVector => { |
| 59 | + return write!(f, "Cannot normalize a zero-length vector"); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl std::error::Error for MathError {} |
0 commit comments