1
+ use std:: hash:: Hash ;
2
+
1
3
use bigdecimal:: BigDecimal ;
2
4
use indexmap:: IndexMap ;
3
-
5
+ use crate :: types:: binary:: Binary ;
6
+ use crate :: types:: collection:: * ;
7
+ use crate :: types:: decimal:: * ;
4
8
use crate :: error:: AppError ;
5
- trait Encode {
9
+ pub trait Encode {
6
10
fn constructor ( & self ) -> Constructor ;
7
11
fn encode ( & self ) -> Vec < u8 > ;
8
12
}
9
13
10
- trait Decode < ' a > : From < & ' a [ u8 ] > + Encode { }
11
- pub struct Timestamp ( u64 ) ;
12
- pub struct Binary ( Vec < u8 > ) ;
14
+ pub trait Decode < ' a > : From < & ' a [ u8 ] > + Encode { }
15
+
16
+ # [ derive ( Hash , Eq , PartialEq ) ]
13
17
pub struct Symbol ( String ) ;
18
+ #[ derive( Hash , Eq , PartialEq ) ]
14
19
pub struct Uuid ( uuid:: Uuid ) ;
20
+ #[ derive( Hash , Eq , PartialEq ) ]
15
21
pub struct Described ( ) ;
22
+ #[ derive( Hash , Eq , PartialEq ) ]
16
23
pub struct Constructor ( u8 ) ;
17
- pub struct Decimal32 ( BigDecimal ) ;
18
- pub struct Decimal64 ( BigDecimal ) ;
19
- pub struct Decimal128 ( BigDecimal ) ;
20
- pub struct List ( Vec < AmqpType > ) ;
21
- pub struct Array ( Vec < AmqpType > ) ;
22
- pub struct Map ( IndexMap < AmqpType , AmqpType > ) ;
23
-
24
+ #[ derive( Hash , Eq , PartialEq ) ]
25
+ pub struct Timestamp ( u64 ) ;
26
+ #[ derive( PartialEq , Eq ) ]
27
+ pub struct Float ( f32 ) ;
28
+ #[ derive( PartialEq , Eq ) ]
29
+ pub struct Double ( f64 ) ;
24
30
31
+ #[ derive( Hash , Eq , PartialEq ) ]
25
32
pub enum AmqpType {
26
33
Null ,
27
34
Boolean ( bool ) ,
@@ -33,8 +40,8 @@ pub enum AmqpType {
33
40
Short ( i16 ) ,
34
41
Int ( i32 ) ,
35
42
Long ( i64 ) ,
36
- Float ( f32 ) ,
37
- Double ( f64 ) ,
43
+ Float ( Float ) ,
44
+ Double ( Double ) ,
38
45
Decimal32 ( Decimal32 ) ,
39
46
Decimal64 ( Decimal64 ) ,
40
47
Decimal128 ( Decimal128 ) ,
@@ -109,6 +116,32 @@ impl Encode for AmqpType {
109
116
}
110
117
}
111
118
119
+ impl Hash for f32 {
120
+ fn hash < H : std:: hash:: Hasher > ( & self , state : & mut H ) {
121
+ todo ! ( )
122
+ }
123
+ }
124
+
125
+ impl From < u8 > for Constructor {
126
+ fn from ( value : u8 ) -> Self {
127
+ Constructor ( value)
128
+ }
129
+ }
130
+
131
+ impl Encode for Timestamp {
132
+ fn constructor ( & self ) -> Constructor {
133
+ Constructor ( 0x83 )
134
+ }
135
+
136
+ fn encode ( & self ) -> Vec < u8 > {
137
+ todo ! ( )
138
+ }
139
+ }
140
+ impl From < Timestamp > for AmqpType {
141
+ fn from ( value : Timestamp ) -> Self {
142
+ AmqpType :: Timestamp ( value)
143
+ }
144
+ }
112
145
impl Encode for bool {
113
146
#[ cfg( feature = "zero-length-bools" ) ]
114
147
fn constructor ( & self ) -> Constructor {
@@ -198,15 +231,6 @@ impl Encode for char {
198
231
}
199
232
}
200
233
201
- impl Encode for Timestamp {
202
- fn constructor ( & self ) -> Constructor {
203
- Constructor ( 0x83 )
204
- }
205
-
206
- fn encode ( & self ) -> Vec < u8 > {
207
- todo ! ( )
208
- }
209
- }
210
234
211
235
impl Encode for Uuid {
212
236
fn constructor ( & self ) -> Constructor {
@@ -284,16 +308,6 @@ impl Encode for String {
284
308
}
285
309
}
286
310
287
- impl Encode for Binary {
288
- fn constructor ( & self ) -> Constructor {
289
- todo ! ( )
290
- }
291
-
292
- fn encode ( & self ) -> Vec < u8 > {
293
- todo ! ( )
294
- }
295
- }
296
-
297
311
impl Encode for Symbol {
298
312
fn constructor ( & self ) -> Constructor {
299
313
todo ! ( )
@@ -304,36 +318,6 @@ impl Encode for Symbol {
304
318
}
305
319
}
306
320
307
- impl Encode for List {
308
- fn constructor ( & self ) -> Constructor {
309
- todo ! ( )
310
- }
311
-
312
- fn encode ( & self ) -> Vec < u8 > {
313
- todo ! ( )
314
- }
315
- }
316
-
317
- impl Encode for Map {
318
- fn constructor ( & self ) -> Constructor {
319
- todo ! ( )
320
- }
321
-
322
- fn encode ( & self ) -> Vec < u8 > {
323
- todo ! ( )
324
- }
325
- }
326
-
327
- impl Encode for Array {
328
- fn constructor ( & self ) -> Constructor {
329
- todo ! ( )
330
- }
331
-
332
- fn encode ( & self ) -> Vec < u8 > {
333
- todo ! ( )
334
- }
335
- }
336
-
337
321
impl Encode for Described {
338
322
fn constructor ( & self ) -> Constructor {
339
323
todo ! ( )
@@ -344,35 +328,6 @@ impl Encode for Described {
344
328
}
345
329
}
346
330
347
- impl Encode for Decimal32 {
348
- fn constructor ( & self ) -> Constructor {
349
- Constructor ( 0x74 )
350
- }
351
-
352
- fn encode ( & self ) -> Vec < u8 > {
353
- todo ! ( )
354
- }
355
- }
356
-
357
- impl Encode for Decimal64 {
358
- fn constructor ( & self ) -> Constructor {
359
- Constructor ( 0x84 )
360
- }
361
-
362
- fn encode ( & self ) -> Vec < u8 > {
363
- todo ! ( )
364
- }
365
- }
366
-
367
- impl Encode for Decimal128 {
368
- fn constructor ( & self ) -> Constructor {
369
- Constructor ( 0x94 )
370
- }
371
-
372
- fn encode ( & self ) -> Vec < u8 > {
373
- todo ! ( )
374
- }
375
- }
376
331
377
332
impl From < bool > for AmqpType {
378
333
fn from ( value : bool ) -> Self {
@@ -446,11 +401,6 @@ impl From<char> for AmqpType {
446
401
}
447
402
}
448
403
449
- impl From < Timestamp > for AmqpType {
450
- fn from ( value : Timestamp ) -> Self {
451
- AmqpType :: Timestamp ( value)
452
- }
453
- }
454
404
455
405
impl From < Uuid > for AmqpType {
456
406
fn from ( value : Uuid ) -> Self {
@@ -476,40 +426,24 @@ impl From<Symbol> for AmqpType {
476
426
}
477
427
}
478
428
479
- impl From < f32 > for Decimal32 {
480
- fn from ( value : f32 ) -> Self {
481
- Decimal32 ( BigDecimal :: try_from ( value) . unwrap ( ) )
429
+
430
+ impl From < List > for AmqpType {
431
+ fn from ( value : List ) -> Self {
432
+ AmqpType :: List ( value)
482
433
}
483
434
}
484
435
485
- impl From < f64 > for Decimal64 {
486
- fn from ( value : f64 ) -> Self {
487
- Decimal64 ( BigDecimal :: try_from ( value) . unwrap ( ) )
436
+ impl From < Map > for AmqpType {
437
+ fn from ( value : Map ) -> Self {
438
+ AmqpType :: Map ( value)
488
439
}
489
440
}
490
441
491
- impl From < f64 > for Decimal128 {
492
- fn from ( value : f64 ) -> Self {
493
- Decimal128 ( BigDecimal :: try_from ( value) . unwrap ( ) )
442
+ impl From < Array > for AmqpType {
443
+ fn from ( value : Array ) -> Self {
444
+ AmqpType :: Array ( value)
494
445
}
495
446
}
496
- // impl From<List> for AmqpType {
497
- // fn from(value: List) -> Self {
498
- // AmqpType::List(value)
499
- // }
500
- // }
501
-
502
- // impl From<Map> for AmqpType {
503
- // fn from(value: Map) -> Self {
504
- // AmqpType::Map(value)
505
- // }
506
- // }
507
-
508
- // impl From<Array> for AmqpType {
509
- // fn from(value: Array) -> Self {
510
- // AmqpType::Array(value)
511
- // }
512
- // }
513
447
514
448
#[ cfg( test) ]
515
449
mod tests {
@@ -671,17 +605,17 @@ mod tests {
671
605
assert_eq ! ( val. constructor( ) . 0 , 0x83 ) ;
672
606
}
673
607
674
- // #[test]
675
- // fn amqp_type_can_construct_uuid() {
676
- // let val = AmqpType::Uuid(Uuid());
677
- // assert_eq!(val.constructor().0, 0x98);
678
- // }
608
+ #[ test]
609
+ fn amqp_type_can_construct_uuid ( ) {
610
+ let val = AmqpType :: Uuid ( Uuid ( uuid :: Uuid :: new_v4 ( ) ) ) ;
611
+ assert_eq ! ( val. constructor( ) . 0 , 0x98 ) ;
612
+ }
679
613
680
- // #[test]
681
- // fn amqp_type_can_construct_binary() {
682
- // let val = AmqpType::Binary(Binary ());
683
- // assert_eq!(val.constructor().0, 0xa0);
684
- // }
614
+ #[ test]
615
+ fn amqp_type_can_construct_binary ( ) {
616
+ let val = AmqpType :: Binary ( Vec :: new ( ) . into ( ) ) ;
617
+ assert_eq ! ( val. constructor( ) . 0 , 0xa0 ) ;
618
+ }
685
619
686
620
#[ test]
687
621
fn amqp_type_encodes_strings_up_to_255_bytes_as_str8 ( ) {
@@ -695,27 +629,49 @@ mod tests {
695
629
assert_eq ! ( val. constructor( ) . 0 , 0xb1 ) ;
696
630
}
697
631
698
- // #[test]
699
- // fn amqp_type_can_construct_symbol() {
700
- // let val = AmqpType::Symbol(Symbol());
701
- // assert_eq!(val.constructor().0, 0xa3);
702
- // }
703
-
704
- // #[test]
705
- // fn amqp_type_can_construct_list() {
706
- // let val = AmqpType::List(List());
707
- // assert_eq!(val.constructor().0, 0x45);
708
- // }
709
-
710
- // #[test]
711
- // fn amqp_type_can_construct_map() {
712
- // let val = AmqpType::Map(Map());
713
- // assert_eq!(val.constructor().0, 0xc1);
714
- // }
715
-
716
- // #[test]
717
- // fn amqp_type_can_construct_array() {
718
- // let val = AmqpType::Array(Array());
719
- // assert_eq!(val.constructor().0, 0xe0);
720
- // }
632
+ #[ test]
633
+ fn amqp_type_can_construct_symbol ( ) {
634
+ let val = AmqpType :: Symbol ( Symbol ( "" . to_string ( ) ) ) ;
635
+ assert_eq ! ( val. constructor( ) . 0 , 0xa3 ) ;
636
+ }
637
+
638
+ #[ test]
639
+ fn amqp_type_can_construct_list ( ) {
640
+ let val = AmqpType :: List ( vec ! [ 1 . into( ) ] . into ( ) ) ;
641
+ assert_eq ! ( val. constructor( ) . 0 , 0x45 ) ;
642
+ }
643
+
644
+ #[ test]
645
+ fn amqp_type_can_construct_map_with_less_than_255_elements ( ) {
646
+ let val = AmqpType :: Map ( IndexMap :: new ( ) ) ;
647
+ assert_eq ! ( val. constructor( ) . 0 , 0xc1 ) ;
648
+ }
649
+
650
+ #[ test]
651
+ fn amqp_type_can_construct_map_with_less_more_255_elements ( ) {
652
+ let mut map = IndexMap :: new ( ) ;
653
+ for i in 1 .. 500 {
654
+ map. insert ( i. into ( ) , i. into ( ) ) ;
655
+ }
656
+ let val = AmqpType :: Map ( map. into ( ) ) ;
657
+ assert_eq ! ( val. constructor( ) . 0 , 0xd1 ) ;
658
+ }
659
+
660
+ #[ test]
661
+ fn amqp_type_can_construct_array_with_less_than_255_elements ( ) {
662
+ let val = AmqpType :: Array ( vec ! [ ] . into ( ) ) ;
663
+ assert_eq ! ( val. constructor( ) . 0 , 0xe0 ) ;
664
+ }
665
+
666
+ #[ test]
667
+ fn amqp_type_can_construct_array_with_more_than_255_elements ( ) {
668
+
669
+ let mut arr = vec ! [ ] ;
670
+ for i in 0 .. 500 {
671
+ arr. push ( i. into ( ) )
672
+ }
673
+ let val = AmqpType :: Array ( arr. into ( ) ) ;
674
+ assert_eq ! ( val. constructor( ) . 0 , 0xf0 ) ;
675
+
676
+ }
721
677
}
0 commit comments