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