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
@@ -25,21 +27,22 @@ macro_rules! write_err {
25
27
}
26
28
27
29
/// Hex decoding error.
28
- #[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
30
+ #[ derive( Debug , 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,77 @@ 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
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.
73
+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
74
+ #[ non_exhaustive]
75
+ pub struct InvalidCharError {
76
+ pub ( crate ) invalid : u8 ,
77
+ }
78
+
79
+ impl fmt:: Display for InvalidCharError {
80
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
81
+ write ! ( f, "invalid hex char {}" , self . invalid)
82
+ }
83
+ }
84
+
85
+ #[ cfg( feature = "std" ) ]
86
+ impl std:: error:: Error for InvalidCharError {
87
+ fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > { None }
88
+ }
89
+
90
+ /// Purported hex string had odd length.
91
+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
92
+ #[ non_exhaustive]
93
+ pub struct OddLengthStringError {
94
+ pub ( crate ) len : usize ,
95
+ }
96
+
97
+ impl fmt:: Display for OddLengthStringError {
98
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
99
+ write ! ( f, "odd hex string length {}" , self . len)
100
+ }
101
+ }
102
+
103
+ #[ cfg( feature = "std" ) ]
104
+ impl std:: error:: Error for OddLengthStringError {
105
+ fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > { None }
106
+ }
107
+
58
108
/// Hex decoding error.
59
- #[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
109
+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
60
110
pub enum HexToArrayError {
61
111
/// Conversion error while parsing hex string.
62
112
Conversion ( HexToBytesError ) ,
63
113
/// Tried to parse fixed-length hash from a string with the wrong length (got, want).
64
- InvalidLength ( usize , usize ) ,
114
+ InvalidLength ( InvalidLengthError ) ,
65
115
}
66
116
67
117
impl fmt:: Display for HexToArrayError {
68
118
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
69
119
use HexToArrayError :: * ;
70
120
71
121
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) ,
122
+ Conversion ( ref e) =>
123
+ crate :: write_err!( f, "conversion error, failed to create array from hex" ; e) ,
124
+ InvalidLength ( ref e) =>
125
+ write_err ! ( f, "invalid length, failed to create array from hex" ; e) ,
75
126
}
76
127
}
77
128
}
@@ -83,7 +134,7 @@ impl std::error::Error for HexToArrayError {
83
134
84
135
match * self {
85
136
Conversion ( ref e) => Some ( e) ,
86
- InvalidLength ( _ , _ ) => None ,
137
+ InvalidLength ( ref e ) => Some ( e ) ,
87
138
}
88
139
}
89
140
}
@@ -92,3 +143,27 @@ impl From<HexToBytesError> for HexToArrayError {
92
143
#[ inline]
93
144
fn from ( e : HexToBytesError ) -> Self { Self :: Conversion ( e) }
94
145
}
146
+
147
+ impl From < InvalidLengthError > for HexToArrayError {
148
+ #[ inline]
149
+ fn from ( e : InvalidLengthError ) -> Self { Self :: InvalidLength ( e) }
150
+ }
151
+
152
+ /// Tried to parse fixed-length hash from a string with the wrong length.
153
+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
154
+ #[ non_exhaustive]
155
+ pub struct InvalidLengthError {
156
+ pub ( crate ) expected : usize ,
157
+ pub ( crate ) got : usize ,
158
+ }
159
+
160
+ impl fmt:: Display for InvalidLengthError {
161
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
162
+ write ! ( f, "bad hex string length {} (expected {})" , self . got, self . expected)
163
+ }
164
+ }
165
+
166
+ #[ cfg( feature = "std" ) ]
167
+ impl std:: error:: Error for InvalidLengthError {
168
+ fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > { None }
169
+ }
0 commit comments