concision_core/
error.rs

1/*
2    Appellation: error <module>
3    Contrib: @FL03
4*/
5//! This module implements the core [`Error`] type for the framework and provides a [`Result`]
6//! type alias for convenience.
7#[cfg(feature = "alloc")]
8use alloc::{
9    boxed::Box,
10    string::{String, ToString},
11};
12
13#[allow(dead_code)]
14/// a type alias for a [Result](core::result::Result) configured with an [`Error`] as its error
15/// type.
16pub type Result<T> = core::result::Result<T, Error>;
17
18/// The [`Error`] type enumerates various errors that can occur within the framework.
19#[derive(Debug, thiserror::Error)]
20pub enum Error {
21    #[error("Invalid Shape")]
22    InvalidShape,
23    #[error(transparent)]
24    PadError(#[from] crate::ops::pad::error::PadError),
25    #[error(transparent)]
26    ParamError(#[from] crate::params::error::ParamsError),
27    #[error(transparent)]
28    #[cfg(feature = "cnc_init")]
29    InitError(#[from] concision_init::error::InitError),
30    #[error(transparent)]
31    TensorError(#[from] ndtensor::error::TensorError),
32    #[error(transparent)]
33    #[cfg(feature = "cnc_utils")]
34    UtilityError(#[from] concision_utils::error::UtilityError),
35    #[cfg(feature = "anyhow")]
36    #[error(transparent)]
37    AnyError(#[from] anyhow::Error),
38    #[cfg(feature = "alloc")]
39    #[error(transparent)]
40    BoxError(#[from] Box<dyn core::error::Error + Send + Sync>),
41    #[cfg(feature = "serde")]
42    #[error(transparent)]
43    DeserializeError(#[from] serde::de::value::Error),
44    #[error(transparent)]
45    FmtError(#[from] core::fmt::Error),
46    #[cfg(feature = "serde_json")]
47    #[error(transparent)]
48    JsonError(#[from] serde_json::Error),
49    #[cfg(feature = "std")]
50    #[error(transparent)]
51    IoError(#[from] std::io::Error),
52    #[error(transparent)]
53    ShapeError(#[from] ndarray::ShapeError),
54    #[error(transparent)]
55    #[cfg(feature = "rand")]
56    UniformError(#[from] rand_distr::uniform::Error),
57    #[cfg(feature = "alloc")]
58    #[error("Unknown Error: {0}")]
59    Unknown(String),
60}
61
62#[cfg(feature = "alloc")]
63impl Error {
64    /// create a new [`BoxError`](Error::BoxError) variant using the given error
65    pub fn box_error<E>(error: E) -> Self
66    where
67        E: 'static + Send + Sync + core::error::Error,
68    {
69        Self::BoxError(Box::new(error))
70    }
71    /// create a new [`Unknown`](Error::Unknown) variant using the given string
72    pub fn unknown<S>(error: S) -> Self
73    where
74        S: ToString,
75    {
76        Self::Unknown(error.to_string())
77    }
78}
79#[cfg(feature = "alloc")]
80impl From<String> for Error {
81    fn from(value: String) -> Self {
82        Self::Unknown(value)
83    }
84}
85
86#[cfg(feature = "alloc")]
87impl From<&str> for Error {
88    fn from(value: &str) -> Self {
89        Self::Unknown(String::from(value))
90    }
91}