22
33#![ allow( unused_macros) ]
44
5- #[ cfg( feature = "alloc" ) ]
6- use crate :: prelude:: * ;
5+ use core:: fmt;
76
87#[ cfg( feature = "alloc" ) ]
98use core:: { num:: ParseIntError , str:: Utf8Error } ;
10- use failure:: Context ;
119
1210#[ cfg( feature = "std" ) ]
1311use std:: {
14- error:: Error as StdError ,
15- fmt:: { self , Display } ,
1612 io,
1713 string:: { FromUtf8Error , String , ToString } ,
1814} ;
@@ -21,7 +17,7 @@ use std::{
2117#[ derive( Debug ) ]
2218pub struct Error {
2319 /// Error context and kind
24- inner : Context < ErrorKind > ,
20+ kind : ErrorKind ,
2521
2622 /// Optional description
2723 #[ cfg( feature = "alloc" ) ]
@@ -45,72 +41,62 @@ impl Error {
4541
4642 /// Obtain the inner `ErrorKind` for this `Error`
4743 pub fn kind ( & self ) -> ErrorKind {
48- * self . inner . get_context ( )
44+ self . kind
4945 }
5046}
5147
52- #[ cfg( feature = "alloc" ) ]
53- impl Display for Error {
48+ impl fmt:: Display for Error {
5449 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
55- self . description ( ) . fmt ( f)
50+ self . kind . fmt ( f)
5651 }
5752}
5853
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 { }
6956
7057impl From < ErrorKind > for Error {
7158 fn from ( kind : ErrorKind ) -> Error {
7259 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,
8461 #[ cfg( feature = "alloc" ) ]
8562 description : None ,
8663 }
8764 }
8865}
8966
9067/// Kinds of errors
91- #[ derive( Copy , Clone , Debug , Fail , Eq , PartialEq ) ]
68+ #[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
9269pub enum ErrorKind {
9370 /// Invalid address
94- #[ fail( display = "address invalid" ) ]
9571 AddrInvalid ,
9672
9773 /// I/O operation failed
98- #[ fail( display = "I/O error" ) ]
9974 IoError ,
10075
10176 /// Parsing data failed
102- #[ fail( display = "parse error" ) ]
10377 ParseError ,
10478
10579 /// Request failed
106- #[ fail( display = "request error" ) ]
10780 RequestError ,
10881
10982 /// Error reading response
110- #[ fail( display = "error reading response" ) ]
11183 ResponseError ,
11284}
11385
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+
114100/// Create a new error (of a given enum variant) with a formatted message
115101macro_rules! err {
116102 ( $variant: ident, $msg: expr) => {
0 commit comments