use crate::buffer::InputSource;
use crate::decoder::Decoder;
use crate::Result;
pub trait DecodeFrom: Sized {
fn decode_from(decoder: &mut Decoder<impl InputSource>) -> Result<Self>;
}
#[doc(hidden)]
#[macro_export]
macro_rules! implement_decode_from_on_numeric_primitive_type {
($ty:ty, $doc_text:literal) => {
impl DecodeFrom for $ty {
#[doc = $doc_text]
fn decode_from(decoder: &mut Decoder<impl InputSource>) -> Result<Self> {
let bytes = decoder.read_bytes_exact()?;
Ok(Self::from_le_bytes(*bytes))
}
}
};
}
pub use implement_decode_from_on_numeric_primitive_type;
#[doc(hidden)]
#[macro_export]
macro_rules! decode_dictionary_entries {
($dictionary:ident, $decoder:ident, $length:ident) => {
for _ in 0..$length {
let key = $decoder.decode()?;
let value = $decoder.decode()?;
if $dictionary.insert(key, value).is_some() {
return Err(InvalidDataErrorKind::DuplicateDictionaryKey.into());
}
}
};
}
pub use decode_dictionary_entries;