1
1
use core:: fmt;
2
+ use unsigned_varint:: decode;
3
+
4
+ #[ cfg( not( feature = "std" ) ) ]
5
+ use core2:: io;
6
+ #[ cfg( feature = "std" ) ]
7
+ use std:: io;
2
8
3
9
/// Error type for parsing a PeerID from a string.
4
- #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
5
- pub enum ParsePeerIDError {
10
+ #[ derive( Debug ) ]
11
+ pub enum Error {
6
12
/// The input string had an incorrect length.
7
13
InvalidLength { expected : usize , actual : usize } ,
8
14
/// The input string contained non-hexadecimal characters or had an odd number of digits.
9
15
InvalidHexEncoding { c : char , index : usize } ,
16
+ /// Io error
17
+ Io ( io:: Error ) ,
18
+ /// Varint error
19
+ Varint ( decode:: Error ) ,
10
20
}
11
21
12
- impl fmt:: Display for ParsePeerIDError {
22
+ impl PartialEq for Error {
23
+ fn eq ( & self , other : & Self ) -> bool {
24
+ match ( self , other) {
25
+ (
26
+ Error :: InvalidLength { expected, actual } ,
27
+ Error :: InvalidLength {
28
+ expected : e,
29
+ actual : a,
30
+ } ,
31
+ ) => expected == e && actual == a,
32
+ (
33
+ Error :: InvalidHexEncoding { c, index } ,
34
+ Error :: InvalidHexEncoding { c : cc, index : i } ,
35
+ ) => c == cc && index == i,
36
+ ( Error :: Io ( err) , Error :: Io ( other_err) ) => err. kind ( ) == other_err. kind ( ) ,
37
+ ( Error :: Varint ( err) , Error :: Varint ( other_err) ) => err == other_err,
38
+ _ => false ,
39
+ }
40
+ }
41
+
42
+ fn ne ( & self , other : & Self ) -> bool {
43
+ !self . eq ( other)
44
+ }
45
+ }
46
+
47
+ impl fmt:: Display for Error {
13
48
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
14
49
match self {
15
- ParsePeerIDError :: InvalidLength { expected, actual } => {
50
+ Error :: InvalidLength { expected, actual } => {
16
51
write ! ( f, "Expected length: {}, but got: {}" , expected, actual)
17
52
}
18
- ParsePeerIDError :: InvalidHexEncoding { c, index } => {
53
+ Error :: InvalidHexEncoding { c, index } => {
19
54
write ! ( f, "Invalid hex character '{}' at index {}" , c, index)
20
55
}
56
+ Error :: Io ( err) => {
57
+ write ! ( f, "IO error: {}" , err)
58
+ }
59
+ Error :: Varint ( err) => {
60
+ write ! ( f, "Varint error: {}" , err)
61
+ }
62
+ }
63
+ }
64
+ }
65
+
66
+ impl From < io:: Error > for Error {
67
+ fn from ( err : io:: Error ) -> Self {
68
+ Error :: Io ( err)
69
+ }
70
+ }
71
+
72
+ impl From < decode:: Error > for Error {
73
+ fn from ( err : decode:: Error ) -> Self {
74
+ Error :: Varint ( err)
75
+ }
76
+ }
77
+
78
+ #[ cfg( feature = "std" ) ]
79
+ impl From < unsigned_varint:: io:: ReadError > for Error {
80
+ fn from ( err : unsigned_varint:: io:: ReadError ) -> Self {
81
+ match err {
82
+ unsigned_varint:: io:: ReadError :: Io ( e) => Error :: Io ( e) ,
83
+ unsigned_varint:: io:: ReadError :: Decode ( e) => Error :: Varint ( e) ,
84
+ _ => {
85
+ // This case should not happen, but if it does, we can convert it to a Varint error.
86
+ Error :: Varint ( decode:: Error :: Insufficient )
87
+ }
21
88
}
22
89
}
23
90
}
24
91
25
92
#[ cfg( feature = "std" ) ]
26
- impl std:: error:: Error for ParsePeerIDError { }
93
+ impl std:: error:: Error for Error { }
27
94
28
95
#[ cfg( test) ]
29
96
#[ cfg_attr( coverage_nightly, coverage( off) ) ]
@@ -32,13 +99,86 @@ mod tests {
32
99
33
100
#[ test]
34
101
fn test_parse_id_error_display ( ) {
35
- let error = ParsePeerIDError :: InvalidLength {
102
+ let error = Error :: InvalidLength {
36
103
expected : 16 ,
37
104
actual : 20 ,
38
105
} ;
39
106
assert_eq ! ( format!( "{}" , error) , "Expected length: 16, but got: 20" ) ;
40
107
41
- let error = ParsePeerIDError :: InvalidHexEncoding { c : 'g' , index : 5 } ;
108
+ let error = Error :: InvalidHexEncoding { c : 'g' , index : 5 } ;
42
109
assert_eq ! ( format!( "{}" , error) , "Invalid hex character 'g' at index 5" ) ;
110
+
111
+ let io_error = io:: Error :: new ( io:: ErrorKind :: Other , "IO error" ) ;
112
+ let error = Error :: Io ( io_error) ;
113
+ assert_eq ! ( format!( "{}" , error) , "IO error: IO error" ) ;
114
+
115
+ let varint_error = decode:: Error :: Insufficient ;
116
+ let error = Error :: Varint ( varint_error) ;
117
+ assert_eq ! ( format!( "{}" , error) , "Varint error: not enough input bytes" ) ;
118
+ }
119
+
120
+ #[ test]
121
+ fn test_partial_eq ( ) {
122
+ let error1 = Error :: InvalidLength {
123
+ expected : 16 ,
124
+ actual : 20 ,
125
+ } ;
126
+ let error2 = Error :: InvalidLength {
127
+ expected : 16 ,
128
+ actual : 20 ,
129
+ } ;
130
+ assert_eq ! ( error1, error2) ;
131
+
132
+ let error3 = Error :: InvalidHexEncoding { c : 'g' , index : 5 } ;
133
+ assert_ne ! ( error1, error3) ;
134
+
135
+ let io_error = io:: Error :: new ( io:: ErrorKind :: Other , "IO error" ) ;
136
+ let error4 = Error :: Io ( io_error) ;
137
+ assert_ne ! ( error1, error4) ;
138
+
139
+ let varint_error = decode:: Error :: Insufficient ;
140
+ let error5 = Error :: Varint ( varint_error) ;
141
+ assert_ne ! ( error1, error5) ;
142
+
143
+ let error6 = Error :: Varint ( decode:: Error :: Insufficient ) ;
144
+ assert_eq ! ( error5, error6) ;
145
+
146
+ let io_error = io:: Error :: new ( io:: ErrorKind :: Other , "IO error" ) ;
147
+ let error7 = Error :: Io ( io_error) ;
148
+ assert_eq ! ( error4, error7) ;
149
+
150
+ assert ! ( error4 != error5) ;
151
+ }
152
+
153
+ #[ test]
154
+ fn test_error_from_io ( ) {
155
+ let err = io:: Error :: new ( io:: ErrorKind :: Other , "IO error" ) ;
156
+ let error = Error :: from ( err) ;
157
+ assert_eq ! (
158
+ error,
159
+ Error :: Io ( io:: Error :: new( io:: ErrorKind :: Other , "IO error" ) )
160
+ ) ;
161
+ }
162
+
163
+ #[ test]
164
+ fn test_error_from_varint ( ) {
165
+ let varint_error = decode:: Error :: Insufficient ;
166
+ let error: Error = Error :: from ( varint_error) ;
167
+ assert_eq ! ( error, Error :: Varint ( decode:: Error :: Insufficient ) ) ;
168
+ }
169
+
170
+ #[ test]
171
+ fn test_error_from_unsigned_varint_io ( ) {
172
+ let read_error =
173
+ unsigned_varint:: io:: ReadError :: Io ( io:: Error :: new ( io:: ErrorKind :: Other , "IO error" ) ) ;
174
+ let error: Error = Error :: from ( read_error) ;
175
+ assert_eq ! (
176
+ error,
177
+ Error :: Io ( io:: Error :: new( io:: ErrorKind :: Other , "IO error" ) )
178
+ ) ;
179
+
180
+ let decode_error = unsigned_varint:: io:: ReadError :: Decode ( decode:: Error :: Insufficient ) ;
181
+ let error: Error = Error :: from ( decode_error) ;
182
+ assert_eq ! ( error, Error :: Varint ( decode:: Error :: Insufficient ) ) ;
43
183
}
44
184
}
0 commit comments