smartcore/error/
mod.rs

1//! # Custom warnings and errors
2use std::error::Error;
3use std::fmt;
4
5#[cfg(feature = "serde")]
6use serde::{Deserialize, Serialize};
7
8/// Generic error to be raised when something goes wrong.
9#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10#[derive(Debug)]
11pub struct Failed {
12    err: FailedError,
13    msg: String,
14}
15
16/// Type of error
17#[non_exhaustive]
18#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
19#[derive(Copy, Clone, Debug)]
20pub enum FailedError {
21    /// Can't fit algorithm to data
22    FitFailed = 1,
23    /// Can't predict new values
24    PredictFailed,
25    /// Can't transform data
26    TransformFailed,
27    /// Can't find an item
28    FindFailed,
29    /// Can't decompose a matrix
30    DecompositionFailed,
31    /// Can't solve for x
32    SolutionFailed,
33    /// Error in input parameters
34    ParametersError,
35    /// Invalid state error (should never happen)
36    InvalidStateError,
37}
38
39impl Failed {
40    ///get type of error
41    #[inline]
42    pub fn error(&self) -> FailedError {
43        self.err
44    }
45
46    /// new instance of `FailedError::FitError`
47    pub fn fit(msg: &str) -> Self {
48        Failed {
49            err: FailedError::FitFailed,
50            msg: msg.to_string(),
51        }
52    }
53    /// new instance of `FailedError::PredictFailed`
54    pub fn predict(msg: &str) -> Self {
55        Failed {
56            err: FailedError::PredictFailed,
57            msg: msg.to_string(),
58        }
59    }
60
61    /// new instance of `FailedError::TransformFailed`
62    pub fn transform(msg: &str) -> Self {
63        Failed {
64            err: FailedError::TransformFailed,
65            msg: msg.to_string(),
66        }
67    }
68
69    /// new instance of `FailedError::ParametersError`
70    pub fn input(msg: &str) -> Self {
71        Failed {
72            err: FailedError::ParametersError,
73            msg: msg.to_string(),
74        }
75    }
76
77    /// new instance of `FailedError::InvalidStateError`
78    pub fn invalid_state(msg: &str) -> Self {
79        Failed {
80            err: FailedError::InvalidStateError,
81            msg: msg.to_string(),
82        }
83    }
84
85    /// new instance of `err`
86    pub fn because(err: FailedError, msg: &str) -> Self {
87        Failed {
88            err,
89            msg: msg.to_string(),
90        }
91    }
92}
93
94impl PartialEq for FailedError {
95    #[inline(always)]
96    fn eq(&self, rhs: &Self) -> bool {
97        *self as u8 == *rhs as u8
98    }
99}
100
101impl PartialEq for Failed {
102    #[inline(always)]
103    fn eq(&self, rhs: &Self) -> bool {
104        self.err == rhs.err && self.msg == rhs.msg
105    }
106}
107
108impl fmt::Display for FailedError {
109    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110        let failed_err_str = match self {
111            FailedError::FitFailed => "Fit failed",
112            FailedError::PredictFailed => "Predict failed",
113            FailedError::TransformFailed => "Transform failed",
114            FailedError::FindFailed => "Find failed",
115            FailedError::DecompositionFailed => "Decomposition failed",
116            FailedError::SolutionFailed => "Can't find solution",
117            FailedError::ParametersError => "Error in input, check parameters",
118            FailedError::InvalidStateError => "Invalid state, this should never happen", // useful in development phase of lib
119        };
120        write!(f, "{failed_err_str}")
121    }
122}
123
124impl fmt::Display for Failed {
125    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126        write!(f, "{}: {}", self.err, self.msg)
127    }
128}
129
130impl Error for Failed {}