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::{boxed::Box, string::String};
9
10/// a type alias for a [Result] with a [Error]
11pub type Result<T = ()> = core::result::Result<T, Error>;
12
13/// The [`Error`] type enumerates various errors that can occur within the framework.
14#[derive(Debug, thiserror::Error)]
15pub enum Error {
16    #[error("Invalid Shape")]
17    InvalidShape,
18    #[error(transparent)]
19    PadError(#[from] crate::ops::pad::error::PadError),
20    #[error(transparent)]
21    ParamError(#[from] crate::params::error::ParamsError),
22    #[cfg(feature = "anyhow")]
23    #[error(transparent)]
24    AnyError(#[from] anyhow::Error),
25    #[cfg(feature = "alloc")]
26    #[error(transparent)]
27    BoxError(#[from] Box<dyn core::error::Error + Send + Sync + 'static>),
28    #[cfg(feature = "serde")]
29    #[error(transparent)]
30    DeserializeError(#[from] serde::de::value::Error),
31    #[error(transparent)]
32    FmtError(#[from] core::fmt::Error),
33    #[cfg(feature = "serde_json")]
34    #[error(transparent)]
35    JsonError(#[from] serde_json::Error),
36    #[cfg(feature = "std")]
37    #[error(transparent)]
38    IoError(#[from] std::io::Error),
39    #[error(transparent)]
40    ShapeError(#[from] ndarray::ShapeError),
41    #[cfg(feature = "rand")]
42    #[error(transparent)]
43    UniformError(#[from] rand_distr::uniform::Error),
44    #[cfg(feature = "alloc")]
45    #[error("Unknown Error: {0}")]
46    Unknown(String),
47    #[error(transparent)]
48    UtilError(#[from] concision_utils::UtilityError),
49}
50
51#[cfg(feature = "alloc")]
52impl From<String> for Error {
53    fn from(value: String) -> Self {
54        Self::Unknown(value)
55    }
56}
57
58#[cfg(feature = "alloc")]
59impl From<&str> for Error {
60    fn from(value: &str) -> Self {
61        Self::Unknown(String::from(value))
62    }
63}