use core::fmt;
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
ShapeMismatch {
expected: Vec<usize>,
actual: Vec<usize>,
},
NonContiguous,
InvalidStride,
RankTooLarge {
requested: usize,
max: usize,
},
DimensionTooLarge,
AllocationFailed,
NotConverged,
NumericalFailure(&'static str),
AxisOutOfBounds {
axis: usize,
ndim: usize,
},
IndexOutOfBounds,
}
impl Error {
pub(crate) fn shape(expected: impl Into<Vec<usize>>, actual: impl Into<Vec<usize>>) -> Self {
Self::ShapeMismatch {
expected: expected.into(),
actual: actual.into(),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ShapeMismatch { expected, actual } => {
write!(f, "shape mismatch: expected {expected:?}, got {actual:?}")
}
Self::NonContiguous => write!(f, "array view is not contiguous"),
Self::InvalidStride => write!(f, "invalid stride or offset"),
Self::RankTooLarge { requested, max } => {
write!(f, "rank {requested} exceeds maximum valid rank {max}")
}
Self::DimensionTooLarge => write!(f, "dimension is too large"),
Self::AllocationFailed => write!(f, "allocation failed"),
Self::NotConverged => write!(f, "algorithm did not converge"),
Self::NumericalFailure(msg) => write!(f, "numerical failure: {msg}"),
Self::AxisOutOfBounds { axis, ndim } => {
write!(f, "axis {axis} out of bounds for {ndim} dimensions")
}
Self::IndexOutOfBounds => write!(f, "index out of bounds"),
}
}
}
impl std::error::Error for Error {}