Skip to content

Commit 6a62d6f

Browse files
split off some types to separate modules
1 parent c29fd89 commit 6a62d6f

File tree

7 files changed

+271
-157
lines changed

7 files changed

+271
-157
lines changed

Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amqp-lib/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ indexmap = "2.0.0"
1919
thiserror = "1.0.47"
2020
tokio = {version = "1", features=["full"]}
2121
tracing = "0.1.37"
22-
uuid = "1.4.1"
22+
uuid = {version = "1.4.1", features = ["v4"]}

amqp-lib/src/types.rs renamed to amqp-lib/src/types/amqp_type.rs

Lines changed: 112 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
1+
use std::hash::Hash;
2+
13
use bigdecimal::BigDecimal;
24
use indexmap::IndexMap;
3-
5+
use crate::types::binary::Binary;
6+
use crate::types::collection::*;
7+
use crate::types::decimal::*;
48
use crate::error::AppError;
5-
trait Encode {
9+
pub trait Encode {
610
fn constructor(&self) -> Constructor;
711
fn encode(&self) -> Vec<u8>;
812
}
913

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)]
1317
pub struct Symbol(String);
18+
#[derive(Hash, Eq, PartialEq)]
1419
pub struct Uuid(uuid::Uuid);
20+
#[derive(Hash, Eq, PartialEq)]
1521
pub struct Described();
22+
#[derive(Hash, Eq, PartialEq)]
1623
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);
2430

31+
#[derive(Hash, Eq, PartialEq)]
2532
pub enum AmqpType {
2633
Null,
2734
Boolean(bool),
@@ -33,8 +40,8 @@ pub enum AmqpType {
3340
Short(i16),
3441
Int(i32),
3542
Long(i64),
36-
Float(f32),
37-
Double(f64),
43+
Float(Float),
44+
Double(Double),
3845
Decimal32(Decimal32),
3946
Decimal64(Decimal64),
4047
Decimal128(Decimal128),
@@ -109,6 +116,32 @@ impl Encode for AmqpType {
109116
}
110117
}
111118

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+
}
112145
impl Encode for bool {
113146
#[cfg(feature = "zero-length-bools")]
114147
fn constructor(&self) -> Constructor {
@@ -198,15 +231,6 @@ impl Encode for char {
198231
}
199232
}
200233

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-
}
210234

211235
impl Encode for Uuid {
212236
fn constructor(&self) -> Constructor {
@@ -284,16 +308,6 @@ impl Encode for String {
284308
}
285309
}
286310

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-
297311
impl Encode for Symbol {
298312
fn constructor(&self) -> Constructor {
299313
todo!()
@@ -304,36 +318,6 @@ impl Encode for Symbol {
304318
}
305319
}
306320

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-
337321
impl Encode for Described {
338322
fn constructor(&self) -> Constructor {
339323
todo!()
@@ -344,35 +328,6 @@ impl Encode for Described {
344328
}
345329
}
346330

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-
}
376331

377332
impl From<bool> for AmqpType {
378333
fn from(value: bool) -> Self {
@@ -446,11 +401,6 @@ impl From<char> for AmqpType {
446401
}
447402
}
448403

449-
impl From<Timestamp> for AmqpType {
450-
fn from(value: Timestamp) -> Self {
451-
AmqpType::Timestamp(value)
452-
}
453-
}
454404

455405
impl From<Uuid> for AmqpType {
456406
fn from(value: Uuid) -> Self {
@@ -476,40 +426,24 @@ impl From<Symbol> for AmqpType {
476426
}
477427
}
478428

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)
482433
}
483434
}
484435

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)
488439
}
489440
}
490441

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)
494445
}
495446
}
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-
// }
513447

514448
#[cfg(test)]
515449
mod tests {
@@ -671,17 +605,17 @@ mod tests {
671605
assert_eq!(val.constructor().0, 0x83);
672606
}
673607

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+
}
679613

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+
}
685619

686620
#[test]
687621
fn amqp_type_encodes_strings_up_to_255_bytes_as_str8() {
@@ -695,27 +629,49 @@ mod tests {
695629
assert_eq!(val.constructor().0, 0xb1);
696630
}
697631

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+
}
721677
}

0 commit comments

Comments
 (0)