concision_neural/
error.rs

1/*
2    Appellation: error <module>
3    Contrib: @FL03
4*/
5//! this module defines the [`ModelError`] type, used to define the various errors encountered
6//! by the different components of a neural network. Additionally, the [`ModelResult`] alias
7//! is defined for convenience, allowing for a more ergonomic way to handle results that may
8//! fail.
9
10#[cfg(feature = "alloc")]
11use alloc::{boxed::Box, string::String};
12
13/// a type alias for a [Result](core::result::Result) configured to use the [`ModelError`]
14/// implementation as its error type.
15pub type ModelResult<T> = core::result::Result<T, ModelError>;
16
17/// The [`ModelError`] type is used to define the various errors encountered by the different
18/// components of a neural network. It is designed to be comprehensive, covering a wide range of
19/// potential issues that may arise during the operation of neural network components, such as
20/// invalid configurations, training failures, and other runtime errors. This error type is
21/// intended to provide a clear and consistent way to handle errors across the neural network
22/// components, making it easier to debug and resolve issues that may occur during the development
23/// and execution of neural network models.
24#[derive(Debug, scsys::VariantConstructors, thiserror::Error)]
25#[non_exhaustive]
26pub enum ModelError {
27    /// The model is not initialized
28    #[error("The model is not initialized")]
29    NotInitialized,
30    #[error("The model is not trained")]
31    /// The model is not trained
32    NotTrained,
33    #[error("Invalid model configuration")]
34    /// The model is not valid
35    InvalidModelConfig,
36    #[error("Unsupported model")]
37    /// The model is not supported
38    UnsupportedModel,
39    #[error("The model is not supported for the given input")]
40    /// The model is not compatible with the given input
41    IncompatibleInput,
42    #[error("An unsupported operation was attempted")]
43    UnsupportedOperation,
44    #[error("Invalid Batch Size")]
45    InvalidBatchSize,
46    #[error("Invalid Input Shape")]
47    InvalidInputShape,
48    #[error("Invalid Output Shape")]
49    InvalidOutputShape,
50    #[error(transparent)]
51    TrainingError(#[from] crate::train::TrainingError),
52    #[error(transparent)]
53    CoreError(#[from] concision_core::error::Error),
54    #[cfg(feature = "alloc")]
55    #[error("Parameter Error")]
56    ParameterError(String),
57}
58
59impl From<ModelError> for concision_core::error::Error {
60    fn from(err: ModelError) -> Self {
61        match err {
62            ModelError::CoreError(e) => e,
63            _ => concision_core::error::Error::box_error(err),
64        }
65    }
66}
67
68#[cfg(feature = "alloc")]
69impl From<Box<dyn core::error::Error + Send + Sync>> for ModelError {
70    fn from(err: Box<dyn core::error::Error + Send + Sync>) -> Self {
71        cnc::Error::BoxError(err).into()
72    }
73}
74#[cfg(feature = "alloc")]
75impl From<String> for ModelError {
76    fn from(err: String) -> Self {
77        cnc::Error::unknown(err).into()
78    }
79}
80
81#[cfg(feature = "alloc")]
82impl From<&str> for ModelError {
83    fn from(err: &str) -> Self {
84        cnc::Error::unknown(err).into()
85    }
86}