#[derive(Debug, PartialEq)]
pub enum Error {
IncompatibleSizeError((usize, usize), usize),
IncompatibleShape((usize, usize), (usize, usize)),
OutOfBoundary(usize, usize),
DividedByZero,
SingularMatrix,
NoSolution(usize, usize),
Message(String),
}
pub type Result<T> = std::result::Result<T, Error>;
impl std::error::Error for Error {}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::IncompatibleSizeError((required_row, required_col), real) => write!(
f,
"incompatible size while require ({:?} x {:?}) but the real size is {:?}",
required_row, required_col, real
),
Error::IncompatibleShape(required, real) => {
write!(
f,
"incompatible shape while require {:?} but the real shape is {:?}",
required, real
)
}
Error::OutOfBoundary(row, col) => {
write!(f, "out of boundary at ({:?}, {:?})", row, col)
}
Error::DividedByZero => write!(f, "can not divide by zero"),
Error::SingularMatrix => write!(f, "matrix is strange which determinant is zero"),
Error::NoSolution(row, col) => {
write!(f, "linear equation {:?}x{:?} has no solution", row, col)
}
Error::Message(msg) => write!(f, "{:?}", msg),
}
}
}