Skip to content

Commit e98a540

Browse files
refactored the error enum, added module to handle verification functions, implemented decode for ushort
1 parent f99b175 commit e98a540

File tree

7 files changed

+60
-43
lines changed

7 files changed

+60
-43
lines changed

amqp-type/src/error.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ 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),
7+
#[error("Error while trying to deserialize value. Constructor {0:#04x} was invalid.")]
8+
DeserializationIllegalConstructorError(u8),
9+
#[error("Iterator was empty or too short.")]
10+
IteratorEmptyOrTooShortError,
911
}

amqp-type/src/fixed_width/boolean.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,8 @@ impl Decode for bool {
4141
match (con, val) {
4242
(Some(c), Some(v)) if c == 0x56 && v == 0x00 => Ok(false),
4343
(Some(c), Some(v)) if c == 0x56 && v == 0x01 => Ok(true),
44-
(Some(c), _) => Err(AppError::DeserializationError(
45-
"bool".to_string(),
46-
format!("bool cannot be constructed from value {:#04x}", c),
47-
)),
48-
(None, _) => Err(AppError::DeserializationError(
49-
"bool".to_string(),
50-
"Iterator was empty".to_string(),
51-
)),
44+
(Some(c), _) => Err(AppError::DeserializationIllegalConstructorError(c)),
45+
(None, _) => Err(AppError::IteratorEmptyOrTooShortError),
5246
}
5347
}
5448
}
@@ -72,16 +66,10 @@ impl Decode for bool {
7266
return match val {
7367
0x41 => Ok(true),
7468
0x42 => Ok(false),
75-
_ => Err(AppError::DeserializationError(
76-
"bool".to_string(),
77-
format!("bool cannot be constructed from value {:#04x}", val),
78-
)),
69+
_ => Err(AppError::DeserializationIllegalConstructorError(val)),
7970
};
8071
}
81-
Err(AppError::DeserializationError(
82-
"bool".to_string(),
83-
"Iterator was empty".to_string(),
84-
))
72+
Err(AppError::IteratorEmptyOrTooShortError)
8573
}
8674
}
8775

amqp-type/src/fixed_width/ubyte.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::error::AppError;
2-
use crate::serde::encode::{Encode, Encoded};
32
use crate::serde::decode::Decode;
4-
3+
use crate::serde::encode::{Encode, Encoded};
54

65
impl Encode for u8 {
76
fn encode(&self) -> Encoded {
@@ -14,28 +13,27 @@ impl Decode for u8 {
1413
let mut iter = data.into_iter().peekable();
1514
match iter.peek() {
1615
Some(0x50) => true,
17-
_ => false
16+
_ => false,
1817
}
1918
}
2019

2120
fn try_decode(mut iter: impl Iterator<Item = u8>) -> Result<Self, crate::error::AppError>
22-
where
23-
Self: Sized {
21+
where
22+
Self: Sized,
23+
{
2424
let con = iter.next();
2525
let val = iter.next();
2626
match (con, val) {
2727
(Some(0x50), Some(x)) => Ok(x),
28-
(Some(_), _) => Err(AppError::DeserializationError("ubyte (u8)".to_string(), "Wrong constructor".to_string())),
29-
(_, _) => Err(AppError::DeserializationError("ubyte (u8)".to_string(), "Iterator was empty".to_string()))
28+
(Some(c), _) => Err(AppError::DeserializationIllegalConstructorError(c)),
29+
(_, _) => Err(AppError::IteratorEmptyOrTooShortError),
3030
}
3131
}
32-
3332
}
3433

3534
#[cfg(test)]
3635
mod test {
3736

38-
3937
use super::*;
4038

4139
#[test]
@@ -61,5 +59,4 @@ mod test {
6159
let val = vec![0x50, 0x10];
6260
assert_eq!(u8::try_decode(val.into_iter()).unwrap(), 16)
6361
}
64-
6562
}

amqp-type/src/fixed_width/ushort.rs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::serde::encode::{Encode, Encoded};
1+
use crate::error::AppError;
22
use crate::serde::decode::Decode;
3-
4-
const BYTE_LEN: usize = 2;
3+
use crate::serde::encode::{Encode, Encoded};
4+
use crate::verify::verify_bytes_read_eq;
55

66
impl Encode for u16 {
77
fn encode(&self) -> Encoded {
@@ -13,24 +13,32 @@ impl Decode for u16 {
1313
fn can_decode(iter: impl Iterator<Item = u8>) -> bool {
1414
match iter.peekable().peek() {
1515
Some(0x60) => true,
16-
_ => false
16+
_ => false,
1717
}
1818
}
1919

2020
fn try_decode(mut iter: impl Iterator<Item = u8>) -> Result<Self, crate::error::AppError>
21-
where
22-
Self: Sized {
23-
let mut val_bytes = [0; BYTE_LEN];
24-
let con = iter.next();
25-
let mut index = 0;
26-
for b in iter.take(BYTE_LEN) {
27-
val_bytes[index] = b;
28-
index += 1;
21+
where
22+
Self: Sized,
23+
{
24+
match iter.next() {
25+
Some(0x60) => Ok(parse_u16(iter)?),
26+
Some(c) => Err(AppError::DeserializationIllegalConstructorError(c)),
27+
None => Err(AppError::IteratorEmptyOrTooShortError),
2928
}
30-
Ok(u16::from_be_bytes(val_bytes))
3129
}
3230
}
3331

32+
fn parse_u16(iter: impl Iterator<Item = u8>) -> Result<u16, AppError> {
33+
let mut val_bytes = [0; 2];
34+
let mut index = 0;
35+
for b in iter.take(2) {
36+
val_bytes[index] = b;
37+
index += 1;
38+
}
39+
verify_bytes_read_eq(index, 2)?;
40+
Ok(u16::from_be_bytes(val_bytes))
41+
}
3442

3543
#[cfg(test)]
3644
mod test {
@@ -43,7 +51,6 @@ mod test {
4351
assert_eq!(val.encode().constructor(), 0x60);
4452
}
4553

46-
4754
#[test]
4855
fn can_deocde_returns_true_if_constructor_is_valid() {
4956
let val = vec![0x60, 0x41];
@@ -61,4 +68,16 @@ mod test {
6168
let val = vec![0x60, 0x00, 0x10];
6269
assert_eq!(u16::try_decode(val.into_iter()).unwrap(), 16)
6370
}
71+
72+
#[test]
73+
fn decode_returns_error_when_value_bytes_are_invalid() {
74+
let val = vec![0x56, 0x44];
75+
assert!(u16::try_decode(val.into_iter()).is_err());
76+
}
77+
78+
#[test]
79+
fn decode_returns_error_when_bytes_are_missing() {
80+
let val = vec![0x60, 0x01];
81+
assert!(u16::try_decode(val.into_iter()).is_err());
82+
}
6483
}

amqp-type/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ mod error;
55
mod fixed_width;
66
mod serde;
77
mod variable_width;
8+
mod verify;

amqp-type/src/serde/decode.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::error::AppError;
22

3-
43
#[derive(Hash, Eq, PartialEq)]
54
pub struct Constructor(u8);
65

amqp-type/src/verify.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use crate::error::AppError;
2+
3+
4+
5+
pub fn verify_bytes_read_eq(actual: usize, expected: usize) -> Result<(), AppError> {
6+
if actual == expected {
7+
Ok(())
8+
} else {
9+
Err(AppError::IteratorEmptyOrTooShortError)
10+
}
11+
}

0 commit comments

Comments
 (0)