1use std::fmt;
2use std::io;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum ErrorKind {
6 Io(io::ErrorKind),
7 Serialize,
8 Deserialize,
9}
10
11#[derive(Debug)]
12pub struct Error {
13 kind: ErrorKind,
14 inner: ErrorInner,
15}
16
17#[derive(Debug)]
18enum ErrorInner {
19 Io(io::Error),
20 Other(String),
21}
22
23impl Error {
24 pub(crate) fn deserialize<S: Into<String>>(message: S) -> Error {
25 Error {
26 kind: ErrorKind::Deserialize,
27 inner: ErrorInner::Other(message.into()),
28 }
29 }
30
31 pub fn kind(&self) -> ErrorKind {
32 self.kind
33 }
34}
35
36impl fmt::Display for Error {
37 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38 match self.inner {
39 ErrorInner::Io(ref err) => write!(f, "i/o error: {}", err),
40 ErrorInner::Other(ref msg) => {
41 write!(f, "{}: {}", ::std::error::Error::description(self), msg)
42 }
43 }
44 }
45}
46
47impl ::std::error::Error for Error {
48 fn description(&self) -> &str {
49 match self.kind {
50 ErrorKind::Io(_) => "i/o error",
51 ErrorKind::Serialize => "serialize error",
52 ErrorKind::Deserialize => "deserialize error",
53 }
54 }
55}
56
57impl ::serde::de::Error for Error {
58 fn custom<T: fmt::Display>(msg: T) -> Error {
59 Error {
60 kind: ErrorKind::Deserialize,
61 inner: ErrorInner::Other(msg.to_string()),
62 }
63 }
64}
65
66impl ::serde::ser::Error for Error {
67 fn custom<T: fmt::Display>(msg: T) -> Error {
68 Error {
69 kind: ErrorKind::Serialize,
70 inner: ErrorInner::Other(msg.to_string()),
71 }
72 }
73}
74
75impl From<io::Error> for Error {
76 fn from(err: io::Error) -> Error {
77 Error {
78 kind: ErrorKind::Io(err.kind()),
79 inner: ErrorInner::Io(err),
80 }
81 }
82}