#[macro_export]
macro_rules! magic_bytes {
($($(#[$attr:meta])* $vis:vis $name:ident($bytes:expr);)*) => {$(
$(#[$attr])*
$vis struct $name;
impl $crate::Encode<()> for $name {
fn encode<W>(&self, _ctx: (), writer: &mut W) -> Result<(), $crate::Error>
where
W: std::io::Write,
{
($bytes).encode((), writer)
}
}
impl $crate::Decode<()> for $name {
fn decode<R>(_ctx: (), reader: &mut R) -> Result<Self, $crate::Error>
where
R: std::io::Read,
{
let bytes: [u8; ($bytes).len()] = $crate::Decode::decode((), reader)?;
if &bytes != $bytes {
return Err($crate::Error::new(format!(
"magic bytes mismatch: expected {:x?}, got {:x?}",
$bytes, bytes,
)));
}
Ok(Self)
}
}
)*}
}