breed_planner/
error.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#[derive(Debug, Clone)]
32pub enum Error
33{
34    /// An error from the underlying data source. This could be a
35    /// database connection issue, or disk I/O failure, or invalid
36    /// data from the data source, etc. This is not a “logic error”
37    /// such as an error from generating SQL statement due to invalid
38    /// backlinks.
39    RuntimeError(String),
40    FormatError(String),
41}
42
43impl fmt::Display for Error
44{
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
46    {
47        match self
48        {
49            Error::RuntimeError(msg) => write!(f, "Runtime error: {}", msg),
50            Error::FormatError(msg) => write!(f, "Format error: {}", msg),
51        }
52    }
53}
54
55impl StdError for Error
56{
57    fn source(&self) -> Option<&(dyn StdError + 'static)> {None}
58}