2
2
3
3
#![ allow( unused_macros) ]
4
4
5
- #[ cfg( feature = "alloc" ) ]
6
- use crate :: prelude:: * ;
5
+ use core:: fmt;
7
6
8
7
#[ cfg( feature = "alloc" ) ]
9
8
use core:: { num:: ParseIntError , str:: Utf8Error } ;
10
- use failure:: Context ;
11
9
12
10
#[ cfg( feature = "std" ) ]
13
11
use std:: {
14
- error:: Error as StdError ,
15
- fmt:: { self , Display } ,
16
12
io,
17
13
string:: { FromUtf8Error , String , ToString } ,
18
14
} ;
@@ -21,7 +17,7 @@ use std::{
21
17
#[ derive( Debug ) ]
22
18
pub struct Error {
23
19
/// Error context and kind
24
- inner : Context < ErrorKind > ,
20
+ kind : ErrorKind ,
25
21
26
22
/// Optional description
27
23
#[ cfg( feature = "alloc" ) ]
@@ -45,72 +41,62 @@ impl Error {
45
41
46
42
/// Obtain the inner `ErrorKind` for this `Error`
47
43
pub fn kind ( & self ) -> ErrorKind {
48
- * self . inner . get_context ( )
44
+ self . kind
49
45
}
50
46
}
51
47
52
- #[ cfg( feature = "alloc" ) ]
53
- impl Display for Error {
48
+ impl fmt:: Display for Error {
54
49
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
55
- self . description ( ) . fmt ( f)
50
+ self . kind . fmt ( f)
56
51
}
57
52
}
58
53
59
- #[ cfg( feature = "alloc" ) ]
60
- impl StdError for Error {
61
- fn description ( & self ) -> & str {
62
- if let Some ( ref desc) = self . description {
63
- desc
64
- } else {
65
- "(none)"
66
- }
67
- }
68
- }
54
+ #[ cfg( feature = "std" ) ]
55
+ impl std:: error:: Error for Error { }
69
56
70
57
impl From < ErrorKind > for Error {
71
58
fn from ( kind : ErrorKind ) -> Error {
72
59
Error {
73
- inner : Context :: new ( kind) ,
74
- #[ cfg( feature = "alloc" ) ]
75
- description : None ,
76
- }
77
- }
78
- }
79
-
80
- impl From < Context < ErrorKind > > for Error {
81
- fn from ( inner : Context < ErrorKind > ) -> Self {
82
- Self {
83
- inner,
60
+ kind,
84
61
#[ cfg( feature = "alloc" ) ]
85
62
description : None ,
86
63
}
87
64
}
88
65
}
89
66
90
67
/// Kinds of errors
91
- #[ derive( Copy , Clone , Debug , Fail , Eq , PartialEq ) ]
68
+ #[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
92
69
pub enum ErrorKind {
93
70
/// Invalid address
94
- #[ fail( display = "address invalid" ) ]
95
71
AddrInvalid ,
96
72
97
73
/// I/O operation failed
98
- #[ fail( display = "I/O error" ) ]
99
74
IoError ,
100
75
101
76
/// Parsing data failed
102
- #[ fail( display = "parse error" ) ]
103
77
ParseError ,
104
78
105
79
/// Request failed
106
- #[ fail( display = "request error" ) ]
107
80
RequestError ,
108
81
109
82
/// Error reading response
110
- #[ fail( display = "error reading response" ) ]
111
83
ResponseError ,
112
84
}
113
85
86
+ impl fmt:: Display for ErrorKind {
87
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
88
+ let description = match self {
89
+ ErrorKind :: AddrInvalid => "address invalid" ,
90
+ ErrorKind :: IoError => "I/O error" ,
91
+ ErrorKind :: ParseError => "parse error" ,
92
+ ErrorKind :: RequestError => "request error" ,
93
+ ErrorKind :: ResponseError => "error reading response" ,
94
+ } ;
95
+
96
+ write ! ( f, "{}" , description)
97
+ }
98
+ }
99
+
114
100
/// Create a new error (of a given enum variant) with a formatted message
115
101
macro_rules! err {
116
102
( $variant: ident, $msg: expr) => {
0 commit comments