Skip to content

Commit f68be5b

Browse files
implemented decode for uint
1 parent e98a540 commit f68be5b

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

amqp-type/src/fixed_width/uint.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
use crate::error::AppError;
12
use crate::serde::encode::{Encode, Encoded};
3+
use crate::serde::decode::Decode;
4+
use crate::verify::verify_bytes_read_eq;
25

36
impl Encode for u32 {
47
fn encode(&self) -> Encoded {
@@ -10,6 +13,49 @@ impl Encode for u32 {
1013
}
1114
}
1215

16+
impl Decode for u32 {
17+
fn can_decode(iter: impl Iterator<Item = u8>) -> bool {
18+
match iter.peekable().peek() {
19+
Some(0x70) => true,
20+
Some(0x52) => true,
21+
Some(0x43) => true,
22+
_ => false
23+
}
24+
}
25+
26+
fn try_decode(mut iter: impl Iterator<Item = u8>) -> Result<Self, crate::error::AppError>
27+
where
28+
Self: Sized {
29+
match iter.next() {
30+
Some(0x70) => Ok(parse_uint(iter)?),
31+
Some(0x52) => Ok(parse_small_uint(iter)?),
32+
Some(0x43) => Ok(0u32),
33+
Some(c) => Err(AppError::DeserializationIllegalConstructorError(c)),
34+
None => Err(AppError::IteratorEmptyOrTooShortError)
35+
}
36+
}
37+
}
38+
39+
fn parse_uint(mut iter: impl Iterator<Item = u8>) -> Result<u32, AppError> {
40+
let mut byte_vals = [0; 4];
41+
let mut index = 0;
42+
for b in iter.take(4) {
43+
byte_vals[index] = b;
44+
index += 1;
45+
}
46+
verify_bytes_read_eq(index, 4)?;
47+
Ok(u32::from_be_bytes(byte_vals))
48+
}
49+
50+
fn parse_small_uint(mut iter: impl Iterator<Item = u8>) -> Result<u32, AppError> {
51+
if let Some(val) = iter.next() {
52+
Ok(val as u32)
53+
} else {
54+
Err(AppError::IteratorEmptyOrTooShortError)
55+
}
56+
57+
}
58+
1359
#[cfg(test)]
1460
mod test {
1561

@@ -32,4 +78,57 @@ mod test {
3278
let val: u32 = 255;
3379
assert_eq!(val.encode().constructor(), 0x52);
3480
}
81+
82+
83+
#[test]
84+
fn can_deocde_returns_true_if_constructor_is_valid() {
85+
let val_norm = vec![0x70];
86+
let val_small = vec![0x52];
87+
let val_zero = vec![0x43];
88+
assert_eq!(u32::can_decode(val_norm.into_iter()), true);
89+
assert_eq!(u32::can_decode(val_small.into_iter()), true);
90+
assert_eq!(u32::can_decode(val_zero.into_iter()), true);
91+
}
92+
93+
#[test]
94+
fn can_decode_return_false_if_constructor_is_invalid() {
95+
let val = vec![0x71];
96+
assert_eq!(u32::can_decode(val.into_iter()), false);
97+
}
98+
99+
#[test]
100+
fn try_decode_returns_correct_value() {
101+
let val = vec![0x70, 0x00,0x00, 0x00, 0x10];
102+
assert_eq!(u32::try_decode(val.into_iter()).unwrap(), 16);
103+
}
104+
105+
#[test]
106+
fn decode_returns_error_when_value_bytes_are_invalid() {
107+
let val = vec![0x66, 0x44];
108+
assert!(u32::try_decode(val.into_iter()).is_err());
109+
}
110+
111+
#[test]
112+
fn decode_returns_error_when_bytes_are_missing() {
113+
let val = vec![0x70, 0x00, 0x00, 0x01];
114+
assert!(u32::try_decode(val.into_iter()).is_err());
115+
}
116+
117+
#[test]
118+
fn try_decode_can_decode_zero_length_value_zero() {
119+
let val = vec![0x43];
120+
assert_eq!(u32::try_decode(val.into_iter()).unwrap(), 0);
121+
}
122+
123+
#[test]
124+
fn try_decode_can_decode_smalluint_values() {
125+
let val = vec![0x52, 0xff];
126+
assert_eq!(u32::try_decode(val.into_iter()).unwrap(), 255);
127+
}
128+
129+
#[test]
130+
fn try_decode_returns_error_when_parsing_small_unint_and_bytes_are_missing() {
131+
let val = vec![0x52];
132+
assert!(u32::try_decode(val.into_iter()).is_err());
133+
}
35134
}

0 commit comments

Comments
 (0)