use crate::error::{Error, Result};
pub(crate) const U16_LEN: usize = core::mem::size_of::<u16>();
pub(crate) const U32_LEN: usize = core::mem::size_of::<u32>();
pub(crate) fn be_u16(bytes: &[u8], off: usize, what: &'static str) -> Result<u16> {
match bytes.get(off..).and_then(|s| s.first_chunk::<U16_LEN>()) {
Some(b) => Ok(u16::from_be_bytes(*b)),
None => Err(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(
off.saturating_add(U16_LEN),
bytes.len(),
what,
),
)),
}
}
pub(crate) fn be_u32(bytes: &[u8], off: usize, what: &'static str) -> Result<u32> {
match bytes.get(off..).and_then(|s| s.first_chunk::<U32_LEN>()) {
Some(b) => Ok(u32::from_be_bytes(*b)),
None => Err(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(
off.saturating_add(U32_LEN),
bytes.len(),
what,
),
)),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exact_ok() {
assert_eq!(be_u16(&[1, 2], 0, "t").unwrap(), 0x0102);
assert_eq!(be_u16(&[9, 1, 2], 1, "t").unwrap(), 0x0102);
assert_eq!(be_u32(&[1, 2, 3, 4], 0, "t").unwrap(), 0x0102_0304);
}
#[test]
fn short_is_err_not_panic() {
assert!(matches!(
be_u16(&[1], 0, "t"),
Err(Error::BufferTooShort(short))
if short == broadcast_core::error::BufferTooShort::new(2, 1, "t")
));
assert!(matches!(
&(be_u16(&[1, 2], 1, "t")),
Err(crate::error::Error::BufferTooShort(..))
));
assert!(matches!(
&(be_u16(&[1, 2], 9, "t")),
Err(crate::error::Error::BufferTooShort(..))
));
assert!(matches!(
&(be_u16(&[], usize::MAX, "t")),
Err(crate::error::Error::BufferTooShort(..))
));
assert!(matches!(
&(be_u32(&[1, 2, 3], 0, "t")),
Err(crate::error::Error::BufferTooShort(..))
));
}
}