Skip to content

Commit 0020db4

Browse files
refactoring, implemented encode and decode for bool type
1 parent ee7679d commit 0020db4

33 files changed

+178
-76
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/target
2+
.idea/

amqp-type/src/amqp_type.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,4 @@ mod tests {
210210
let val = AmqpType::Null;
211211
assert_eq!(val.encode().constructor(), 0x40);
212212
}
213-
214-
215213
}

amqp-type/src/array/array.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ impl From<Vec<AmqpType>> for Array {
2020
}
2121
}
2222

23-
2423
#[cfg(test)]
2524
mod test {
2625
use super::*;
@@ -50,4 +49,4 @@ mod test {
5049
let val = Array(arr);
5150
assert_eq!(val.encode().constructor(), 0xf0);
5251
}
53-
}
52+
}

amqp-type/src/array/mod.rs

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

amqp-type/src/compound/encoded_vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ impl From<EncodedVec> for Vec<u8> {
1717
}
1818
res
1919
}
20-
}
20+
}

amqp-type/src/compound/list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ mod test {
6161
let val = List(arr);
6262
assert_eq!(val.encode().constructor(), 0xd0);
6363
}
64-
}
64+
}

amqp-type/src/compound/map.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
use std::hash::Hash;
2-
use indexmap::IndexMap;
31
use crate::amqp_type::AmqpType;
42
use crate::compound::encoded_vec::EncodedVec;
53
use crate::serde::encode::{Encode, Encoded};
4+
use indexmap::IndexMap;
5+
use std::hash::Hash;
66

77
#[derive(Eq, PartialEq)]
88
pub struct Map(IndexMap<AmqpType, AmqpType>);
99

10-
1110
impl Encode for Map {
1211
fn encode(&self) -> Encoded {
1312
let mut res: Vec<Encoded> = Vec::new();
@@ -23,7 +22,7 @@ impl Encode for Map {
2322
}
2423
match data_len {
2524
x if x <= 255 => Encoded::new_compound(0xc1, count, EncodedVec::new(res).into()),
26-
_ => Encoded::new_compound(0xd1, count, EncodedVec::new(res).into())
25+
_ => Encoded::new_compound(0xd1, count, EncodedVec::new(res).into()),
2726
}
2827
}
2928
}
@@ -59,4 +58,4 @@ mod test {
5958
let val = Map(map);
6059
assert_eq!(val.encode().constructor(), 0xd1);
6160
}
62-
}
61+
}

amqp-type/src/compound/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::serde::encode::Encoded;
22

3+
mod encoded_vec;
34
pub(crate) mod list;
45
pub(crate) mod map;
5-
mod encoded_vec;

amqp-type/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ use thiserror::Error;
44
pub enum AppError {
55
#[error("Encountered an IO Error.")]
66
IoError(#[from] std::io::Error),
7+
#[error("Error while trying to deserialize value of type `{0}`. Reason: {1}")]
8+
DeserializationError(String, String)
9+
710
}

amqp-type/src/fixed_width/boolean.rs

Lines changed: 113 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,85 @@
1+
use crate::serde::decode::Decode;
12
use crate::serde::encode::{Encode, Encoded};
3+
use crate::error::AppError;
24

5+
#[cfg(not(feature = "zero-length-bools"))]
36
impl Encode for bool {
4-
#[cfg(feature = "zero-length-bools")]
57
fn encode(&self) -> Encoded {
68
match self {
7-
true => 0x41.into(),
8-
false => 0x42.into(),
9+
true => Encoded::new_fixed(0x56, vec![0x01]),
10+
false => Encoded::new_fixed(0x56, vec![0x00]),
911
}
1012
}
13+
}
1114

12-
#[cfg(not(feature = "zero-length-bools"))]
15+
16+
#[cfg(feature = "zero-length-bools")]
17+
impl Encode for bool {
18+
1319
fn encode(&self) -> Encoded {
1420
match self {
15-
true => Encoded::new_fixed(0x56, vec![0x01]),
16-
false => Encoded::new_fixed(0x56, vec![0x00]),
21+
true => 0x41.into(),
22+
false => 0x42.into(),
23+
}
24+
}
25+
}
26+
27+
28+
29+
#[cfg(not(feature = "zero-length-bools"))]
30+
impl Decode for bool {
31+
32+
fn can_decode(data: impl Iterator<Item = u8>) -> bool {
33+
let mut iter = data.into_iter().peekable();
34+
match iter.peek() {
35+
Some(0x56) => true,
36+
_ => false
37+
}
38+
}
39+
40+
fn try_decode(mut iter: impl Iterator<Item = u8>) -> Result<Self, AppError> where Self: Sized {
41+
let con = iter.next();
42+
let val = iter.next();
43+
match (con, val) {
44+
(Some(c), Some(v)) if c == 0x56 && v == 0x00 => Ok(false),
45+
(Some(c), Some(v)) if c == 0x56 && v == 0x01 => Ok(true),
46+
(Some(c), _) => Err(AppError::DeserializationError("bool".to_string(),format!("bool cannot be constructed from value {:#04x}", c))),
47+
(Some(c), None) => Err(AppError::DeserializationError("bool".to_string(), "Iterator was empty".to_string())),
48+
(None, _) => Err(AppError::DeserializationError("bool".to_string(), "Iterator was empty".to_string())),
49+
}
50+
51+
}
52+
}
53+
54+
55+
56+
#[cfg(feature = "zero-length-bools")]
57+
impl Decode for bool {
58+
59+
fn can_decode(data: Iterator<Item = u8>) -> bool {
60+
let mut iter = data.into_iter().peekable();
61+
match iter.peek() {
62+
Some(0x41) => true,
63+
Some(0x42) => true,
64+
_ => false
65+
}
66+
}
67+
68+
fn try_decode(data: Iterator<Item = u8>) -> Result<Self, AppError> where Self: Sized {
69+
if let Some(val) = iter.next() {
70+
return match val {
71+
0x41 => Ok(true),
72+
0x42 => Ok(false),
73+
_ => Err(AppError::DeserializationError("bool".to_string(), format!("bool cannot be constructed from value {:#04x}", val)))
74+
}
1775
}
76+
Err(AppError::DeserializationError("bool".to_string(), "Iterator was empty".to_string()))
1877
}
1978
}
2079

80+
2181
#[cfg(test)]
2282
mod test {
23-
use crate::serde::encode::Encode;
24-
2583
use super::*;
2684

2785
#[test]
@@ -30,6 +88,52 @@ mod test {
3088
assert_eq!(true.encode().constructor(), 0x56);
3189
}
3290

91+
#[test]
92+
#[cfg(not(feature = "zero-length-bools"))]
93+
fn bool_gets_encoded_correctly() {
94+
assert_eq!(true.encode().to_bytes(), vec![0x56, 0x01]);
95+
assert_eq!(false.encode().to_bytes(), vec![0x56, 0x00]);
96+
}
97+
98+
#[test]
99+
#[cfg(not(feature = "zero-length-bools"))]
100+
fn can_decode_returns_true_if_constructor_is_valid() {
101+
let val_true = vec![0x56, 0x01];
102+
let val_false = vec![0x56, 0x00];
103+
assert_eq!(bool::can_decode(val_true.into_iter()), true);
104+
assert_eq!(bool::can_decode(val_false.into_iter()), true);
105+
}
106+
107+
#[test]
108+
#[cfg(not(feature = "zero-length-bools"))]
109+
fn can_decode_returns_false_if_constructor_invalid() {
110+
let val_true = vec![0x88, 0x01];
111+
let val_false = vec![0x97, 0x00];
112+
assert_eq!(bool::can_decode(val_true.into_iter()), false);
113+
assert_eq!(bool::can_decode(val_false.into_iter()), false);
114+
}
115+
116+
117+
#[test]
118+
#[cfg(not(feature = "zero-length-bools"))]
119+
fn decode_returns_error_when_value_bytes_are_invalid() {
120+
let val_true = vec![0x56, 0x34];
121+
let val_false = vec![0x56, 0x44];
122+
assert!(bool::try_decode(val_true.into_iter()).is_err());
123+
assert!(bool::try_decode(val_false.into_iter()).is_err());
124+
}
125+
126+
127+
#[test]
128+
#[cfg(not(feature = "zero-length-bools"))]
129+
fn try_decode_returns_correct_value_if_bytes_are_valid() {
130+
let val_true = vec![0x56, 0x01];
131+
let val_false = vec![0x56, 0x00];
132+
assert_eq!(bool::try_decode(val_true.into_iter()).unwrap(), true);
133+
assert_eq!(bool::try_decode(val_false.into_iter()).unwrap(), false);
134+
}
135+
136+
33137
#[test]
34138
#[cfg(feature = "zero-length-bools")]
35139
fn amqp_type_constructs_bool_false_as_zero_length() {
@@ -41,4 +145,4 @@ mod test {
41145
fn amqp_type_constructs_bool_true_as_zero_length() {
42146
assert_eq!(true.encode().constructor(), 0x41)
43147
}
44-
}
148+
}

0 commit comments

Comments
 (0)