concision_neural/
error.rs

1/*
2    Appellation: error <module>
3    Contrib: @FL03
4*/
5//! this module defines the [`NeuralError`] type, used to define the various errors encountered
6//! by the different components of a neural network. Additionally, the [`NeuralResult`] 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 [`NeuralError`]
14/// implementation as its error type.
15pub type NeuralResult<T> = core::result::Result<T, NeuralError>;
16
17/// The [`NeuralError`] 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 NeuralError {
27    #[error("Invalid Batch Size")]
28    InvalidBatchSize,
29    #[error("Invalid Input Shape")]
30    InvalidInputShape,
31    #[error("Invalid Output Shape")]
32    InvalidOutputShape,
33    #[error(transparent)]
34    TrainingError(#[from] TrainingError),
35    #[error(transparent)]
36    CoreError(#[from] concision_core::error::Error),
37    #[cfg(feature = "alloc")]
38    #[error("Parameter Error")]
39    ParameterError(String),
40}
41
42#[derive(Debug, scsys::VariantConstructors, thiserror::Error)]
43pub enum TrainingError {
44    #[error("Invalid Training Data")]
45    InvalidTrainingData,
46    #[error("Training Failed")]
47    TrainingFailed,
48}
49
50impl From<NeuralError> for concision_core::error::Error {
51    fn from(err: NeuralError) -> Self {
52        match err {
53            NeuralError::CoreError(e) => e,
54            _ => concision_core::error::Error::box_error(err),
55        }
56    }
57}
58
59#[cfg(feature = "alloc")]
60impl From<Box<dyn core::error::Error + Send + Sync>> for NeuralError {
61    fn from(err: Box<dyn core::error::Error + Send + Sync>) -> Self {
62        cnc::Error::BoxError(err).into()
63    }
64}
65#[cfg(feature = "alloc")]
66impl From<String> for NeuralError {
67    fn from(err: String) -> Self {
68        cnc::Error::unknown(err).into()
69    }
70}
71
72#[cfg(feature = "alloc")]
73impl From<&str> for NeuralError {
74    fn from(err: &str) -> Self {
75        cnc::Error::unknown(err).into()
76    }
77}