use core::fmt;
#[cfg_attr(
feature = "serde-support",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum CoreError {
DimensionMismatch {
expected: Vec<usize>,
got: Vec<usize>,
},
InvalidShape {
shape: Vec<usize>,
reason: &'static str,
},
AxisOutOfBounds { axis: usize, ndim: usize },
IndexOutOfBounds {
index: Vec<usize>,
shape: Vec<usize>,
},
SingularMatrix,
InvalidArgument { reason: &'static str },
BroadcastError {
shape_a: Vec<usize>,
shape_b: Vec<usize>,
},
}
impl fmt::Display for CoreError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::DimensionMismatch { expected, got } => {
write!(f, "dimension mismatch: expected {expected:?}, got {got:?}")
}
Self::InvalidShape { shape, reason } => {
write!(f, "invalid shape {shape:?}: {reason}")
}
Self::AxisOutOfBounds { axis, ndim } => {
write!(
f,
"axis {axis} out of bounds for tensor with {ndim} dimensions"
)
}
Self::IndexOutOfBounds { index, shape } => {
write!(f, "index {index:?} out of bounds for shape {shape:?}")
}
Self::SingularMatrix => write!(f, "singular matrix"),
Self::InvalidArgument { reason } => write!(f, "invalid argument: {reason}"),
Self::BroadcastError { shape_a, shape_b } => {
write!(f, "cannot broadcast shapes {shape_a:?} and {shape_b:?}")
}
}
}
}
impl std::error::Error for CoreError {}
pub type Result<T> = std::result::Result<T, CoreError>;