Skip to content

Commit 3a4d404

Browse files
restructured types into individual modules
1 parent 78fff04 commit 3a4d404

32 files changed

+835
-650
lines changed

amqp-type/src/amqp_type.rs

Lines changed: 11 additions & 510 deletions
Large diffs are not rendered by default.

amqp-type/src/array/array.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use crate::amqp_type::AmqpType;
2+
use crate::serde::encode::{Encode, Encoded};
3+
4+
#[derive(Hash, Eq, PartialEq)]
5+
pub struct Array(Vec<AmqpType>);
6+
7+
impl Encode for Array {
8+
fn encode(&self) -> Encoded {
9+
let encoded: Vec<Encoded> = self.0.iter().map(|x| x.encode()).collect();
10+
let byte_size = encoded.iter().fold(0, |acc, x| acc + x.data_len());
11+
match (encoded.len(), byte_size) {
12+
(len, size) if len <= 255 && size < 256 => 0xe0.into(),
13+
(_, _) => 0xf0.into(),
14+
}
15+
}
16+
}
17+
impl From<Vec<AmqpType>> for Array {
18+
fn from(value: Vec<AmqpType>) -> Self {
19+
Array(value)
20+
}
21+
}
22+
23+
24+
#[cfg(test)]
25+
mod test {
26+
use super::*;
27+
28+
#[test]
29+
fn construct_array_with_less_than_255_elements() {
30+
let val = Array(Vec::new());
31+
assert_eq!(val.encode().constructor(), 0xe0);
32+
}
33+
34+
#[test]
35+
fn construct_array_with_more_than_255_elements() {
36+
let mut arr = vec![];
37+
for i in 0..500 {
38+
arr.push(i.into())
39+
}
40+
let val = Array(arr);
41+
assert_eq!(val.encode().constructor(), 0xf0);
42+
}
43+
44+
#[test]
45+
fn construct_array_with_less_than_255_elements_and_larger_than_255_bytes() {
46+
let mut arr = vec![];
47+
for _ in 0..100 {
48+
arr.push("aaaaaaaaaaaaaaaaaaaa".into());
49+
}
50+
let val = Array(arr);
51+
assert_eq!(val.encode().constructor(), 0xf0);
52+
}
53+
}

amqp-type/src/array/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub(crate) mod array;

amqp-type/src/collection.rs

Lines changed: 0 additions & 99 deletions
This file was deleted.

amqp-type/src/compound/encoded_vec.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crate::serde::encode::Encoded;
2+
3+
pub struct EncodedVec(Vec<Encoded>);
4+
5+
impl EncodedVec {
6+
pub fn new(data: Vec<Encoded>) -> Self {
7+
EncodedVec(data)
8+
}
9+
}
10+
11+
impl From<EncodedVec> for Vec<u8> {
12+
fn from(value: EncodedVec) -> Self {
13+
let mut res = Vec::new();
14+
for val in value.0 {
15+
let mut enc: Vec<u8> = val.into();
16+
res.append(&mut enc);
17+
}
18+
res
19+
}
20+
}

amqp-type/src/compound/list.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use crate::amqp_type::AmqpType;
2+
use crate::compound::encoded_vec::EncodedVec;
3+
use crate::serde::encode::{Encode, Encoded};
4+
5+
#[derive(Hash, Eq, PartialEq)]
6+
pub struct List(Vec<AmqpType>);
7+
8+
impl Encode for List {
9+
fn encode(&self) -> Encoded {
10+
let encoded: Vec<Encoded> = self.0.iter().map(|x| x.encode()).collect();
11+
let count = encoded.len() as u32;
12+
let byte_size = encoded.iter().fold(0, |acc, x| acc + x.data_len());
13+
match (encoded.len(), byte_size) {
14+
(0, _) => 0x45.into(),
15+
(len, size) if len <= 255 && size < 256 => {
16+
Encoded::new_compound(0xc0, count, EncodedVec::new(encoded).into())
17+
}
18+
(_, _) => Encoded::new_compound(0xd0, count, EncodedVec::new(encoded).into()),
19+
}
20+
}
21+
}
22+
23+
impl From<Vec<AmqpType>> for List {
24+
fn from(value: Vec<AmqpType>) -> Self {
25+
List(value)
26+
}
27+
}
28+
29+
#[cfg(test)]
30+
mod test {
31+
32+
use super::*;
33+
#[test]
34+
fn construct_empty_list() {
35+
let val = AmqpType::List(vec![].into());
36+
assert_eq!(val.encode().constructor(), 0x45);
37+
}
38+
39+
#[test]
40+
fn construct_list_with_less_than_255_elements() {
41+
let val = AmqpType::List(vec![1.into()].into());
42+
assert_eq!(val.encode().constructor(), 0xc0);
43+
}
44+
45+
#[test]
46+
fn construct_list_with_more_than_255_elements() {
47+
let mut arr = vec![];
48+
for i in 0..500 {
49+
arr.push(i.into())
50+
}
51+
let val = AmqpType::List(arr.into());
52+
assert_eq!(val.encode().constructor(), 0xd0);
53+
}
54+
55+
#[test]
56+
fn construct_list_with_less_than_255_elements_and_larger_than_255_bytes() {
57+
let mut arr = vec![];
58+
for _ in 0..100 {
59+
arr.push("aaaaaaaaaaaaaaaaaaaa".into());
60+
}
61+
let val = AmqpType::List(arr.into());
62+
assert_eq!(val.encode().constructor(), 0xd0);
63+
}
64+
}

amqp-type/src/compound/map.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use std::hash::Hash;
2+
use indexmap::IndexMap;
3+
use crate::amqp_type::AmqpType;
4+
use crate::compound::encoded_vec::EncodedVec;
5+
use crate::serde::encode::{Encode, Encoded};
6+
7+
#[derive(Eq, PartialEq)]
8+
pub struct Map(IndexMap<AmqpType, AmqpType>);
9+
10+
11+
impl Encode for Map {
12+
fn encode(&self) -> Encoded {
13+
let mut res: Vec<Encoded> = Vec::new();
14+
let mut data_len = 0;
15+
let mut count = 0;
16+
for (key, value) in &self.0 {
17+
let k = key.encode();
18+
let v = value.encode();
19+
data_len += k.data_len() + v.data_len();
20+
res.push(k);
21+
res.push(v);
22+
count += 2;
23+
}
24+
match data_len {
25+
x if x <= 255 => Encoded::new_compound(0xc1, count, EncodedVec::new(res).into()),
26+
_ => Encoded::new_compound(0xd1, count, EncodedVec::new(res).into())
27+
}
28+
}
29+
}
30+
31+
impl Hash for Map {
32+
fn hash<H: std::hash::Hasher>(&self, _state: &mut H) {
33+
todo!()
34+
}
35+
}
36+
37+
impl From<IndexMap<AmqpType, AmqpType>> for Map {
38+
fn from(value: IndexMap<AmqpType, AmqpType>) -> Self {
39+
Map(value)
40+
}
41+
}
42+
43+
#[cfg(test)]
44+
mod test {
45+
46+
use super::*;
47+
#[test]
48+
fn construct_map_with_less_than_255_elements() {
49+
let val = AmqpType::Map(IndexMap::new().into());
50+
assert_eq!(val.encode().constructor(), 0xc1);
51+
}
52+
53+
#[test]
54+
fn construct_map_with_less_more_255_elements() {
55+
let mut map = IndexMap::new();
56+
for i in 1..500 {
57+
map.insert(i.into(), i.into());
58+
}
59+
let val = AmqpType::Map(map.into());
60+
assert_eq!(val.encode().constructor(), 0xd1);
61+
}
62+
}

amqp-type/src/compound/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use crate::serde::encode::Encoded;
2+
3+
pub(crate) mod list;
4+
pub(crate) mod map;
5+
mod encoded_vec;

amqp-type/src/fixed_width/boolean.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use crate::amqp_type::AmqpType;
2+
use crate::serde::encode::{Encode, Encoded};
3+
4+
impl From<bool> for AmqpType {
5+
fn from(value: bool) -> Self {
6+
AmqpType::Boolean(value)
7+
}
8+
}
9+
10+
impl Encode for bool {
11+
#[cfg(feature = "zero-length-bools")]
12+
fn encode(&self) -> Encoded {
13+
match self {
14+
true => 0x41.into(),
15+
false => 0x42.into(),
16+
}
17+
}
18+
19+
#[cfg(not(feature = "zero-length-bools"))]
20+
fn encode(&self) -> Encoded {
21+
match self {
22+
true => Encoded::new_fixed(0x56, vec![0x01]),
23+
false => Encoded::new_fixed(0x56, vec![0x00]),
24+
}
25+
}
26+
}
27+
28+
#[cfg(test)]
29+
mod test {
30+
use crate::serde::encode::Encode;
31+
use super::*;
32+
#[test]
33+
#[cfg(not(feature = "zero-length-bools"))]
34+
fn construct_bool() {
35+
let val = AmqpType::Boolean(true);
36+
assert_eq!(val.encode().constructor(), 0x56);
37+
}
38+
39+
#[test]
40+
#[cfg(feature = "zero-length-bools")]
41+
fn amqp_type_constructs_bool_false_as_zero_length() {
42+
let val = AmqpType::Boolean(false);
43+
assert_eq!(val.encode().constructor(), 0x42);
44+
}
45+
46+
#[test]
47+
#[cfg(feature = "zero-length-bools")]
48+
fn amqp_type_constructs_bool_true_as_zero_length() {
49+
let val = AmqpType::Boolean(true);
50+
assert_eq!(val.encode().constructor(), 0x41)
51+
}
52+
}

amqp-type/src/fixed_width/byte.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use crate::serde::encode::{Encode, Encoded};
2+
3+
impl Encode for i8 {
4+
fn encode(&self) -> Encoded {
5+
Encoded::new_fixed(0x51, self.to_be_bytes().to_vec())
6+
}
7+
}
8+
9+
10+
#[cfg(test)]
11+
mod test {
12+
13+
use super::*;
14+
15+
16+
#[test]
17+
fn construct_byte() {
18+
let val: i8 = 8;
19+
assert_eq!(val.encode().constructor(), 0x51);
20+
}
21+
}

0 commit comments

Comments
 (0)