1use std::error::Error as StdError;
2use std::fmt::{Display, Formatter};
3
4macro_rules! impl_error {
5 ($($name: ident ($typ: ty)),*; $($simple_name: ident),*) => {
6 #[non_exhaustive]
11 #[derive(Debug)]
12 pub enum Error {
13 $($simple_name,)*
14 $($name($typ),)*
15 }
16
17 $(
18 impl From<$typ> for Error {
19 fn from(value: $typ) -> Self {
20 Error::$name(value)
21 }
22 }
23 )*
24
25 impl Display for Error {
26 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
27 match self {
28 $(Error::$simple_name => write!(f, stringify!($simple_name)),)*
29 $(Error::$name(v) => Display::fmt(v, f),)*
30 }
31 }
32 }
33
34 impl StdError for Error {}
35 };
36}
37
38impl_error!(
39 Reqwest(reqwest::Error);
40 UnsupportedURLScheme,
41 UnsupportedImageFormat
42);
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47
48 type SendBox = Box<dyn StdError + Send + Sync>;
49
50 #[test]
55 #[should_panic]
56 fn is_send_and_sync() {
57 let _: SendBox = (|| -> Box<Error> { panic!("Success") })(); }
59}