use crate::error::{Error, Result};
pub(crate) const MAGIC: u32 = 0x05E1_2A84;
pub(crate) fn check_magic(bytes: &[u8]) -> Result<()> {
let Some(&[a, b, c, d]) = bytes.get(..4) else {
return Err(Error::UnsupportedWeights { tag: 0 });
};
let tag = u32::from_le_bytes([a, b, c, d]);
if tag == MAGIC {
Ok(())
} else {
Err(Error::UnsupportedWeights { tag })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_magic_tag_is_accepted() {
assert!(check_magic(&[0x84, 0x2A, 0xE1, 0x05]).is_ok());
assert!(check_magic(&[0x84, 0x2A, 0xE1, 0x05, 0xFF]).is_ok());
}
#[test]
fn another_tag_is_reported() {
let error = check_magic(&[0x83, 0x2A, 0xE1, 0x05]).expect_err("a Needle 2 tag");
assert!(matches!(
error,
Error::UnsupportedWeights { tag: 0x05E1_2A83 }
));
}
#[test]
fn a_short_header_reports_tag_zero() {
let error = check_magic(&[0x84, 0x2A, 0xE1]).expect_err("three bytes");
assert!(matches!(error, Error::UnsupportedWeights { tag: 0 }));
}
}