File tree Expand file tree Collapse file tree 3 files changed +42
-9
lines changed Expand file tree Collapse file tree 3 files changed +42
-9
lines changed Original file line number Diff line number Diff line change @@ -45,10 +45,6 @@ impl Decode for bool {
45
45
"bool" . to_string ( ) ,
46
46
format ! ( "bool cannot be constructed from value {:#04x}" , c) ,
47
47
) ) ,
48
- ( Some ( c) , None ) => Err ( AppError :: DeserializationError (
49
- "bool" . to_string ( ) ,
50
- "Iterator was empty" . to_string ( ) ,
51
- ) ) ,
52
48
( None , _) => Err ( AppError :: DeserializationError (
53
49
"bool" . to_string ( ) ,
54
50
"Iterator was empty" . to_string ( ) ,
Original file line number Diff line number Diff line change 1
1
use crate :: serde:: encode:: { Encode , Encoded } ;
2
+ use crate :: serde:: decode:: Decode ;
3
+
2
4
3
5
impl Encode for u8 {
4
6
fn encode ( & self ) -> Encoded {
5
7
Encoded :: new_fixed ( 0x50 , self . to_be_bytes ( ) . to_vec ( ) )
6
8
}
7
9
}
8
10
11
+ impl Decode for u8 {
12
+ fn can_decode ( data : impl Iterator < Item = u8 > ) -> bool {
13
+ let mut iter = data. into_iter ( ) . peekable ( ) ;
14
+ match iter. peek ( ) {
15
+ Some ( 0x50 ) => true ,
16
+ _ => false
17
+ }
18
+ }
19
+
20
+ fn try_decode ( data : impl Iterator < Item = u8 > ) -> Result < Self , crate :: error:: AppError >
21
+ where
22
+ Self : Sized {
23
+ todo ! ( )
24
+ }
25
+
26
+ }
27
+
9
28
#[ cfg( test) ]
10
29
mod test {
11
30
31
+
12
32
use super :: * ;
13
33
14
34
#[ test]
15
35
fn construct_ubyte ( ) {
16
36
let val: u8 = 8 ;
17
37
assert_eq ! ( val. encode( ) . constructor( ) , 0x50 ) ;
18
38
}
39
+
40
+ #[ test]
41
+ fn can_deocde_returns_true_if_constructor_is_valid ( ) {
42
+ let val = vec ! [ 0x50 , 0x41 ] ;
43
+ assert_eq ! ( u8 :: can_decode( val. into_iter( ) ) , true ) ;
44
+ }
45
+
46
+ #[ test]
47
+ fn can_decode_return_false_if_constructor_is_invalid ( ) {
48
+ let val = vec ! [ 0x51 ] ;
49
+ assert_eq ! ( u8 :: can_decode( val. into_iter( ) ) , false ) ;
50
+ }
51
+
52
+ #[ test]
53
+ fn try_decode_returns_correct_value ( ) {
54
+ let val = vec ! [ 0x50 , 0x10 ] ;
55
+ assert_eq ! ( u8 :: try_decode( val. into_iter( ) ) . unwrap( ) , 16 )
56
+ }
57
+
19
58
}
Original file line number Diff line number Diff line change 1
- use crate :: {
2
- error:: AppError ,
3
- serde:: encode:: { Encode , Encoded } ,
4
- } ;
1
+ use crate :: error:: AppError ;
2
+
5
3
6
4
#[ derive( Hash , Eq , PartialEq ) ]
7
5
pub struct Constructor ( u8 ) ;
8
6
9
7
pub trait Decode {
8
+ fn can_decode ( data : impl Iterator < Item = u8 > ) -> bool ;
10
9
fn try_decode ( data : impl Iterator < Item = u8 > ) -> Result < Self , AppError >
11
10
where
12
11
Self : Sized ;
13
- fn can_decode ( data : impl Iterator < Item = u8 > ) -> bool ;
14
12
}
15
13
16
14
impl From < u8 > for Constructor {
You can’t perform that action at this time.
0 commit comments