use dvb_common::{Parse, Serialize};
#[derive(Debug, PartialEq, Eq)]
struct Toy {
tag: u8,
value: u16,
}
const LEN: usize = 3;
#[derive(Debug)]
#[allow(dead_code)] enum ToyError {
TooShort { need: usize, have: usize },
}
impl<'a> Parse<'a> for Toy {
type Error = ToyError;
fn parse(bytes: &'a [u8]) -> Result<Self, Self::Error> {
if bytes.len() < LEN {
return Err(ToyError::TooShort {
need: LEN,
have: bytes.len(),
});
}
Ok(Toy {
tag: bytes[0],
value: u16::from_be_bytes([bytes[1], bytes[2]]),
})
}
}
impl Serialize for Toy {
type Error = ToyError;
fn serialized_len(&self) -> usize {
LEN
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize, Self::Error> {
if buf.len() < LEN {
return Err(ToyError::TooShort {
need: LEN,
have: buf.len(),
});
}
buf[0] = self.tag;
buf[1..3].copy_from_slice(&self.value.to_be_bytes());
Ok(LEN)
}
}
fn main() {
let wire = [0x42, 0x12, 0x34];
let toy = Toy::parse(&wire).expect("parse");
println!("parsed: {toy:?}");
let back = toy.to_bytes();
println!("re-serialized: {back:02X?}");
assert_eq!(back, wire);
assert_eq!(Toy::parse(&back).unwrap(), toy);
println!("round-trip holds ✔");
}