error/
lib.rs

1use std::error::Error as StdError;
2use std::fmt;
3
4#[macro_export]
5macro_rules! error
6{
7    ( $err_type:ident, $msg:literal ) =>
8    {
9        {
10            Error::$err_type(String::from($msg))
11        }
12    };
13    ( $err_type:ident, $msg:literal $(, $x:expr)+) =>
14    {
15        {
16            Error::$err_type(format!($msg $(, $x)+))
17        }
18    };
19}
20
21// Construct a RuntimeError
22#[macro_export]
23macro_rules! rterr
24{
25    ($msg:literal $(, $x:expr)*) =>
26    {
27        error!(RuntimeError, $msg $(, $x)*)
28    };
29}
30
31// Construct an XMLError
32#[macro_export]
33macro_rules! xmlerr
34{
35    ($msg:literal $(, $x:expr)*) =>
36    {
37        error!(XMLError, $msg $(, $x)*)
38    };
39}
40
41#[derive(Debug, Clone)]
42pub enum Error
43{
44    /// An error from the underlying data source. This could be a
45    /// database connection issue, or disk I/O failure, or invalid
46    /// data from the data source, etc. This is not a “logic error”
47    /// such as an error from generating SQL statement due to invalid
48    /// backlinks.
49    RuntimeError(String),
50    XMLError(String),
51}
52
53impl fmt::Display for Error
54{
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
56    {
57        match self
58        {
59            Error::RuntimeError(msg) => write!(f, "Runtime error: {}", msg),
60            Error::XMLError(msg) => write!(f, "XML error: {}", msg),
61        }
62    }
63}
64
65impl StdError for Error
66{
67    fn source(&self) -> Option<&(dyn StdError + 'static)> {None}
68}