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
8/// a type alias for a [`Result`](core::result::Result) defined to use the custom [`Error`] as its error type.
9pub type Result<T> = core::result::Result<T, Error>;
10
11#[cfg(feature = "alloc")]
12use alloc::{boxed::Box, string::String};
13/// The [`Error`] type enumerates various errors that can occur within the framework.
14#[derive(Debug, thiserror::Error)]
15#[non_exhaustive]
16pub enum Error {
17    #[error("The provided batch is empty")]
18    EmptyBatch,
19    #[error("Invalid model configuration")]
20    InvalidModelConfig,
21    #[error("The model is not supported for the given input")]
22    IncompatibleInput,
23    #[error("An invalid batch size was provided: {0}")]
24    InvalidBatchSize(usize),
25    #[error("Input is incompatible with the model: found {0} and expected {1}")]
26    InvalidInputFeatures(usize, usize),
27    #[error("The provided dataset has invalid target features: found {0} and expected {1}")]
28    InvalidTargetFeatures(usize, usize),
29    #[error("An uninitialized object was used")]
30    Uninitialized,
31    #[error("The model is not trained")]
32    Untrained,
33    #[cfg(feature = "alloc")]
34    #[error("Unsupported model {0}")]
35    UnsupportedModel(String),
36    #[cfg(feature = "alloc")]
37    #[error("An unsupported operation was attempted: {0}")]
38    UnsupportedOperation(String),
39    #[error("Parameter Error")]
40    ParameterError(String),
41    #[error(transparent)]
42    AnyError(#[from] anyhow::Error),
43    #[cfg(feature = "alloc")]
44    #[error(transparent)]
45    BoxError(#[from] Box<dyn core::error::Error + Send + Sync>),
46    #[error(transparent)]
47    ParamError(#[from] concision_params::ParamsError),
48    #[error(transparent)]
49    #[cfg(feature = "concision_init")]
50    InitError(#[from] concision_init::InitError),
51    #[cfg(feature = "serde")]
52    #[error(transparent)]
53    DeserializeError(#[from] serde::de::value::Error),
54    #[error(transparent)]
55    FmtError(#[from] core::fmt::Error),
56    #[cfg(feature = "serde_json")]
57    #[error(transparent)]
58    JsonError(#[from] serde_json::Error),
59    #[cfg(feature = "std")]
60    #[error(transparent)]
61    IoError(#[from] std::io::Error),
62    #[error(transparent)]
63    ShapeError(#[from] ndarray::ShapeError),
64    #[error("Unknown Error: {0}")]
65    UnknownError(String),
66}
67
68impl From<String> for Error {
69    fn from(value: String) -> Self {
70        Self::UnknownError(value)
71    }
72}
73
74impl From<&str> for Error {
75    fn from(value: &str) -> Self {
76        Self::UnknownError(String::from(value))
77    }
78}