1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::error;
use std::fmt::{Display, Formatter, Result};

#[derive(Debug, PartialEq, Eq)]
pub enum Error {
    TypeError(String),
    DeserializeError(String),
}

impl Error {
    pub fn type_error(expected: &str) -> Self {
        Error::TypeError(expected.into())
    }

    pub fn deserialize_error(msg: String) -> Self {
        Error::DeserializeError(msg)
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        use Error::*;

        match *self {
            TypeError(ref s) => write!(f, "Type error, expected: {s}"),
            DeserializeError(ref s) => write!(f, "Deserialize error: {s}"),
        }
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        use Error::*;

        match *self {
            TypeError(_) => "Type error",
            DeserializeError(_) => "Deserialize error",
        }
    }
}

impl From<json::Error> for Error {
    fn from(err: json::Error) -> Self {
        Error::deserialize_error(err.to_string())
    }
}