rstsr_common/
error.rs

1#[cfg(feature = "std")]
2extern crate std;
3
4use crate::prelude_dev::*;
5use core::convert::Infallible;
6use core::num::TryFromIntError;
7use derive_builder::UninitializedFieldError;
8use std::alloc::LayoutError;
9use std::collections::TryReserveError;
10
11#[non_exhaustive]
12#[derive(Debug)]
13pub enum Error {
14    ValueOutOfRange(String),
15    InvalidValue(String),
16    InvalidLayout(String),
17    RuntimeError(String),
18    DeviceMismatch(String),
19    UnImplemented(String),
20    MemoryError(String),
21
22    TryFromIntError(String),
23    Infallible,
24
25    BuilderError(UninitializedFieldError),
26    DeviceError(String),
27    RayonError(String),
28
29    ErrorCode(i32, String),
30
31    Miscellaneous(String),
32}
33
34impl core::fmt::Display for Error {
35    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36        Debug::fmt(self, f)
37    }
38}
39
40#[cfg(feature = "std")]
41impl std::error::Error for Error {}
42
43pub type Result<E> = core::result::Result<E, Error>;
44
45impl From<TryFromIntError> for Error {
46    fn from(e: TryFromIntError) -> Self {
47        Error::TryFromIntError(format!("{e:?}"))
48    }
49}
50
51impl From<Infallible> for Error {
52    fn from(_: Infallible) -> Self {
53        Error::Infallible
54    }
55}
56
57#[cfg(feature = "rayon")]
58impl From<rayon::ThreadPoolBuildError> for Error {
59    fn from(e: rayon::ThreadPoolBuildError) -> Self {
60        Error::RayonError(format!("{e:?}"))
61    }
62}
63
64impl From<UninitializedFieldError> for Error {
65    fn from(e: UninitializedFieldError) -> Self {
66        Error::BuilderError(e)
67    }
68}
69
70impl From<TryReserveError> for Error {
71    fn from(e: TryReserveError) -> Self {
72        Error::MemoryError(format!("{e:?}"))
73    }
74}
75
76impl From<LayoutError> for Error {
77    fn from(e: LayoutError) -> Self {
78        Error::MemoryError(format!("{e:?}"))
79    }
80}
81
82#[macro_export]
83macro_rules! rstsr_assert {
84    ($cond:expr, $errtype:ident) => {
85        if $cond {
86            Ok(())
87        } else {
88            use $crate::prelude_dev::*;
89            let mut s = String::new();
90            write!(s, concat!(file!(), ":", line!(), ": ")).unwrap();
91            write!(s, concat!("Error::", stringify!($errtype))).unwrap();
92            write!(s, " : {:}", stringify!($cond)).unwrap();
93            Err(Error::$errtype(s))
94        }
95    };
96    ($cond:expr, $errtype:ident, $($arg:tt)*) => {{
97        if $cond {
98            Ok(())
99        } else {
100            use $crate::prelude_dev::*;
101            let mut s = String::new();
102            write!(s, concat!(file!(), ":", line!(), ": ")).unwrap();
103            write!(s, concat!("Error::", stringify!($errtype))).unwrap();
104            write!(s, " : ").unwrap();
105            write!(s, $($arg)*).unwrap();
106            write!(s, " : {:}", stringify!($cond)).unwrap();
107            Err(Error::$errtype(s))
108        }
109    }};
110}
111
112#[macro_export]
113macro_rules! rstsr_assert_eq {
114    ($lhs:expr, $rhs:expr, $errtype:ident) => {
115        if $lhs == $rhs {
116            Ok(())
117        } else {
118            use $crate::prelude_dev::*;
119            let mut s = String::new();
120            write!(s, concat!(file!(), ":", line!(), ": ")).unwrap();
121            write!(s, concat!("Error::", stringify!($errtype))).unwrap();
122            write!(
123                s,
124                " : {:} = {:?} not equal to {:} = {:?}",
125                stringify!($lhs),
126                $lhs,
127                stringify!($rhs),
128                $rhs
129            )
130            .unwrap();
131            Err(Error::$errtype(s))
132        }
133    };
134    ($lhs:expr, $rhs:expr, $errtype:ident, $($arg:tt)*) => {
135        if $lhs == $rhs {
136            Ok(())
137        } else {
138            use $crate::prelude_dev::*;
139            let mut s = String::new();
140            write!(s, concat!(file!(), ":", line!(), ": ")).unwrap();
141            write!(s, concat!("Error::", stringify!($errtype))).unwrap();
142            write!(s, " : ").unwrap();
143            write!(s, $($arg)*).unwrap();
144            write!(
145                s,
146                " : {:} = {:?} not equal to {:} = {:?}",
147                stringify!($lhs),
148                $lhs,
149                stringify!($rhs),
150                $rhs
151            )
152            .unwrap();
153            Err(Error::$errtype(s))
154        }
155    };
156}
157
158#[macro_export]
159macro_rules! rstsr_invalid {
160    ($word:expr) => {{
161        use core::fmt::Write;
162        let mut s = String::new();
163        write!(s, concat!(file!(), ":", line!(), ": ")).unwrap();
164        write!(s, "Error::InvalidValue").unwrap();
165        write!(s, " : {:?} = {:?}", stringify!($word), $word).unwrap();
166        Err(Error::InvalidValue(s))
167    }};
168    ($word:expr, $($arg:tt)*) => {{
169        use core::fmt::Write;
170        let mut s = String::new();
171        write!(s, concat!(file!(), ":", line!(), ": ")).unwrap();
172        write!(s, "Error::InvalidValue").unwrap();
173        write!(s, " : {:?} = {:?}", stringify!($word), $word).unwrap();
174        write!(s, " : ").unwrap();
175        write!(s, $($arg)*).unwrap();
176        Err(Error::InvalidValue(s))
177    }};
178}
179
180#[macro_export]
181macro_rules! rstsr_errcode {
182    ($word:expr) => {{
183        use core::fmt::Write;
184        let mut s = String::new();
185        write!(s, concat!(file!(), ":", line!(), ": ")).unwrap();
186        write!(s, "Error::ErrorCode").unwrap();
187        write!(s, " : {:?}", $word).unwrap();
188        Err(Error::ErrorCode($word, s))
189    }};
190    ($word:expr, $($arg:tt)*) => {{
191        use core::fmt::Write;
192        let mut s = String::new();
193        write!(s, concat!(file!(), ":", line!(), ": ")).unwrap();
194        write!(s, "Error::ErrorCode").unwrap();
195        write!(s, " : {:?}", $word).unwrap();
196        write!(s, " : ").unwrap();
197        write!(s, $($arg)*).unwrap();
198        Err(Error::ErrorCode($word, s))
199    }};
200}
201
202#[macro_export]
203macro_rules! rstsr_error {
204    ($errtype:ident) => {{
205        use $crate::prelude_dev::*;
206        let mut s = String::new();
207        write!(s, concat!(file!(), ":", line!(), ": ")).unwrap();
208        write!(s, concat!("Error::", stringify!($errtype))).unwrap();
209        Error::$errtype(s)
210    }};
211    ($errtype:ident, $($arg:tt)*) => {{
212        use $crate::prelude_dev::*;
213        let mut s = String::new();
214        write!(s, concat!(file!(), ":", line!(), ": ")).unwrap();
215        write!(s, concat!("Error::", stringify!($errtype))).unwrap();
216        write!(s, " : ").unwrap();
217        write!(s, $($arg)*).unwrap();
218        Error::$errtype(s)
219    }};
220}
221
222#[macro_export]
223macro_rules! rstsr_raise {
224    ($errtype:ident) => {{
225        use $crate::prelude_dev::*;
226        let mut s = String::new();
227        write!(s, concat!(file!(), ":", line!(), ": ")).unwrap();
228        write!(s, concat!("Error::", stringify!($errtype))).unwrap();
229        Err(Error::$errtype(s))
230    }};
231    ($errtype:ident, $($arg:tt)*) => {{
232        use $crate::prelude_dev::*;
233        let mut s = String::new();
234        write!(s, concat!(file!(), ":", line!(), ": ")).unwrap();
235        write!(s, concat!("Error::", stringify!($errtype))).unwrap();
236        write!(s, " : ").unwrap();
237        write!(s, $($arg)*).unwrap();
238        Err(Error::$errtype(s))
239    }};
240}
241
242#[macro_export]
243macro_rules! rstsr_pattern {
244    ($value:expr, $pattern:expr, $errtype:ident) => {
245        if ($pattern).contains(&($value)) {
246            Ok(())
247        } else {
248            use $crate::prelude_dev::*;
249            let mut s = String::new();
250            write!(s, concat!(file!(), ":", line!(), ": ")).unwrap();
251            write!(s, concat!("Error::", stringify!($errtype))).unwrap();
252            write!(
253                s,
254                " : {:?} = {:?} not match to pattern {:} = {:?}",
255                stringify!($value),
256                $value,
257                stringify!($pattern),
258                $pattern
259            )
260            .unwrap();
261            Err(Error::$errtype(s))
262        }
263    };
264    ($value:expr, $pattern:expr, $errtype:ident, $($arg:tt)*) => {
265        if ($pattern).contains(&($value)) {
266            Ok(())
267        } else {
268            use $crate::prelude_dev::*;
269            let mut s = String::new();
270            write!(s, concat!(file!(), ":", line!(), ": ")).unwrap();
271            write!(s, concat!("Error::", stringify!($errtype))).unwrap();
272            write!(s, " : ").unwrap();
273            write!(s, $($arg)*).unwrap();
274            write!(
275                s,
276                " : {:?} = {:?} not match to pattern {:} = {:?}",
277                stringify!($value),
278                $value,
279                stringify!($pattern),
280                $pattern
281            )
282            .unwrap();
283            Err(Error::$errtype(s))
284        }
285    };
286}