zyx_core/
error.rs

1use crate::dtype::DType;
2use alloc::boxed::Box;
3use core::fmt::{Debug, Display, Formatter};
4
5/// ZyxError
6#[derive(Debug)]
7pub enum ZyxError {
8    /// Error returned by backend
9    BackendError(&'static str),
10    /// Compilation error
11    CompileError(Box<dyn Debug>),
12    /// Unexpected dtype found
13    InvalidDType {
14        /// Expected dtype
15        expected: DType,
16        /// Found dtype
17        found: DType,
18    },
19    /// Index out of bounds
20    IndexOutOfBounds {
21        /// Passed index
22        index: usize,
23        /// Actual length
24        len: usize,
25    },
26    /// IO error when writing to or reading from disk
27    #[cfg(feature = "std")]
28    IOError(std::io::Error),
29    /// Parse error
30    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 {}