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