linalg_rs/
error.rs

1//! Errors on matrices
2#![warn(missing_docs)]
3
4use std::fmt::{Display, Formatter, Result};
5
6#[derive(Debug, PartialEq)]
7/// Common Matrix errors that can occur
8pub enum MatrixError {
9    /// Upon creation of a matrix, this could occur
10    MatrixCreationError,
11    /// Index out of bound error
12    MatrixIndexOutOfBoundsError,
13    /// This can only happen on matmul, where if the 2 matrices are not in the form of
14    /// (M x N) @ (N x P) then this error will occur.
15    MatrixMultiplicationDimensionMismatchError,
16    /// Occurs on matrix operations where there is a dimension mismatch between
17    /// the two matrices.
18    MatrixDimensionMismatchError,
19    /// Concatination Error
20    MatrixConcatinationError,
21    /// If reading matrix from file and an error occurs,
22    /// this will be thrown
23    MatrixParseError,
24    /// Divide by zero
25    MatrixDivideByZeroError,
26    /// File read error
27    MatrixFileReadError(&'static str),
28}
29
30impl Display for MatrixError {
31    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
32        match self {
33            MatrixError::MatrixCreationError => {
34                write!(f, "There was an error creating the matrix.")
35            }
36            MatrixError::MatrixIndexOutOfBoundsError => {
37                write!(f, "The indexes are out of bounds for the matrix")
38            }
39            MatrixError::MatrixMultiplicationDimensionMismatchError => {
40                write!(
41                    f,
42                    "The two matrices supplied are not on the form M x N @ N x P"
43                )
44            }
45            MatrixError::MatrixDimensionMismatchError => {
46                write!(f, "The matrixs provided are both not on the form M x N")
47            }
48
49            MatrixError::MatrixConcatinationError => {
50                write!(
51                    f,
52                    "Matrixs could not be concatinated or extended due to more than 1 dim mismatch"
53                )
54            }
55            MatrixError::MatrixParseError => write!(f, "Failed to parse matrix from file"),
56            MatrixError::MatrixDivideByZeroError => write!(f, "Tried to divide by zero"),
57            MatrixError::MatrixFileReadError(path) => {
58                write!(f, "Could not read file from path: {}", path)
59            }
60        }
61    }
62}