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