1 use serde::de::Error as DeError;
2 use serde::ser::Error as SerError;
3 use std::fmt::Display;
4 use thiserror::Error;
5 
6 #[derive(Debug, Error)]
7 pub enum Error {
8     #[error("Expected token {token}, found {found}")]
9     UnexpectedToken { token: String, found: String },
10     #[error("custom: {field}")]
11     Custom { field: String },
12     #[error("unsupported operation: '{operation}'")]
13     UnsupportedOperation { operation: String },
14 
15     #[error("IO error: {source}")]
16     Io {
17         #[from]
18         source: ::std::io::Error,
19     },
20 
21     #[error("FromUtf8Error: {source}")]
22     FromUtf8Error {
23         #[from]
24         source: ::std::string::FromUtf8Error,
25     },
26 
27     #[error("ParseIntError: {source}")]
28     ParseIntError {
29         #[from]
30         source: ::std::num::ParseIntError,
31     },
32 
33     #[error("ParseFloatError: {source}")]
34     ParseFloatError {
35         #[from]
36         source: ::std::num::ParseFloatError,
37     },
38 
39     #[error("ParseBoolError: {source}")]
40     ParseBoolError {
41         #[from]
42         source: ::std::str::ParseBoolError,
43     },
44 
45     #[error("Syntax: {source}")]
46     Syntax {
47         #[from]
48         source: ::xml::reader::Error,
49     },
50 
51     #[error("Writer: {source}")]
52     Writer {
53         #[from]
54         source: ::xml::writer::Error,
55     },
56 }
57 
58 pub type Result<T> = std::result::Result<T, Error>;
59 
60 #[macro_export]
61 macro_rules! expect {
62     ($actual: expr, $($expected: pat)|+ => $if_ok: expr) => {
63         match $actual {
64             $($expected)|+ => $if_ok,
65             actual => Err($crate::Error::UnexpectedToken {
66                 token: stringify!($($expected)|+).to_string(),
67                 found: format!("{:?}",actual)
68             }) as Result<_>
69         }
70     }
71 }
72 
73 #[cfg(debug_assertions)]
74 #[macro_export]
75 macro_rules! debug_expect {
76     ($actual: expr, $($expected: pat)|+ => $if_ok: expr) => {
77         match $actual {
78             $($expected)|+ => $if_ok,
79             actual => panic!(
80                 "Internal error: Expected token {}, found {:?}",
81                 stringify!($($expected)|+),
82                 actual
83             )
84         }
85     }
86 }
87 
88 #[cfg(not(debug_assertions))]
89 #[macro_export]
90 macro_rules! debug_expect {
91     ($actual: expr, $($expected: pat)|+ => $if_ok: expr) => {
92         match $actual {
93             $($expected)|+ => $if_ok,
94             _ => unreachable!()
95         }
96     }
97 }
98 
99 impl DeError for Error {
custom<T: Display>(msg: T) -> Self100     fn custom<T: Display>(msg: T) -> Self {
101         Error::Custom {
102             field: msg.to_string(),
103         }
104     }
105 }
106 
107 impl SerError for Error {
custom<T: Display>(msg: T) -> Self108     fn custom<T: Display>(msg: T) -> Self {
109         Error::Custom {
110             field: msg.to_string(),
111         }
112     }
113 }
114