query_params_serialize/
errors.rs

1//! Serializer errors.
2
3#[derive(Fail, Debug)]
4enum InnerError {
5    /// Standard IO error.
6    #[fail(display = "i/o error: {:?}", _0)]
7    Io(#[cause] std::io::Error),
8
9    /// Standard format error.
10    #[fail(display = "fmt error: {:?}", _0)]
11    Fmt(#[cause] std::fmt::Error),
12
13    /// Custom error.
14    #[fail(display = "serde: {:?}", _0)]
15    Custom(String),
16}
17
18#[derive(Debug)]
19pub struct Error {
20    inner: InnerError,
21}
22
23impl std::error::Error for Error {}
24
25impl serde::ser::Error for Error {
26    fn custom<T: std::fmt::Display>(msg: T) -> Self {
27        Error {
28            inner: InnerError::Custom(msg.to_string()),
29        }
30    }
31}
32
33impl std::fmt::Display for Error {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        write!(f, "{:?}", self.inner)
36    }
37}
38
39macro_rules! err_converter {
40    ( $a:ident, $b:ty ) => {
41        impl From<$b> for InnerError {
42            fn from(e: $b) -> Self {
43                InnerError::$a(e)
44            }
45        }
46
47        impl From<$b> for Error {
48            fn from(e: $b) -> Self {
49                Error { inner: e.into() }
50            }
51        }
52    };
53}
54
55pub type Result<T> = std::result::Result<T, Error>;
56
57err_converter!(Io, std::io::Error);
58err_converter!(Fmt, std::fmt::Error);