Skip to content

Commit ff00473

Browse files
* Properly implemented List encode
* Rustfmt
1 parent 37c8439 commit ff00473

File tree

4 files changed

+49
-66
lines changed

4 files changed

+49
-66
lines changed

amqp-lib/src/types/amqp_type.rs

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,46 @@
11
use std::hash::Hash;
22

3-
use crate::error::AppError;
3+
use bigdecimal::num_traits::ToBytes;
4+
use indexmap::IndexMap;
5+
46
use crate::types::binary::Binary;
57
use crate::types::collection::*;
68
use crate::types::decimal::*;
79
use crate::types::floating_point::*;
8-
use bigdecimal::BigDecimal;
9-
use bigdecimal::num_traits::ToBytes;
10-
use indexmap::IndexMap;
1110

12-
pub trait Hashable: std::hash::Hash {}
11+
pub trait Hashable: Hash {}
1312
pub trait Encode {
1413
fn encode(&self) -> Encoded;
1514
}
1615

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

19-
20-
2118
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
2724
}
2825

29-
3026
impl Encoded {
3127
pub fn new_empty(constructor: u8) -> Self {
3228
Encoded::Empty(constructor)
3329
}
3430

35-
pub fn new_fixed(constructor: u8, data: Vec<u8>) -> Self {
31+
pub fn new_fixed(constructor: u8, data: Vec<u8>) -> Self {
3632
Encoded::Fixed(constructor, data)
37-
}
33+
}
3834

39-
pub fn new_variable(constructor: u8, data: Vec<u8>) -> Self {
35+
pub fn new_variable(constructor: u8, data: Vec<u8>) -> Self {
4036
Encoded::Variable(constructor, data)
4137
}
4238

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 {
4440
Encoded::Compound(constructor, count, data)
4541
}
4642

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 {
4844
Encoded::Array(constructor, count, element_constructor, data)
4945
}
5046

@@ -54,7 +50,7 @@ impl Encoded {
5450
Self::Fixed(c, _) => c.to_owned(),
5551
Self::Variable(c, _) => c.to_owned(),
5652
Self::Compound(c, _, _) => c.to_owned(),
57-
Self::Array(c, _, _, _) => c.to_owned()
53+
Self::Array(c, _, _, _) => c.to_owned(),
5854
}
5955
}
6056

@@ -65,7 +61,6 @@ impl Encoded {
6561
Self::Variable(_, data) => data.len(),
6662
Self::Compound(_, _, data) => data.len(),
6763
Self::Array(_, _, _, data) => data.len(),
68-
6964
}
7065
}
7166
}
@@ -75,29 +70,19 @@ impl From<Encoded> for Vec<u8> {
7570
let mut res = Vec::new();
7671
match value {
7772
Encoded::Empty(c) => res.push(c),
78-
Encoded::Fixed(c, data) => {
73+
Encoded::Fixed(c, mut data) => {
7974
res.push(c);
80-
res.append(data);
75+
res.append(&mut data);
8176
}
82-
Encoded::Variable(c, data) => {
77+
Encoded::Variable(c, mut data) => {
8378
res.push(c);
84-
res.append(data);
79+
res.append(&mut data);
8580
}
86-
Encoded::Compound(c, count, data) => {
87-
88-
}
89-
}
90-
res
91-
}
92-
}
81+
Encoded::Compound(c, count, data) => {}
9382

94-
95-
impl From<Encoded> for &mut Vec<u8> {
96-
fn from(value: Encoded) -> Self {
97-
match value {
98-
83+
Encoded::Array(_, _, _, _) => {}
9984
}
100-
todo!()
85+
res
10186
}
10287
}
10388

@@ -207,7 +192,7 @@ impl Encode for bool {
207192
fn encode(&self) -> Encoded {
208193
match self {
209194
true => Encoded::new_fixed(0x56, vec![0x01]),
210-
false => Encoded::new_fixed(0x56, vec![0x00])
195+
false => Encoded::new_fixed(0x56, vec![0x00]),
211196
}
212197
}
213198
}
@@ -262,7 +247,7 @@ impl Encode for u64 {
262247
match self {
263248
0 => Encoded::new_empty(0x44),
264249
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()),
266251
}
267252
}
268253
}
@@ -271,7 +256,7 @@ impl Encode for i32 {
271256
fn encode(&self) -> Encoded {
272257
match self {
273258
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()),
275260
}
276261
}
277262
}
@@ -280,7 +265,7 @@ impl Encode for i64 {
280265
fn encode(&self) -> Encoded {
281266
match self {
282267
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()),
284269
}
285270
}
286271
}
@@ -291,7 +276,7 @@ impl Encode for String {
291276
x if x >= 0 as usize && x <= 255 as usize => {
292277
Encoded::new_variable(0xa1, self.as_bytes().to_vec())
293278
}
294-
_ => Encoded::new_variable(0xb1, self.as_bytes().to_vec())
279+
_ => Encoded::new_variable(0xb1, self.as_bytes().to_vec()),
295280
}
296281
}
297282
}
@@ -371,14 +356,12 @@ impl From<Float> for AmqpType {
371356
}
372357
}
373358

374-
375359
impl From<Double> for AmqpType {
376360
fn from(value: Double) -> Self {
377361
AmqpType::Double(value.into())
378362
}
379363
}
380364

381-
382365
impl From<char> for AmqpType {
383366
fn from(value: char) -> Self {
384367
AmqpType::Char(value)
@@ -435,7 +418,6 @@ impl From<Array> for AmqpType {
435418

436419
#[cfg(test)]
437420
mod tests {
438-
439421
use super::*;
440422

441423
#[test]
@@ -695,6 +677,5 @@ mod tests {
695677
}
696678
let val = AmqpType::Array(arr.into());
697679
assert_eq!(val.encode().constructor(), 0xf0);
698-
699680
}
700681
}

amqp-lib/src/types/binary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ impl Encode for Binary {
99
fn encode(&self) -> Encoded {
1010
match self.0.len() {
1111
x if x <= 255 => Encoded::new_variable(0xa0, self.0.to_owned()),
12-
_ => Encoded::new_variable(0xb0, self.0.to_owned())
12+
_ => Encoded::new_variable(0xb0, self.0.to_owned()),
1313
}
1414
}
1515
}

amqp-lib/src/types/collection.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
1-
use crate::types::amqp_type::{AmqpType, Constructor, Encode};
2-
use indexmap::IndexMap;
3-
use tracing::info;
41
use std::hash::Hash;
52

3+
use indexmap::IndexMap;
4+
5+
use crate::types::amqp_type::{AmqpType, Encode};
6+
67
use super::amqp_type::Encoded;
78

89
#[derive(Hash, Eq, PartialEq)]
910
pub struct List(Vec<AmqpType>);
11+
1012
#[derive(Hash, Eq, PartialEq)]
1113
pub struct Array(Vec<AmqpType>);
14+
1215
#[derive(Eq, PartialEq)]
1316
pub struct Map(IndexMap<AmqpType, AmqpType>);
1417

15-
struct EncodedCompound(Vec<Encoded>);
18+
struct EncodedVec(Vec<Encoded>);
19+
1620
impl Encode for List {
1721
fn encode(&self) -> Encoded {
1822
let encoded: Vec<Encoded> = self.0.iter().map(|x| x.encode()).collect();
19-
let count = encoded.len();
23+
let count = encoded.len() as u32;
2024
let byte_size = encoded.iter().fold(0, |acc, x| acc + x.data_len());
2125
match (encoded.len(), byte_size) {
2226
(0, _) => 0x45.into(),
23-
(len, size) if len <= 255 && size < 256 => Encoded::new_compound(0xc0, count as u32 as u32, EncodedCompound(encoded).into()),
24-
(_, _) =>Encoded::new_compound(0xd0, count as u32 as u32, EncodedCompound(encoded).into()),
27+
(len, size) if len <= 255 && size < 256 => {
28+
Encoded::new_compound(0xc0, count, EncodedVec(encoded).into())
29+
}
30+
(_, _) => Encoded::new_compound(0xd0, count, EncodedVec(encoded).into()),
2531
}
2632
}
2733
}
@@ -40,15 +46,15 @@ impl Encode for Array {
4046
(len, size) if len <= 255 && size < 256 => 0xe0.into(),
4147
(_, _) => 0xf0.into(),
4248
}
43-
4449
}
4550
}
4651

47-
impl From<EncodedCompound> for Vec<u8> {
48-
fn from(value: EncodedCompound) -> Self {
52+
impl From<EncodedVec> for Vec<u8> {
53+
fn from(value: EncodedVec) -> Self {
4954
let mut res = Vec::new();
50-
for val in value.0 {
51-
res.append(val.into());
55+
for mut val in value.0 {
56+
let mut enc: Vec<u8> = val.into();
57+
res.append(&mut enc);
5258
}
5359
res
5460
}

amqp-lib/src/types/floating_point.rs

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

44
pub struct Float(f32);
55
pub struct Double(f64);
66

7-
/// Crate assumes nothing about the values being passed to it.
8-
/// Any kind of f32 value is handled as is.
7+
/// Crate assumes nothing about the values being passed to it.
8+
/// Any kind of f32 value is handled as is.
99
/// This means that the hash function considers only the bytes of a float.
1010
/// Two f32 or f64 values are considered Equal if and only if, the entirety of their by sequences match.
1111
/// This ensures that no information is lost.
@@ -45,8 +45,6 @@ impl PartialEq for Double {
4545
}
4646
}
4747

48-
49-
5048
impl Encode for Float {
5149
fn encode(&self) -> Encoded {
5250
Encoded::new_fixed(0x72, self.0.to_be_bytes().to_vec())
@@ -59,8 +57,6 @@ impl Encode for Double {
5957
}
6058
}
6159

62-
63-
6460
impl From<f32> for Float {
6561
fn from(value: f32) -> Self {
6662
Float(value)

0 commit comments

Comments
 (0)