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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::fmt;
use std::fmt::Display;
use failure::{Backtrace, Context, Fail};
#[derive(Debug, Fail, PartialEq)]
pub enum ErrorKind {
#[fail(display = "I/O Error")]
Io,
#[fail(display = "Parse Value Error")]
ParseError,
#[fail(display = "Invalid Format: {:?}", error)]
InvalidFormat{
error: std::string::String
},
}
impl std::cmp::Eq for ErrorKind {}
#[derive(Debug)]
pub struct Error {
inner: Context<ErrorKind>,
}
pub type Result<T> = std::result::Result<T, Error>;
impl std::convert::From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Error {
Error {
inner: error.context(ErrorKind::Io),
}
}
}
impl std::convert::From<std::num::ParseFloatError> for Error {
fn from(error: std::num::ParseFloatError) -> Error {
Error {
inner: error.context(ErrorKind::ParseError),
}
}
}
impl std::convert::From<std::num::ParseIntError> for Error {
fn from(error: std::num::ParseIntError) -> Error {
Error {
inner: error.context(ErrorKind::ParseError),
}
}
}
impl std::convert::From<std::string::ParseError> for Error {
fn from(error: std::string::ParseError) -> Error {
Error {
inner: error.context(ErrorKind::ParseError),
}
}
}
impl Fail for Error {
fn cause(&self) -> Option<&Fail> {
self.inner.cause()
}
fn backtrace(&self) -> Option<&Backtrace> {
self.inner.backtrace()
}
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.inner, f)
}
}
impl Error {
pub fn new(inner: Context<ErrorKind>) -> Error {
Error{inner}
}
pub fn kind(&self) -> &ErrorKind {
self.inner.get_context()
}
pub fn invalid_format(s: std::string::String) -> Error {
Error{inner: failure::Context::new(ErrorKind::InvalidFormat{error: s})}
}
}
impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Error {
Error {
inner: Context::new(kind),
}
}
}
impl From<Context<ErrorKind>> for Error {
fn from(inner: Context<ErrorKind>) -> Error {
Error {inner}
}
}
#[cfg(test)]
mod tests {
#[test]
fn from_std_io_error() {
let e = std::io::Error::new(std::io::ErrorKind::Other, "test");
let err: super::Error = std::convert::From::from(e);
assert_eq!(*err.kind(), super::ErrorKind::Io);
}
#[test]
fn from_std_num_parseinterror() {
let e = "foo".parse::<i64>().unwrap_err();
let err: super::Error = std::convert::From::from(e);
assert_eq!(*err.kind(), super::ErrorKind::ParseError);
}
#[test]
fn from_std_num_parsefloaterror() {
let e = "foo".parse::<f64>().unwrap_err();
let err: super::Error = std::convert::From::from(e);
assert_eq!(*err.kind(), super::ErrorKind::ParseError);
}
#[test]
fn from_invalid_format() {
let err = super::Error::invalid_format("test".to_string());
assert_eq!(*err.kind(), super::ErrorKind::InvalidFormat{error: "test".to_string()});
}
}