@@ -6,8 +6,12 @@ use core::{fmt, str};
6
6
7
7
#[ cfg( all( feature = "alloc" , not( feature = "std" ) ) ) ]
8
8
use crate :: alloc:: vec:: Vec ;
9
+ use crate :: error:: InvalidLengthError ;
9
10
use crate :: iter:: HexToBytesIter ;
10
11
12
+ #[ rustfmt:: skip] // Keep public re-exports separate.
13
+ pub use crate :: error:: { HexToBytesError , HexToArrayError } ;
14
+
11
15
/// Trait for objects that can be deserialized from hex strings.
12
16
pub trait FromHex : Sized {
13
17
/// Error type returned while parsing hex string.
@@ -37,37 +41,6 @@ impl FromHex for Vec<u8> {
37
41
}
38
42
}
39
43
40
- /// Hex decoding error.
41
- #[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
42
- pub enum HexToBytesError {
43
- /// Non-hexadecimal character.
44
- InvalidChar ( u8 ) ,
45
- /// Purported hex string had odd length.
46
- OddLengthString ( usize ) ,
47
- }
48
-
49
- impl fmt:: Display for HexToBytesError {
50
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
51
- use HexToBytesError :: * ;
52
-
53
- match * self {
54
- InvalidChar ( ch) => write ! ( f, "invalid hex character {}" , ch) ,
55
- OddLengthString ( ell) => write ! ( f, "odd hex string length {}" , ell) ,
56
- }
57
- }
58
- }
59
-
60
- #[ cfg( feature = "std" ) ]
61
- impl std:: error:: Error for HexToBytesError {
62
- fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
63
- use HexToBytesError :: * ;
64
-
65
- match self {
66
- InvalidChar ( _) | OddLengthString ( _) => None ,
67
- }
68
- }
69
- }
70
-
71
44
macro_rules! impl_fromhex_array {
72
45
( $len: expr) => {
73
46
impl FromHex for [ u8 ; $len] {
@@ -86,7 +59,7 @@ macro_rules! impl_fromhex_array {
86
59
}
87
60
Ok ( ret)
88
61
} else {
89
- Err ( HexToArrayError :: InvalidLength ( 2 * $len, 2 * iter. len( ) ) )
62
+ Err ( InvalidLengthError { expected : 2 * $len, got : 2 * iter. len( ) } . into ( ) )
90
63
}
91
64
}
92
65
}
@@ -113,67 +86,28 @@ impl_fromhex_array!(256);
113
86
impl_fromhex_array ! ( 384 ) ;
114
87
impl_fromhex_array ! ( 512 ) ;
115
88
116
- /// Hex decoding error.
117
- #[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
118
- pub enum HexToArrayError {
119
- /// Conversion error while parsing hex string.
120
- Conversion ( HexToBytesError ) ,
121
- /// Tried to parse fixed-length hash from a string with the wrong length (got, want).
122
- InvalidLength ( usize , usize ) ,
123
- }
124
-
125
- impl fmt:: Display for HexToArrayError {
126
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
127
- use HexToArrayError :: * ;
128
-
129
- match * self {
130
- Conversion ( ref e) => crate :: write_err!( f, "conversion error" ; e) ,
131
- InvalidLength ( got, want) =>
132
- write ! ( f, "bad hex string length {} (expected {})" , got, want) ,
133
- }
134
- }
135
- }
136
-
137
- #[ cfg( feature = "std" ) ]
138
- impl std:: error:: Error for HexToArrayError {
139
- fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
140
- use HexToArrayError :: * ;
141
-
142
- match * self {
143
- Conversion ( ref e) => Some ( e) ,
144
- InvalidLength ( _, _) => None ,
145
- }
146
- }
147
- }
148
-
149
- impl From < HexToBytesError > for HexToArrayError {
150
- #[ inline]
151
- fn from ( e : HexToBytesError ) -> Self { Self :: Conversion ( e) }
152
- }
153
-
154
89
#[ cfg( test) ]
155
90
mod tests {
156
91
use super :: * ;
157
92
use crate :: display:: DisplayHex ;
93
+ use crate :: error:: { InvalidCharError , InvalidLengthError , OddLengthStringError } ;
158
94
159
95
#[ test]
160
96
#[ cfg( feature = "alloc" ) ]
161
97
fn hex_error ( ) {
162
- use HexToBytesError :: * ;
163
-
164
98
let oddlen = "0123456789abcdef0" ;
165
99
let badchar1 = "Z123456789abcdef" ;
166
100
let badchar2 = "012Y456789abcdeb" ;
167
101
let badchar3 = "«23456789abcdef" ;
168
102
169
- assert_eq ! ( Vec :: <u8 >:: from_hex( oddlen) , Err ( OddLengthString ( 17 ) ) ) ;
103
+ assert_eq ! ( Vec :: <u8 >:: from_hex( oddlen) , Err ( OddLengthStringError { len : 17 } . into ( ) ) ) ;
170
104
assert_eq ! (
171
105
<[ u8 ; 4 ] >:: from_hex( oddlen) ,
172
- Err ( HexToArrayError :: Conversion ( OddLengthString ( 17 ) ) )
106
+ Err ( HexToBytesError :: OddLengthString ( OddLengthStringError { len : 17 } ) . into ( ) )
173
107
) ;
174
- assert_eq ! ( Vec :: <u8 >:: from_hex( badchar1) , Err ( InvalidChar ( b'Z' ) ) ) ;
175
- assert_eq ! ( Vec :: <u8 >:: from_hex( badchar2) , Err ( InvalidChar ( b'Y' ) ) ) ;
176
- assert_eq ! ( Vec :: <u8 >:: from_hex( badchar3) , Err ( InvalidChar ( 194 ) ) ) ;
108
+ assert_eq ! ( Vec :: <u8 >:: from_hex( badchar1) , Err ( InvalidCharError { invalid : b'Z' } . into ( ) ) ) ;
109
+ assert_eq ! ( Vec :: <u8 >:: from_hex( badchar2) , Err ( InvalidCharError { invalid : b'Y' } . into ( ) ) ) ;
110
+ assert_eq ! ( Vec :: <u8 >:: from_hex( badchar3) , Err ( InvalidCharError { invalid : 194 } . into ( ) ) ) ;
177
111
}
178
112
179
113
#[ test]
@@ -183,9 +117,11 @@ mod tests {
183
117
}
184
118
#[ test]
185
119
fn hex_to_array_error ( ) {
186
- use HexToArrayError :: * ;
187
120
let len_sixteen = "0123456789abcdef" ;
188
- assert_eq ! ( <[ u8 ; 4 ] >:: from_hex( len_sixteen) , Err ( InvalidLength ( 8 , 16 ) ) ) ;
121
+ assert_eq ! (
122
+ <[ u8 ; 4 ] >:: from_hex( len_sixteen) ,
123
+ Err ( InvalidLengthError { expected: 8 , got: 16 } . into( ) )
124
+ )
189
125
}
190
126
191
127
#[ test]
0 commit comments