1use std::error::Error;
3use std::fmt;
4
5#[cfg(feature = "serde")]
6use serde::{Deserialize, Serialize};
7
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10#[derive(Debug)]
11pub struct Failed {
12 err: FailedError,
13 msg: String,
14}
15
16#[non_exhaustive]
18#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
19#[derive(Copy, Clone, Debug)]
20pub enum FailedError {
21 FitFailed = 1,
23 PredictFailed,
25 TransformFailed,
27 FindFailed,
29 DecompositionFailed,
31 SolutionFailed,
33 ParametersError,
35 InvalidStateError,
37}
38
39impl Failed {
40 #[inline]
42 pub fn error(&self) -> FailedError {
43 self.err
44 }
45
46 pub fn fit(msg: &str) -> Self {
48 Failed {
49 err: FailedError::FitFailed,
50 msg: msg.to_string(),
51 }
52 }
53 pub fn predict(msg: &str) -> Self {
55 Failed {
56 err: FailedError::PredictFailed,
57 msg: msg.to_string(),
58 }
59 }
60
61 pub fn transform(msg: &str) -> Self {
63 Failed {
64 err: FailedError::TransformFailed,
65 msg: msg.to_string(),
66 }
67 }
68
69 pub fn input(msg: &str) -> Self {
71 Failed {
72 err: FailedError::ParametersError,
73 msg: msg.to_string(),
74 }
75 }
76
77 pub fn invalid_state(msg: &str) -> Self {
79 Failed {
80 err: FailedError::InvalidStateError,
81 msg: msg.to_string(),
82 }
83 }
84
85 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", };
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 {}