Skip to content

Commit 15fa1ff

Browse files
stared impl on decode for ubyte
1 parent 4ff7c72 commit 15fa1ff

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

amqp-type/src/fixed_width/boolean.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ impl Decode for bool {
4545
"bool".to_string(),
4646
format!("bool cannot be constructed from value {:#04x}", c),
4747
)),
48-
(Some(c), None) => Err(AppError::DeserializationError(
49-
"bool".to_string(),
50-
"Iterator was empty".to_string(),
51-
)),
5248
(None, _) => Err(AppError::DeserializationError(
5349
"bool".to_string(),
5450
"Iterator was empty".to_string(),

amqp-type/src/fixed_width/ubyte.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,58 @@
11
use crate::serde::encode::{Encode, Encoded};
2+
use crate::serde::decode::Decode;
3+
24

35
impl Encode for u8 {
46
fn encode(&self) -> Encoded {
57
Encoded::new_fixed(0x50, self.to_be_bytes().to_vec())
68
}
79
}
810

11+
impl Decode for u8 {
12+
fn can_decode(data: impl Iterator<Item = u8>) -> bool {
13+
let mut iter = data.into_iter().peekable();
14+
match iter.peek() {
15+
Some(0x50) => true,
16+
_ => false
17+
}
18+
}
19+
20+
fn try_decode(data: impl Iterator<Item = u8>) -> Result<Self, crate::error::AppError>
21+
where
22+
Self: Sized {
23+
todo!()
24+
}
25+
26+
}
27+
928
#[cfg(test)]
1029
mod test {
1130

31+
1232
use super::*;
1333

1434
#[test]
1535
fn construct_ubyte() {
1636
let val: u8 = 8;
1737
assert_eq!(val.encode().constructor(), 0x50);
1838
}
39+
40+
#[test]
41+
fn can_deocde_returns_true_if_constructor_is_valid() {
42+
let val = vec![0x50, 0x41];
43+
assert_eq!(u8::can_decode(val.into_iter()), true);
44+
}
45+
46+
#[test]
47+
fn can_decode_return_false_if_constructor_is_invalid() {
48+
let val = vec![0x51];
49+
assert_eq!(u8::can_decode(val.into_iter()), false);
50+
}
51+
52+
#[test]
53+
fn try_decode_returns_correct_value() {
54+
let val = vec![0x50, 0x10];
55+
assert_eq!(u8::try_decode(val.into_iter()).unwrap(), 16)
56+
}
57+
1958
}

amqp-type/src/serde/decode.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
use crate::{
2-
error::AppError,
3-
serde::encode::{Encode, Encoded},
4-
};
1+
use crate::error::AppError;
2+
53

64
#[derive(Hash, Eq, PartialEq)]
75
pub struct Constructor(u8);
86

97
pub trait Decode {
8+
fn can_decode(data: impl Iterator<Item = u8>) -> bool;
109
fn try_decode(data: impl Iterator<Item = u8>) -> Result<Self, AppError>
1110
where
1211
Self: Sized;
13-
fn can_decode(data: impl Iterator<Item = u8>) -> bool;
1412
}
1513

1614
impl From<u8> for Constructor {

0 commit comments

Comments
 (0)