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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::fmt;
use std::io;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorKind {
    Io(io::ErrorKind),
    Serialize,
    Deserialize,
}

#[derive(Debug)]
pub struct Error {
    kind: ErrorKind,
    inner: ErrorInner,
}

#[derive(Debug)]
enum ErrorInner {
    Io(io::Error),
    Other(String),
}

impl Error {
    pub(crate) fn deserialize<S: Into<String>>(message: S) -> Error {
        Error {
            kind: ErrorKind::Deserialize,
            inner: ErrorInner::Other(message.into()),
        }
    }

    pub fn kind(&self) -> ErrorKind {
        self.kind
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.inner {
            ErrorInner::Io(ref err) => write!(f, "i/o error: {}", err),
            ErrorInner::Other(ref msg) => {
                write!(f, "{}: {}", ::std::error::Error::description(self), msg)
            }
        }
    }
}

impl ::std::error::Error for Error {
    fn description(&self) -> &str {
        match self.kind {
            ErrorKind::Io(_) => "i/o error",
            ErrorKind::Serialize => "serialize error",
            ErrorKind::Deserialize => "deserialize error",
        }
    }
}

impl ::serde::de::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Error {
        Error {
            kind: ErrorKind::Deserialize,
            inner: ErrorInner::Other(msg.to_string()),
        }
    }
}

impl ::serde::ser::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Error {
        Error {
            kind: ErrorKind::Serialize,
            inner: ErrorInner::Other(msg.to_string()),
        }
    }
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Error {
        Error {
            kind: ErrorKind::Io(err.kind()),
            inner: ErrorInner::Io(err),
        }
    }
}