Skip to content

Commit abc4b8a

Browse files
* fixed variable encoding
1 parent ff00473 commit abc4b8a

File tree

3 files changed

+65
-39
lines changed

3 files changed

+65
-39
lines changed

amqp-lib/src/types/amqp_type.rs

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@ use crate::types::decimal::*;
99
use crate::types::floating_point::*;
1010

1111
pub trait Hashable: Hash {}
12+
1213
pub trait Encode {
1314
fn encode(&self) -> Encoded;
1415
}
1516

1617
pub trait Decode<'a>: From<&'a [u8]> + Encode {}
1718

1819
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
2328
Array(u8, u32, u8, Vec<u8>), // Constructor, count, element constructor, data
2429
}
2530

@@ -76,11 +81,21 @@ impl From<Encoded> for Vec<u8> {
7681
}
7782
Encoded::Variable(c, mut data) => {
7883
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);
7989
res.append(&mut data);
8090
}
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+
}
8499
}
85100
res
86101
}
@@ -94,12 +109,16 @@ impl From<u8> for Encoded {
94109

95110
#[derive(Hash, Eq, PartialEq)]
96111
pub struct Symbol(String);
112+
97113
#[derive(Hash, Eq, PartialEq)]
98114
pub struct Uuid(uuid::Uuid);
115+
99116
#[derive(Hash, Eq, PartialEq)]
100117
pub struct Described();
118+
101119
#[derive(Hash, Eq, PartialEq)]
102120
pub struct Constructor(u8);
121+
103122
#[derive(Hash, Eq, PartialEq)]
104123
pub struct Timestamp(u64);
105124

@@ -163,6 +182,7 @@ impl Encode for AmqpType {
163182
}
164183

165184
impl Eq for Double {}
185+
166186
impl From<u8> for Constructor {
167187
fn from(value: u8) -> Self {
168188
Constructor(value)
@@ -174,11 +194,13 @@ impl Encode for Timestamp {
174194
0x83.into()
175195
}
176196
}
197+
177198
impl From<Timestamp> for AmqpType {
178199
fn from(value: Timestamp) -> Self {
179200
AmqpType::Timestamp(value)
180201
}
181202
}
203+
182204
impl Encode for bool {
183205
#[cfg(feature = "zero-length-bools")]
184206
fn encode(&self) -> Encoded {
@@ -232,6 +254,7 @@ impl Encode for Uuid {
232254
Encoded::new_fixed(0x98, self.0.into_bytes().to_vec())
233255
}
234256
}
257+
235258
impl Encode for u32 {
236259
fn encode(&self) -> Encoded {
237260
match self {
@@ -421,14 +444,14 @@ mod tests {
421444
use super::*;
422445

423446
#[test]
424-
fn amqp_type_can_construct_null() {
447+
fn construct_null() {
425448
let val = AmqpType::Null;
426449
assert_eq!(val.encode().constructor(), 0x40);
427450
}
428451

429452
#[test]
430453
#[cfg(not(feature = "zero-length-bools"))]
431-
fn amqp_type_can_construct_bool() {
454+
fn construct_bool() {
432455
let val = AmqpType::Boolean(true);
433456
assert_eq!(val.encode().constructor(), 0x56);
434457
}
@@ -448,19 +471,19 @@ mod tests {
448471
}
449472

450473
#[test]
451-
fn amqp_type_can_construct_ubyte() {
474+
fn construct_ubyte() {
452475
let val = AmqpType::Ubyte(8);
453476
assert_eq!(val.encode().constructor(), 0x50);
454477
}
455478

456479
#[test]
457-
fn amqp_type_can_construct_ushort() {
480+
fn construct_ushort() {
458481
let val = AmqpType::Ushort(16);
459482
assert_eq!(val.encode().constructor(), 0x60);
460483
}
461484

462485
#[test]
463-
fn amqp_type_can_construct_uint() {
486+
fn construct_uint() {
464487
let val = AmqpType::Uint(500);
465488
assert_eq!(val.encode().constructor(), 0x70);
466489
}
@@ -476,8 +499,9 @@ mod tests {
476499
let val = AmqpType::Uint(255);
477500
assert_eq!(val.encode().constructor(), 0x52);
478501
}
502+
479503
#[test]
480-
fn amqp_type_can_construct_ulong() {
504+
fn construct_ulong() {
481505
let val = AmqpType::Ulong(500);
482506
assert_eq!(val.encode().constructor(), 0x80);
483507
}
@@ -495,19 +519,19 @@ mod tests {
495519
}
496520

497521
#[test]
498-
fn amqp_type_can_construct_byte() {
522+
fn construct_byte() {
499523
let val = AmqpType::Byte(8);
500524
assert_eq!(val.encode().constructor(), 0x51);
501525
}
502526

503527
#[test]
504-
fn amqp_type_can_construct_short() {
528+
fn construct_short() {
505529
let val = AmqpType::Short(8);
506530
assert_eq!(val.encode().constructor(), 0x61);
507531
}
508532

509533
#[test]
510-
fn amqp_type_can_construct_int() {
534+
fn construct_int() {
511535
let val = AmqpType::Int(500);
512536
assert_eq!(val.encode().constructor(), 0x71);
513537
}
@@ -519,8 +543,9 @@ mod tests {
519543
assert_eq!(lower.encode().constructor(), 0x54);
520544
assert_eq!(higher.encode().constructor(), 0x54);
521545
}
546+
522547
#[test]
523-
fn amqp_type_can_construct_long() {
548+
fn construct_long() {
524549
let val = AmqpType::Long(500);
525550
assert_eq!(val.encode().constructor(), 0x81);
526551
}
@@ -534,55 +559,55 @@ mod tests {
534559
}
535560

536561
#[test]
537-
fn amqp_type_can_construct_float() {
562+
fn construct_float() {
538563
let val = AmqpType::Float(32.0.into());
539564
assert_eq!(val.encode().constructor(), 0x72);
540565
}
541566

542567
#[test]
543-
fn amqp_type_can_construct_double() {
568+
fn construct_double() {
544569
let val = AmqpType::Double(64.0.into());
545570
assert_eq!(val.encode().constructor(), 0x82);
546571
}
547572

548573
#[test]
549-
fn amqp_type_can_construct_decimal_32() {
574+
fn construct_decimal_32() {
550575
let val = AmqpType::Decimal32(32.0.into());
551576
assert_eq!(val.encode().constructor(), 0x74);
552577
}
553578

554579
#[test]
555-
fn amqp_type_can_construct_decimal_64() {
580+
fn construct_decimal_64() {
556581
let val = AmqpType::Decimal64(64.0.into());
557582
assert_eq!(val.encode().constructor(), 0x84);
558583
}
559584

560585
#[test]
561-
fn amqp_type_can_construct_decimal_128() {
586+
fn construct_decimal_128() {
562587
let val = AmqpType::Decimal128(128.0.into());
563588
assert_eq!(val.encode().constructor(), 0x94);
564589
}
565590

566591
#[test]
567-
fn amqp_type_can_construct_char() {
592+
fn construct_char() {
568593
let val = AmqpType::Char('a');
569594
assert_eq!(val.encode().constructor(), 0x73);
570595
}
571596

572597
#[test]
573-
fn amqp_type_can_construct_timestamp() {
598+
fn construct_timestamp() {
574599
let val = AmqpType::Timestamp(Timestamp(1));
575600
assert_eq!(val.encode().constructor(), 0x83);
576601
}
577602

578603
#[test]
579-
fn amqp_type_can_construct_uuid() {
604+
fn construct_uuid() {
580605
let val = AmqpType::Uuid(Uuid(uuid::Uuid::new_v4()));
581606
assert_eq!(val.encode().constructor(), 0x98);
582607
}
583608

584609
#[test]
585-
fn amqp_type_can_construct_binary() {
610+
fn construct_binary() {
586611
let val = AmqpType::Binary(Vec::new().into());
587612
assert_eq!(val.encode().constructor(), 0xa0);
588613
}
@@ -600,25 +625,25 @@ mod tests {
600625
}
601626

602627
#[test]
603-
fn amqp_type_can_construct_symbol() {
628+
fn construct_symbol() {
604629
let val = AmqpType::Symbol(Symbol("".to_string()));
605630
assert_eq!(val.encode().constructor(), 0xa3);
606631
}
607632

608633
#[test]
609-
fn amqp_type_can_construct_empty_list() {
634+
fn construct_empty_list() {
610635
let val = AmqpType::List(vec![].into());
611636
assert_eq!(val.encode().constructor(), 0x45);
612637
}
613638

614639
#[test]
615-
fn amqp_type_can_construct_list_with_less_than_255_elements() {
640+
fn construct_list_with_less_than_255_elements() {
616641
let val = AmqpType::List(vec![1.into()].into());
617642
assert_eq!(val.encode().constructor(), 0xc0);
618643
}
619644

620645
#[test]
621-
fn amqp_type_can_construct_list_with_more_than_255_elements() {
646+
fn construct_list_with_more_than_255_elements() {
622647
let mut arr = vec![];
623648
for i in 0..500 {
624649
arr.push(i.into())
@@ -628,7 +653,7 @@ mod tests {
628653
}
629654

630655
#[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() {
632657
let mut arr = vec![];
633658
for i in 0..100 {
634659
arr.push("aaaaaaaaaaaaaaaaaaaa".into());
@@ -638,13 +663,13 @@ mod tests {
638663
}
639664

640665
#[test]
641-
fn amqp_type_can_construct_map_with_less_than_255_elements() {
666+
fn construct_map_with_less_than_255_elements() {
642667
let val = AmqpType::Map(IndexMap::new().into());
643668
assert_eq!(val.encode().constructor(), 0xc1);
644669
}
645670

646671
#[test]
647-
fn amqp_type_can_construct_map_with_less_more_255_elements() {
672+
fn construct_map_with_less_more_255_elements() {
648673
let mut map = IndexMap::new();
649674
for i in 1..500 {
650675
map.insert(i.into(), i.into());
@@ -654,13 +679,13 @@ mod tests {
654679
}
655680

656681
#[test]
657-
fn amqp_type_can_construct_array_with_less_than_255_elements() {
682+
fn construct_array_with_less_than_255_elements() {
658683
let val = AmqpType::Array(vec![].into());
659684
assert_eq!(val.encode().constructor(), 0xe0);
660685
}
661686

662687
#[test]
663-
fn amqp_type_can_construct_array_with_more_than_255_elements() {
688+
fn construct_array_with_more_than_255_elements() {
664689
let mut arr = vec![];
665690
for i in 0..500 {
666691
arr.push(i.into())
@@ -670,7 +695,7 @@ mod tests {
670695
}
671696

672697
#[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() {
674699
let mut arr = vec![];
675700
for i in 0..100 {
676701
arr.push("aaaaaaaaaaaaaaaaaaaa".into());

amqp-lib/src/types/binary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::types::amqp_type::{Constructor, Encode};
1+
use crate::types::amqp_type::Encode;
22

33
use super::amqp_type::Encoded;
44

amqp-lib/src/types/floating_point.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use crate::types::amqp_type::{Encode, Encoded};
21
use std::hash::Hash;
32

3+
use crate::types::amqp_type::{Encode, Encoded};
4+
45
pub struct Float(f32);
56
pub struct Double(f64);
67

@@ -37,7 +38,7 @@ impl Eq for Float {}
3738

3839
impl PartialEq for Double {
3940
fn eq(&self, other: &Self) -> bool {
40-
self.0.to_bits() == self.0.to_bits()
41+
self.0.to_bits() == other.0.to_bits()
4142
}
4243

4344
fn ne(&self, other: &Self) -> bool {

0 commit comments

Comments
 (0)