2
2
3
3
use core:: fmt;
4
4
5
+ use crate :: write_err;
6
+
5
7
/// Formats error.
6
8
///
7
9
/// If `std` feature is OFF appends error source (delimited by `: `). We do this because
@@ -28,18 +30,19 @@ macro_rules! write_err {
28
30
#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
29
31
pub enum HexToBytesError {
30
32
/// Non-hexadecimal character.
31
- InvalidChar ( u8 ) ,
33
+ InvalidChar ( InvalidCharError ) ,
32
34
/// Purported hex string had odd length.
33
- OddLengthString ( usize ) ,
35
+ OddLengthString ( OddLengthStringError ) ,
34
36
}
35
37
36
38
impl fmt:: Display for HexToBytesError {
37
39
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
38
40
use HexToBytesError :: * ;
39
41
40
42
match * self {
41
- InvalidChar ( ch) => write ! ( f, "invalid hex character {}" , ch) ,
42
- OddLengthString ( ell) => write ! ( f, "odd hex string length {}" , ell) ,
43
+ InvalidChar ( ref e) => write_err ! ( f, "invalid char, failed to create bytes from hex" ; e) ,
44
+ OddLengthString ( ref e) =>
45
+ write_err ! ( f, "odd length, failed to create bytes from hex" ; e) ,
43
46
}
44
47
}
45
48
}
@@ -49,29 +52,75 @@ impl std::error::Error for HexToBytesError {
49
52
fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
50
53
use HexToBytesError :: * ;
51
54
52
- match self {
53
- InvalidChar ( _) | OddLengthString ( _) => None ,
55
+ match * self {
56
+ InvalidChar ( ref e) => Some ( e) ,
57
+ OddLengthString ( ref e) => Some ( e) ,
54
58
}
55
59
}
56
60
}
57
61
58
- /// Hex decoding error.
62
+ impl From < InvalidCharError > for HexToBytesError {
63
+ #[ inline]
64
+ fn from ( e : InvalidCharError ) -> Self { Self :: InvalidChar ( e) }
65
+ }
66
+
67
+ impl From < OddLengthStringError > for HexToBytesError {
68
+ #[ inline]
69
+ fn from ( e : OddLengthStringError ) -> Self { Self :: OddLengthString ( e) }
70
+ }
71
+
72
+ /// Invalid hex character.
59
73
#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
74
+ pub struct InvalidCharError {
75
+ pub ( crate ) invalid : u8 ,
76
+ }
77
+
78
+ impl fmt:: Display for InvalidCharError {
79
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
80
+ write ! ( f, "invalid hex char {}" , self . invalid)
81
+ }
82
+ }
83
+
84
+ #[ cfg( feature = "std" ) ]
85
+ impl std:: error:: Error for InvalidCharError {
86
+ fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > { None }
87
+ }
88
+
89
+ /// Purported hex string had odd length.
90
+ #[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
91
+ pub struct OddLengthStringError {
92
+ pub ( crate ) len : usize ,
93
+ }
94
+
95
+ impl fmt:: Display for OddLengthStringError {
96
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
97
+ write ! ( f, "odd hex string length {}" , self . len)
98
+ }
99
+ }
100
+
101
+ #[ cfg( feature = "std" ) ]
102
+ impl std:: error:: Error for OddLengthStringError {
103
+ fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > { None }
104
+ }
105
+
106
+ /// Hex decoding error.
107
+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
60
108
pub enum HexToArrayError {
61
109
/// Conversion error while parsing hex string.
62
110
Conversion ( HexToBytesError ) ,
63
111
/// Tried to parse fixed-length hash from a string with the wrong length (got, want).
64
- InvalidLength ( usize , usize ) ,
112
+ InvalidLength ( InvalidLengthError ) ,
65
113
}
66
114
67
115
impl fmt:: Display for HexToArrayError {
68
116
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
69
117
use HexToArrayError :: * ;
70
118
71
119
match * self {
72
- Conversion ( ref e) => crate :: write_err!( f, "conversion error" ; e) ,
73
- InvalidLength ( got, want) =>
74
- write ! ( f, "bad hex string length {} (expected {})" , got, want) ,
120
+ Conversion ( ref e) =>
121
+ crate :: write_err!( f, "conversion error, failed to create array from hex" ; e) ,
122
+ InvalidLength ( ref e) =>
123
+ write_err ! ( f, "invalid length, failed to create array from hex" ; e) ,
75
124
}
76
125
}
77
126
}
@@ -83,7 +132,7 @@ impl std::error::Error for HexToArrayError {
83
132
84
133
match * self {
85
134
Conversion ( ref e) => Some ( e) ,
86
- InvalidLength ( _ , _ ) => None ,
135
+ InvalidLength ( ref e ) => Some ( e ) ,
87
136
}
88
137
}
89
138
}
@@ -92,3 +141,26 @@ impl From<HexToBytesError> for HexToArrayError {
92
141
#[ inline]
93
142
fn from ( e : HexToBytesError ) -> Self { Self :: Conversion ( e) }
94
143
}
144
+
145
+ impl From < InvalidLengthError > for HexToArrayError {
146
+ #[ inline]
147
+ fn from ( e : InvalidLengthError ) -> Self { Self :: InvalidLength ( e) }
148
+ }
149
+
150
+ /// Tried to parse fixed-length hash from a string with the wrong length.
151
+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
152
+ pub struct InvalidLengthError {
153
+ pub ( crate ) expected : usize ,
154
+ pub ( crate ) got : usize ,
155
+ }
156
+
157
+ impl fmt:: Display for InvalidLengthError {
158
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
159
+ write ! ( f, "bad hex string length {} (expected {})" , self . got, self . expected)
160
+ }
161
+ }
162
+
163
+ #[ cfg( feature = "std" ) ]
164
+ impl std:: error:: Error for InvalidLengthError {
165
+ fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > { None }
166
+ }
0 commit comments