1
1
use std:: hash:: Hash ;
2
2
3
- use crate :: error:: AppError ;
3
+ use bigdecimal:: num_traits:: ToBytes ;
4
+ use indexmap:: IndexMap ;
5
+
4
6
use crate :: types:: binary:: Binary ;
5
7
use crate :: types:: collection:: * ;
6
8
use crate :: types:: decimal:: * ;
7
9
use crate :: types:: floating_point:: * ;
8
- use bigdecimal:: BigDecimal ;
9
- use bigdecimal:: num_traits:: ToBytes ;
10
- use indexmap:: IndexMap ;
11
10
12
- pub trait Hashable : std :: hash :: Hash { }
11
+ pub trait Hashable : Hash { }
13
12
pub trait Encode {
14
13
fn encode ( & self ) -> Encoded ;
15
14
}
16
15
17
16
pub trait Decode < ' a > : From < & ' a [ u8 ] > + Encode { }
18
17
19
-
20
-
21
18
pub enum Encoded {
22
- Empty ( u8 ) , // Constructor
23
- Fixed ( u8 , Vec < u8 > ) , // Constructor, Data
24
- Variable ( u8 , Vec < u8 > ) , // Constructor, Data, size is computed from data
25
- Compound ( u8 , u32 , Vec < u8 > ) , // Constructor, count, data
26
- Array ( u8 , u32 , u8 , Vec < u8 > ) // Constructor, count, element constructor, data
19
+ Empty ( u8 ) , // Constructor
20
+ Fixed ( u8 , Vec < u8 > ) , // Constructor, Data
21
+ Variable ( u8 , Vec < u8 > ) , // Constructor, Data, size is computed from data
22
+ Compound ( u8 , u32 , Vec < u8 > ) , // Constructor, count, data
23
+ Array ( u8 , u32 , u8 , Vec < u8 > ) , // Constructor, count, element constructor, data
27
24
}
28
25
29
-
30
26
impl Encoded {
31
27
pub fn new_empty ( constructor : u8 ) -> Self {
32
28
Encoded :: Empty ( constructor)
33
29
}
34
30
35
- pub fn new_fixed ( constructor : u8 , data : Vec < u8 > ) -> Self {
31
+ pub fn new_fixed ( constructor : u8 , data : Vec < u8 > ) -> Self {
36
32
Encoded :: Fixed ( constructor, data)
37
- }
33
+ }
38
34
39
- pub fn new_variable ( constructor : u8 , data : Vec < u8 > ) -> Self {
35
+ pub fn new_variable ( constructor : u8 , data : Vec < u8 > ) -> Self {
40
36
Encoded :: Variable ( constructor, data)
41
37
}
42
38
43
- pub fn new_compound ( constructor : u8 , count : u32 , data : Vec < u8 > ) -> Self {
39
+ pub fn new_compound ( constructor : u8 , count : u32 , data : Vec < u8 > ) -> Self {
44
40
Encoded :: Compound ( constructor, count, data)
45
41
}
46
42
47
- pub fn new_array ( constructor : u8 , count : u32 , element_constructor : u8 , data : Vec < u8 > ) -> Self {
43
+ pub fn new_array ( constructor : u8 , count : u32 , element_constructor : u8 , data : Vec < u8 > ) -> Self {
48
44
Encoded :: Array ( constructor, count, element_constructor, data)
49
45
}
50
46
@@ -54,7 +50,7 @@ impl Encoded {
54
50
Self :: Fixed ( c, _) => c. to_owned ( ) ,
55
51
Self :: Variable ( c, _) => c. to_owned ( ) ,
56
52
Self :: Compound ( c, _, _) => c. to_owned ( ) ,
57
- Self :: Array ( c, _, _, _) => c. to_owned ( )
53
+ Self :: Array ( c, _, _, _) => c. to_owned ( ) ,
58
54
}
59
55
}
60
56
@@ -65,7 +61,6 @@ impl Encoded {
65
61
Self :: Variable ( _, data) => data. len ( ) ,
66
62
Self :: Compound ( _, _, data) => data. len ( ) ,
67
63
Self :: Array ( _, _, _, data) => data. len ( ) ,
68
-
69
64
}
70
65
}
71
66
}
@@ -75,29 +70,19 @@ impl From<Encoded> for Vec<u8> {
75
70
let mut res = Vec :: new ( ) ;
76
71
match value {
77
72
Encoded :: Empty ( c) => res. push ( c) ,
78
- Encoded :: Fixed ( c, data) => {
73
+ Encoded :: Fixed ( c, mut data) => {
79
74
res. push ( c) ;
80
- res. append ( data) ;
75
+ res. append ( & mut data) ;
81
76
}
82
- Encoded :: Variable ( c, data) => {
77
+ Encoded :: Variable ( c, mut data) => {
83
78
res. push ( c) ;
84
- res. append ( data) ;
79
+ res. append ( & mut data) ;
85
80
}
86
- Encoded :: Compound ( c, count, data) => {
87
-
88
- }
89
- }
90
- res
91
- }
92
- }
81
+ Encoded :: Compound ( c, count, data) => { }
93
82
94
-
95
- impl From < Encoded > for & mut Vec < u8 > {
96
- fn from ( value : Encoded ) -> Self {
97
- match value {
98
-
83
+ Encoded :: Array ( _, _, _, _) => { }
99
84
}
100
- todo ! ( )
85
+ res
101
86
}
102
87
}
103
88
@@ -207,7 +192,7 @@ impl Encode for bool {
207
192
fn encode ( & self ) -> Encoded {
208
193
match self {
209
194
true => Encoded :: new_fixed ( 0x56 , vec ! [ 0x01 ] ) ,
210
- false => Encoded :: new_fixed ( 0x56 , vec ! [ 0x00 ] )
195
+ false => Encoded :: new_fixed ( 0x56 , vec ! [ 0x00 ] ) ,
211
196
}
212
197
}
213
198
}
@@ -262,7 +247,7 @@ impl Encode for u64 {
262
247
match self {
263
248
0 => Encoded :: new_empty ( 0x44 ) ,
264
249
x if x > & & 0 && x <= & 255 => Encoded :: new_fixed ( 0x53 , x. to_be_bytes ( ) . to_vec ( ) ) ,
265
- _ => Encoded :: new_fixed ( 0x80 , self . to_be_bytes ( ) . to_vec ( ) )
250
+ _ => Encoded :: new_fixed ( 0x80 , self . to_be_bytes ( ) . to_vec ( ) ) ,
266
251
}
267
252
}
268
253
}
@@ -271,7 +256,7 @@ impl Encode for i32 {
271
256
fn encode ( & self ) -> Encoded {
272
257
match self {
273
258
x if x >= & -128 && x <= & 127 => Encoded :: new_fixed ( 0x54 , x. to_be_bytes ( ) . to_vec ( ) ) ,
274
- _ => Encoded :: new_fixed ( 0x71 , self . to_be_bytes ( ) . to_vec ( ) )
259
+ _ => Encoded :: new_fixed ( 0x71 , self . to_be_bytes ( ) . to_vec ( ) ) ,
275
260
}
276
261
}
277
262
}
@@ -280,7 +265,7 @@ impl Encode for i64 {
280
265
fn encode ( & self ) -> Encoded {
281
266
match self {
282
267
x if x >= & -128 && x <= & 127 => Encoded :: new_fixed ( 0x55 , x. to_be_bytes ( ) . to_vec ( ) ) ,
283
- _ => Encoded :: new_fixed ( 0x81 , self . to_be_bytes ( ) . to_vec ( ) )
268
+ _ => Encoded :: new_fixed ( 0x81 , self . to_be_bytes ( ) . to_vec ( ) ) ,
284
269
}
285
270
}
286
271
}
@@ -291,7 +276,7 @@ impl Encode for String {
291
276
x if x >= 0 as usize && x <= 255 as usize => {
292
277
Encoded :: new_variable ( 0xa1 , self . as_bytes ( ) . to_vec ( ) )
293
278
}
294
- _ => Encoded :: new_variable ( 0xb1 , self . as_bytes ( ) . to_vec ( ) )
279
+ _ => Encoded :: new_variable ( 0xb1 , self . as_bytes ( ) . to_vec ( ) ) ,
295
280
}
296
281
}
297
282
}
@@ -371,14 +356,12 @@ impl From<Float> for AmqpType {
371
356
}
372
357
}
373
358
374
-
375
359
impl From < Double > for AmqpType {
376
360
fn from ( value : Double ) -> Self {
377
361
AmqpType :: Double ( value. into ( ) )
378
362
}
379
363
}
380
364
381
-
382
365
impl From < char > for AmqpType {
383
366
fn from ( value : char ) -> Self {
384
367
AmqpType :: Char ( value)
@@ -435,7 +418,6 @@ impl From<Array> for AmqpType {
435
418
436
419
#[ cfg( test) ]
437
420
mod tests {
438
-
439
421
use super :: * ;
440
422
441
423
#[ test]
@@ -695,6 +677,5 @@ mod tests {
695
677
}
696
678
let val = AmqpType :: Array ( arr. into ( ) ) ;
697
679
assert_eq ! ( val. encode( ) . constructor( ) , 0xf0 ) ;
698
-
699
680
}
700
681
}
0 commit comments