use std::io::{Cursor, Read};
use crate::{DecodeError, MAX_ARRAY_LENGTH};
pub trait Decode {
fn decode(reader: &mut impl Read) -> Result<Self, DecodeError>
where
Self: Sized;
fn decode_from_slice(buffer: &[u8]) -> Result<Self, DecodeError>
where
Self: Sized,
{
let mut cursor = Cursor::new(buffer);
Self::decode(&mut cursor)
}
}
impl Decode for u8 {
fn decode(reader: &mut impl Read) -> Result<Self, DecodeError> {
let mut buffer = [0u8; 1];
reader.read_exact(&mut buffer)?;
Ok(buffer[0])
}
}
impl Decode for bool {
fn decode(reader: &mut impl Read) -> Result<Self, DecodeError> {
let value = u8::decode(reader)?;
Ok(value != 0)
}
}
impl Decode for u32 {
fn decode(reader: &mut impl Read) -> Result<Self, DecodeError> {
let mut buffer = [0u8; 4];
reader.read_exact(&mut buffer)?;
Ok(u32::from_be_bytes(buffer))
}
}
impl Decode for usize {
fn decode(reader: &mut impl Read) -> Result<Self, DecodeError> {
let size = u32::decode(reader)?;
size.try_into()
.map_err(|_| DecodeError::OutsideUsizeRange(size as u64))
}
}
impl<const N: usize> Decode for [u8; N] {
fn decode(reader: &mut impl Read) -> Result<Self, DecodeError> {
let mut buffer = [0u8; N];
reader.read_exact(&mut buffer)?;
Ok(buffer)
}
}
impl<const N: usize> Decode for Box<[u8; N]> {
fn decode(reader: &mut impl Read) -> Result<Self, DecodeError> {
let mut buffer = Box::new([0u8; N]);
reader.read_exact(buffer.as_mut_slice())?;
Ok(buffer)
}
}
impl<T: Decode> Decode for Vec<T> {
fn decode(reader: &mut impl Read) -> Result<Self, DecodeError> {
let length = usize::decode(reader)?;
if length > MAX_ARRAY_LENGTH {
Err(DecodeError::ArrayTooBig(length))
} else {
let mut buffer = Vec::with_capacity(length);
for _ in 0..length {
let element = T::decode(reader)?;
buffer.push(element);
}
Ok(buffer)
}
}
}