1
+ use crate :: error:: AppError ;
1
2
use crate :: serde:: decode:: Decode ;
2
3
use crate :: serde:: encode:: { Encode , Encoded } ;
3
- use crate :: error:: AppError ;
4
4
5
5
#[ cfg( not( feature = "zero-length-bools" ) ) ]
6
6
impl Encode for bool {
@@ -12,10 +12,8 @@ impl Encode for bool {
12
12
}
13
13
}
14
14
15
-
16
15
#[ cfg( feature = "zero-length-bools" ) ]
17
16
impl Encode for bool {
18
-
19
17
fn encode ( & self ) -> Encoded {
20
18
match self {
21
19
true => 0x41 . into ( ) ,
@@ -24,60 +22,73 @@ impl Encode for bool {
24
22
}
25
23
}
26
24
27
-
28
-
29
25
#[ cfg( not( feature = "zero-length-bools" ) ) ]
30
26
impl Decode for bool {
31
-
32
27
fn can_decode ( data : impl Iterator < Item = u8 > ) -> bool {
33
28
let mut iter = data. into_iter ( ) . peekable ( ) ;
34
29
match iter. peek ( ) {
35
30
Some ( 0x56 ) => true ,
36
- _ => false
31
+ _ => false ,
37
32
}
38
33
}
39
34
40
- fn try_decode ( mut iter : impl Iterator < Item = u8 > ) -> Result < Self , AppError > where Self : Sized {
35
+ fn try_decode ( mut iter : impl Iterator < Item = u8 > ) -> Result < Self , AppError >
36
+ where
37
+ Self : Sized ,
38
+ {
41
39
let con = iter. next ( ) ;
42
40
let val = iter. next ( ) ;
43
41
match ( con, val) {
44
42
( Some ( c) , Some ( v) ) if c == 0x56 && v == 0x00 => Ok ( false ) ,
45
43
( Some ( c) , Some ( v) ) if c == 0x56 && v == 0x01 => Ok ( true ) ,
46
- ( Some ( c) , _) => Err ( AppError :: DeserializationError ( "bool" . to_string ( ) , format ! ( "bool cannot be constructed from value {:#04x}" , c) ) ) ,
47
- ( Some ( c) , None ) => Err ( AppError :: DeserializationError ( "bool" . to_string ( ) , "Iterator was empty" . to_string ( ) ) ) ,
48
- ( None , _) => Err ( AppError :: DeserializationError ( "bool" . to_string ( ) , "Iterator was empty" . to_string ( ) ) ) ,
44
+ ( Some ( c) , _) => Err ( AppError :: DeserializationError (
45
+ "bool" . to_string ( ) ,
46
+ format ! ( "bool cannot be constructed from value {:#04x}" , c) ,
47
+ ) ) ,
48
+ ( Some ( c) , None ) => Err ( AppError :: DeserializationError (
49
+ "bool" . to_string ( ) ,
50
+ "Iterator was empty" . to_string ( ) ,
51
+ ) ) ,
52
+ ( None , _) => Err ( AppError :: DeserializationError (
53
+ "bool" . to_string ( ) ,
54
+ "Iterator was empty" . to_string ( ) ,
55
+ ) ) ,
49
56
}
50
-
51
57
}
52
58
}
53
59
54
-
55
-
56
60
#[ cfg( feature = "zero-length-bools" ) ]
57
61
impl Decode for bool {
58
-
59
62
fn can_decode ( data : Iterator < Item = u8 > ) -> bool {
60
63
let mut iter = data. into_iter ( ) . peekable ( ) ;
61
64
match iter. peek ( ) {
62
65
Some ( 0x41 ) => true ,
63
66
Some ( 0x42 ) => true ,
64
- _ => false
67
+ _ => false ,
65
68
}
66
69
}
67
70
68
- fn try_decode ( data : Iterator < Item = u8 > ) -> Result < Self , AppError > where Self : Sized {
71
+ fn try_decode ( data : Iterator < Item = u8 > ) -> Result < Self , AppError >
72
+ where
73
+ Self : Sized ,
74
+ {
69
75
if let Some ( val) = iter. next ( ) {
70
76
return match val {
71
77
0x41 => Ok ( true ) ,
72
78
0x42 => Ok ( false ) ,
73
- _ => Err ( AppError :: DeserializationError ( "bool" . to_string ( ) , format ! ( "bool cannot be constructed from value {:#04x}" , val) ) )
74
- }
79
+ _ => Err ( AppError :: DeserializationError (
80
+ "bool" . to_string ( ) ,
81
+ format ! ( "bool cannot be constructed from value {:#04x}" , val) ,
82
+ ) ) ,
83
+ } ;
75
84
}
76
- Err ( AppError :: DeserializationError ( "bool" . to_string ( ) , "Iterator was empty" . to_string ( ) ) )
85
+ Err ( AppError :: DeserializationError (
86
+ "bool" . to_string ( ) ,
87
+ "Iterator was empty" . to_string ( ) ,
88
+ ) )
77
89
}
78
90
}
79
91
80
-
81
92
#[ cfg( test) ]
82
93
mod test {
83
94
use super :: * ;
@@ -113,7 +124,6 @@ mod test {
113
124
assert_eq ! ( bool :: can_decode( val_false. into_iter( ) ) , false ) ;
114
125
}
115
126
116
-
117
127
#[ test]
118
128
#[ cfg( not( feature = "zero-length-bools" ) ) ]
119
129
fn decode_returns_error_when_value_bytes_are_invalid ( ) {
@@ -123,7 +133,6 @@ mod test {
123
133
assert ! ( bool :: try_decode( val_false. into_iter( ) ) . is_err( ) ) ;
124
134
}
125
135
126
-
127
136
#[ test]
128
137
#[ cfg( not( feature = "zero-length-bools" ) ) ]
129
138
fn try_decode_returns_correct_value_if_bytes_are_valid ( ) {
@@ -132,7 +141,6 @@ mod test {
132
141
assert_eq ! ( bool :: try_decode( val_true. into_iter( ) ) . unwrap( ) , true ) ;
133
142
assert_eq ! ( bool :: try_decode( val_false. into_iter( ) ) . unwrap( ) , false ) ;
134
143
}
135
-
136
144
137
145
#[ test]
138
146
#[ cfg( feature = "zero-length-bools" ) ]
0 commit comments