@@ -9,17 +9,22 @@ use crate::types::decimal::*;
9
9
use crate :: types:: floating_point:: * ;
10
10
11
11
pub trait Hashable : Hash { }
12
+
12
13
pub trait Encode {
13
14
fn encode ( & self ) -> Encoded ;
14
15
}
15
16
16
17
pub trait Decode < ' a > : From < & ' a [ u8 ] > + Encode { }
17
18
18
19
pub enum Encoded {
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
20
+ Empty ( u8 ) ,
21
+ // Constructor
22
+ Fixed ( u8 , Vec < u8 > ) ,
23
+ // Constructor, Data
24
+ Variable ( u8 , Vec < u8 > ) ,
25
+ // Constructor, Data, size is computed from data
26
+ Compound ( u8 , u32 , Vec < u8 > ) ,
27
+ // Constructor, count, data
23
28
Array ( u8 , u32 , u8 , Vec < u8 > ) , // Constructor, count, element constructor, data
24
29
}
25
30
@@ -76,11 +81,21 @@ impl From<Encoded> for Vec<u8> {
76
81
}
77
82
Encoded :: Variable ( c, mut data) => {
78
83
res. push ( c) ;
84
+ let mut size: Vec < u8 > = match c {
85
+ 0xA => vec ! [ data. len( ) as u8 ] ,
86
+ _ => ( data. len ( ) as u32 ) . to_be_bytes ( ) . to_vec ( )
87
+ } ;
88
+ res. append ( & mut size) ;
79
89
res. append ( & mut data) ;
80
90
}
81
- Encoded :: Compound ( c, count, data) => { }
82
-
83
- Encoded :: Array ( _, _, _, _) => { }
91
+ Encoded :: Compound ( c, count, mut data) => {
92
+ res. push ( c) ;
93
+ res. append ( & mut count. to_be_bytes ( ) . to_vec ( ) ) ;
94
+ res. append ( & mut data) ;
95
+ }
96
+ Encoded :: Array ( _, _, _, _) => {
97
+ todo ! ( "Implement Array encode to bytes" )
98
+ }
84
99
}
85
100
res
86
101
}
@@ -94,12 +109,16 @@ impl From<u8> for Encoded {
94
109
95
110
#[ derive( Hash , Eq , PartialEq ) ]
96
111
pub struct Symbol ( String ) ;
112
+
97
113
#[ derive( Hash , Eq , PartialEq ) ]
98
114
pub struct Uuid ( uuid:: Uuid ) ;
115
+
99
116
#[ derive( Hash , Eq , PartialEq ) ]
100
117
pub struct Described ( ) ;
118
+
101
119
#[ derive( Hash , Eq , PartialEq ) ]
102
120
pub struct Constructor ( u8 ) ;
121
+
103
122
#[ derive( Hash , Eq , PartialEq ) ]
104
123
pub struct Timestamp ( u64 ) ;
105
124
@@ -163,6 +182,7 @@ impl Encode for AmqpType {
163
182
}
164
183
165
184
impl Eq for Double { }
185
+
166
186
impl From < u8 > for Constructor {
167
187
fn from ( value : u8 ) -> Self {
168
188
Constructor ( value)
@@ -174,11 +194,13 @@ impl Encode for Timestamp {
174
194
0x83 . into ( )
175
195
}
176
196
}
197
+
177
198
impl From < Timestamp > for AmqpType {
178
199
fn from ( value : Timestamp ) -> Self {
179
200
AmqpType :: Timestamp ( value)
180
201
}
181
202
}
203
+
182
204
impl Encode for bool {
183
205
#[ cfg( feature = "zero-length-bools" ) ]
184
206
fn encode ( & self ) -> Encoded {
@@ -232,6 +254,7 @@ impl Encode for Uuid {
232
254
Encoded :: new_fixed ( 0x98 , self . 0 . into_bytes ( ) . to_vec ( ) )
233
255
}
234
256
}
257
+
235
258
impl Encode for u32 {
236
259
fn encode ( & self ) -> Encoded {
237
260
match self {
@@ -421,14 +444,14 @@ mod tests {
421
444
use super :: * ;
422
445
423
446
#[ test]
424
- fn amqp_type_can_construct_null ( ) {
447
+ fn construct_null ( ) {
425
448
let val = AmqpType :: Null ;
426
449
assert_eq ! ( val. encode( ) . constructor( ) , 0x40 ) ;
427
450
}
428
451
429
452
#[ test]
430
453
#[ cfg( not( feature = "zero-length-bools" ) ) ]
431
- fn amqp_type_can_construct_bool ( ) {
454
+ fn construct_bool ( ) {
432
455
let val = AmqpType :: Boolean ( true ) ;
433
456
assert_eq ! ( val. encode( ) . constructor( ) , 0x56 ) ;
434
457
}
@@ -448,19 +471,19 @@ mod tests {
448
471
}
449
472
450
473
#[ test]
451
- fn amqp_type_can_construct_ubyte ( ) {
474
+ fn construct_ubyte ( ) {
452
475
let val = AmqpType :: Ubyte ( 8 ) ;
453
476
assert_eq ! ( val. encode( ) . constructor( ) , 0x50 ) ;
454
477
}
455
478
456
479
#[ test]
457
- fn amqp_type_can_construct_ushort ( ) {
480
+ fn construct_ushort ( ) {
458
481
let val = AmqpType :: Ushort ( 16 ) ;
459
482
assert_eq ! ( val. encode( ) . constructor( ) , 0x60 ) ;
460
483
}
461
484
462
485
#[ test]
463
- fn amqp_type_can_construct_uint ( ) {
486
+ fn construct_uint ( ) {
464
487
let val = AmqpType :: Uint ( 500 ) ;
465
488
assert_eq ! ( val. encode( ) . constructor( ) , 0x70 ) ;
466
489
}
@@ -476,8 +499,9 @@ mod tests {
476
499
let val = AmqpType :: Uint ( 255 ) ;
477
500
assert_eq ! ( val. encode( ) . constructor( ) , 0x52 ) ;
478
501
}
502
+
479
503
#[ test]
480
- fn amqp_type_can_construct_ulong ( ) {
504
+ fn construct_ulong ( ) {
481
505
let val = AmqpType :: Ulong ( 500 ) ;
482
506
assert_eq ! ( val. encode( ) . constructor( ) , 0x80 ) ;
483
507
}
@@ -495,19 +519,19 @@ mod tests {
495
519
}
496
520
497
521
#[ test]
498
- fn amqp_type_can_construct_byte ( ) {
522
+ fn construct_byte ( ) {
499
523
let val = AmqpType :: Byte ( 8 ) ;
500
524
assert_eq ! ( val. encode( ) . constructor( ) , 0x51 ) ;
501
525
}
502
526
503
527
#[ test]
504
- fn amqp_type_can_construct_short ( ) {
528
+ fn construct_short ( ) {
505
529
let val = AmqpType :: Short ( 8 ) ;
506
530
assert_eq ! ( val. encode( ) . constructor( ) , 0x61 ) ;
507
531
}
508
532
509
533
#[ test]
510
- fn amqp_type_can_construct_int ( ) {
534
+ fn construct_int ( ) {
511
535
let val = AmqpType :: Int ( 500 ) ;
512
536
assert_eq ! ( val. encode( ) . constructor( ) , 0x71 ) ;
513
537
}
@@ -519,8 +543,9 @@ mod tests {
519
543
assert_eq ! ( lower. encode( ) . constructor( ) , 0x54 ) ;
520
544
assert_eq ! ( higher. encode( ) . constructor( ) , 0x54 ) ;
521
545
}
546
+
522
547
#[ test]
523
- fn amqp_type_can_construct_long ( ) {
548
+ fn construct_long ( ) {
524
549
let val = AmqpType :: Long ( 500 ) ;
525
550
assert_eq ! ( val. encode( ) . constructor( ) , 0x81 ) ;
526
551
}
@@ -534,55 +559,55 @@ mod tests {
534
559
}
535
560
536
561
#[ test]
537
- fn amqp_type_can_construct_float ( ) {
562
+ fn construct_float ( ) {
538
563
let val = AmqpType :: Float ( 32.0 . into ( ) ) ;
539
564
assert_eq ! ( val. encode( ) . constructor( ) , 0x72 ) ;
540
565
}
541
566
542
567
#[ test]
543
- fn amqp_type_can_construct_double ( ) {
568
+ fn construct_double ( ) {
544
569
let val = AmqpType :: Double ( 64.0 . into ( ) ) ;
545
570
assert_eq ! ( val. encode( ) . constructor( ) , 0x82 ) ;
546
571
}
547
572
548
573
#[ test]
549
- fn amqp_type_can_construct_decimal_32 ( ) {
574
+ fn construct_decimal_32 ( ) {
550
575
let val = AmqpType :: Decimal32 ( 32.0 . into ( ) ) ;
551
576
assert_eq ! ( val. encode( ) . constructor( ) , 0x74 ) ;
552
577
}
553
578
554
579
#[ test]
555
- fn amqp_type_can_construct_decimal_64 ( ) {
580
+ fn construct_decimal_64 ( ) {
556
581
let val = AmqpType :: Decimal64 ( 64.0 . into ( ) ) ;
557
582
assert_eq ! ( val. encode( ) . constructor( ) , 0x84 ) ;
558
583
}
559
584
560
585
#[ test]
561
- fn amqp_type_can_construct_decimal_128 ( ) {
586
+ fn construct_decimal_128 ( ) {
562
587
let val = AmqpType :: Decimal128 ( 128.0 . into ( ) ) ;
563
588
assert_eq ! ( val. encode( ) . constructor( ) , 0x94 ) ;
564
589
}
565
590
566
591
#[ test]
567
- fn amqp_type_can_construct_char ( ) {
592
+ fn construct_char ( ) {
568
593
let val = AmqpType :: Char ( 'a' ) ;
569
594
assert_eq ! ( val. encode( ) . constructor( ) , 0x73 ) ;
570
595
}
571
596
572
597
#[ test]
573
- fn amqp_type_can_construct_timestamp ( ) {
598
+ fn construct_timestamp ( ) {
574
599
let val = AmqpType :: Timestamp ( Timestamp ( 1 ) ) ;
575
600
assert_eq ! ( val. encode( ) . constructor( ) , 0x83 ) ;
576
601
}
577
602
578
603
#[ test]
579
- fn amqp_type_can_construct_uuid ( ) {
604
+ fn construct_uuid ( ) {
580
605
let val = AmqpType :: Uuid ( Uuid ( uuid:: Uuid :: new_v4 ( ) ) ) ;
581
606
assert_eq ! ( val. encode( ) . constructor( ) , 0x98 ) ;
582
607
}
583
608
584
609
#[ test]
585
- fn amqp_type_can_construct_binary ( ) {
610
+ fn construct_binary ( ) {
586
611
let val = AmqpType :: Binary ( Vec :: new ( ) . into ( ) ) ;
587
612
assert_eq ! ( val. encode( ) . constructor( ) , 0xa0 ) ;
588
613
}
@@ -600,25 +625,25 @@ mod tests {
600
625
}
601
626
602
627
#[ test]
603
- fn amqp_type_can_construct_symbol ( ) {
628
+ fn construct_symbol ( ) {
604
629
let val = AmqpType :: Symbol ( Symbol ( "" . to_string ( ) ) ) ;
605
630
assert_eq ! ( val. encode( ) . constructor( ) , 0xa3 ) ;
606
631
}
607
632
608
633
#[ test]
609
- fn amqp_type_can_construct_empty_list ( ) {
634
+ fn construct_empty_list ( ) {
610
635
let val = AmqpType :: List ( vec ! [ ] . into ( ) ) ;
611
636
assert_eq ! ( val. encode( ) . constructor( ) , 0x45 ) ;
612
637
}
613
638
614
639
#[ test]
615
- fn amqp_type_can_construct_list_with_less_than_255_elements ( ) {
640
+ fn construct_list_with_less_than_255_elements ( ) {
616
641
let val = AmqpType :: List ( vec ! [ 1 . into( ) ] . into ( ) ) ;
617
642
assert_eq ! ( val. encode( ) . constructor( ) , 0xc0 ) ;
618
643
}
619
644
620
645
#[ test]
621
- fn amqp_type_can_construct_list_with_more_than_255_elements ( ) {
646
+ fn construct_list_with_more_than_255_elements ( ) {
622
647
let mut arr = vec ! [ ] ;
623
648
for i in 0 ..500 {
624
649
arr. push ( i. into ( ) )
@@ -628,7 +653,7 @@ mod tests {
628
653
}
629
654
630
655
#[ test]
631
- fn amqp_type_can_construct_list_with_less_than_255_elements_and_larger_than_255_bytes ( ) {
656
+ fn construct_list_with_less_than_255_elements_and_larger_than_255_bytes ( ) {
632
657
let mut arr = vec ! [ ] ;
633
658
for i in 0 ..100 {
634
659
arr. push ( "aaaaaaaaaaaaaaaaaaaa" . into ( ) ) ;
@@ -638,13 +663,13 @@ mod tests {
638
663
}
639
664
640
665
#[ test]
641
- fn amqp_type_can_construct_map_with_less_than_255_elements ( ) {
666
+ fn construct_map_with_less_than_255_elements ( ) {
642
667
let val = AmqpType :: Map ( IndexMap :: new ( ) . into ( ) ) ;
643
668
assert_eq ! ( val. encode( ) . constructor( ) , 0xc1 ) ;
644
669
}
645
670
646
671
#[ test]
647
- fn amqp_type_can_construct_map_with_less_more_255_elements ( ) {
672
+ fn construct_map_with_less_more_255_elements ( ) {
648
673
let mut map = IndexMap :: new ( ) ;
649
674
for i in 1 ..500 {
650
675
map. insert ( i. into ( ) , i. into ( ) ) ;
@@ -654,13 +679,13 @@ mod tests {
654
679
}
655
680
656
681
#[ test]
657
- fn amqp_type_can_construct_array_with_less_than_255_elements ( ) {
682
+ fn construct_array_with_less_than_255_elements ( ) {
658
683
let val = AmqpType :: Array ( vec ! [ ] . into ( ) ) ;
659
684
assert_eq ! ( val. encode( ) . constructor( ) , 0xe0 ) ;
660
685
}
661
686
662
687
#[ test]
663
- fn amqp_type_can_construct_array_with_more_than_255_elements ( ) {
688
+ fn construct_array_with_more_than_255_elements ( ) {
664
689
let mut arr = vec ! [ ] ;
665
690
for i in 0 ..500 {
666
691
arr. push ( i. into ( ) )
@@ -670,7 +695,7 @@ mod tests {
670
695
}
671
696
672
697
#[ test]
673
- fn amqp_type_can_construct_array_with_less_than_255_elements_and_larger_than_255_bytes ( ) {
698
+ fn construct_array_with_less_than_255_elements_and_larger_than_255_bytes ( ) {
674
699
let mut arr = vec ! [ ] ;
675
700
for i in 0 ..100 {
676
701
arr. push ( "aaaaaaaaaaaaaaaaaaaa" . into ( ) ) ;
0 commit comments