addition: error implementation
This commit is contained in:
42
src/lib.rs
42
src/lib.rs
@@ -1,6 +1,7 @@
|
|||||||
use fractions::{Fraction, FractionError};
|
use fractions::{Fraction, FractionError};
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
use std::fmt::{Debug, Display};
|
use std::error::Error;
|
||||||
|
use std::fmt::{self, Debug, Display};
|
||||||
use std::ops::Add;
|
use std::ops::Add;
|
||||||
use std::ops::Mul;
|
use std::ops::Mul;
|
||||||
use std::ops::Sub;
|
use std::ops::Sub;
|
||||||
@@ -27,6 +28,45 @@ impl From<FractionError> for MatrixError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for MatrixError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
MatrixError::IndexOutOfRange => write!(f, "Index out of range"),
|
||||||
|
MatrixError::RowOutOfRange => write!(f, "Row index out of range"),
|
||||||
|
MatrixError::ColumnOutOfRange => write!(f, "Column index out of range"),
|
||||||
|
MatrixError::NotSquared => write!(f, "Matrix must be square"),
|
||||||
|
MatrixError::InvalidDataSize => write!(f, "Invalid data size for matrix"),
|
||||||
|
MatrixError::InvalidSizeForAdd => {
|
||||||
|
write!(f, "Matrices must have same dimensions for addition")
|
||||||
|
}
|
||||||
|
MatrixError::InvalidSizeForSub => {
|
||||||
|
write!(f, "Matrices must have same dimensions for subtraction")
|
||||||
|
}
|
||||||
|
MatrixError::InvalidSizeForMul => {
|
||||||
|
write!(f, "Invalid matrix dimensions for multiplication")
|
||||||
|
}
|
||||||
|
MatrixError::ZeroSize => write!(f, "Matrix cannot have zero rows or columns"),
|
||||||
|
MatrixError::FailedGauss => {
|
||||||
|
write!(f, "Gaussian elimination failed (possibly singular matrix)")
|
||||||
|
}
|
||||||
|
MatrixError::FailedGaussJordan => write!(
|
||||||
|
f,
|
||||||
|
"Gauss-Jordan elimination failed (possibly singular matrix)"
|
||||||
|
),
|
||||||
|
MatrixError::FractionError(err) => write!(f, "Fraction error: {}", err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error for MatrixError {
|
||||||
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||||
|
match self {
|
||||||
|
MatrixError::FractionError(err) => Some(err),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Debug, Clone)]
|
#[derive(PartialEq, Eq, Debug, Clone)]
|
||||||
pub struct Matrix {
|
pub struct Matrix {
|
||||||
rows: usize,
|
rows: usize,
|
||||||
|
|||||||
Reference in New Issue
Block a user