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
83
84
use std::fmt;
use std::io;
use std::error;
use std::string::FromUtf8Error;
use rustc_serialize;
#[derive(Debug)]
pub enum Error {
Utf8(FromUtf8Error),
JsonDecode(rustc_serialize::json::DecoderError),
JsonParse(rustc_serialize::json::ParserError),
JsonEncode(rustc_serialize::json::EncoderError),
Internal(String),
}
impl From<rustc_serialize::json::DecoderError> for Error {
fn from(err: rustc_serialize::json::DecoderError) -> Error {
Error::JsonDecode(err)
}
}
impl From<rustc_serialize::json::ParserError> for Error {
fn from(err: rustc_serialize::json::ParserError) -> Error {
Error::JsonParse(err)
}
}
impl From<rustc_serialize::json::EncoderError> for Error {
fn from(err: rustc_serialize::json::EncoderError) -> Error {
Error::JsonEncode(err)
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
Error::Internal(format!("{:?}", err))
}
}
impl From<FromUtf8Error> for Error {
fn from(err: FromUtf8Error) -> Error {
Error::Utf8(err)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Utf8(ref e) => write!(f, "Utf8 decode Error: {:?}", e),
Error::JsonDecode(ref e) => write!(f, "Json Decode Error: {:?}", e),
Error::JsonParse(ref e) => write!(f, "Json Parse Error: {:?}", e),
Error::JsonEncode(ref e) => write!(f, "Json Encode Error: {:?}", e),
Error::Internal(ref st) => write!(f, "Internal Error: {:?}", st),
}
}
}
impl error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::Utf8(ref e) => e.description(),
Error::JsonDecode(ref e) => e.description(),
Error::JsonParse(ref e) => e.description(),
Error::JsonEncode(ref e) => e.description(),
Error::Internal(ref st) => st,
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
Error::Utf8(ref e) => Some(e),
Error::JsonDecode(ref e) => Some(e),
Error::JsonParse(ref e) => Some(e),
Error::JsonEncode(ref e) => Some(e),
Error::Internal(_) => None,
}
}
}