use crate::dtype::DType;
use alloc::boxed::Box;
use core::fmt::{Debug, Display, Formatter};
#[derive(Debug)]
pub enum ZyxError {
BackendError(&'static str),
CompileError(Box<dyn Debug>),
InvalidDType {
expected: DType,
found: DType,
},
IndexOutOfBounds {
index: usize,
len: usize,
},
#[cfg(feature = "std")]
IOError(std::io::Error),
ParseError(alloc::string::String),
}
impl Display for ZyxError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
ZyxError::BackendError(err) => f.write_fmt(format_args!("{err:?}")),
ZyxError::CompileError(err) => f.write_fmt(format_args!(
"compiled backend could not compile this program:\n{err:?}"
)),
ZyxError::InvalidDType { expected, found } => f.write_fmt(format_args!(
"invalid dtype: expected {expected:?} but found {found:?}."
)),
ZyxError::IndexOutOfBounds { index, len } => f.write_fmt(format_args!(
"range out of bounds: the index is {index}, but the len is {len}"
)),
ZyxError::IOError(err) => f.write_fmt(format_args!("{err}")),
ZyxError::ParseError(err) => f.write_fmt(format_args!("{err}")),
}
}
}
#[cfg(feature = "std")]
impl From<std::io::Error> for ZyxError {
fn from(err: std::io::Error) -> Self {
Self::IOError(err)
}
}
#[cfg(feature = "std")]
impl std::error::Error for ZyxError {}