1use crate::dtype::DType;
2use alloc::boxed::Box;
3use core::fmt::{Debug, Display, Formatter};
4
5#[derive(Debug)]
7pub enum ZyxError {
8 BackendError(&'static str),
10 CompileError(Box<dyn Debug>),
12 InvalidDType {
14 expected: DType,
16 found: DType,
18 },
19 IndexOutOfBounds {
21 index: usize,
23 len: usize,
25 },
26 #[cfg(feature = "std")]
28 IOError(std::io::Error),
29 ParseError(alloc::string::String),
31}
32
33impl Display for ZyxError {
34 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
35 match self {
36 ZyxError::BackendError(err) => f.write_fmt(format_args!("{err:?}")),
37 ZyxError::CompileError(err) => f.write_fmt(format_args!(
38 "compiled backend could not compile this program:\n{err:?}"
39 )),
40 ZyxError::InvalidDType { expected, found } => f.write_fmt(format_args!(
41 "invalid dtype: expected {expected:?} but found {found:?}."
42 )),
43 ZyxError::IndexOutOfBounds { index, len } => f.write_fmt(format_args!(
44 "range out of bounds: the index is {index}, but the len is {len}"
45 )),
46 ZyxError::IOError(err) => f.write_fmt(format_args!("{err}")),
47 ZyxError::ParseError(err) => f.write_fmt(format_args!("{err}")),
48 }
49 }
50}
51
52#[cfg(feature = "std")]
53impl From<std::io::Error> for ZyxError {
54 fn from(err: std::io::Error) -> Self {
55 Self::IOError(err)
56 }
57}
58
59#[cfg(feature = "std")]
60impl std::error::Error for ZyxError {}