use crate::bytes;
use crate::error::BdError;
pub mod chapters;
pub mod clpi;
pub mod disc;
pub mod interleaved;
pub mod m2ts;
pub mod mpls;
pub mod order;
pub(crate) fn byte(buf: &[u8], off: usize) -> Result<u8, BdError> {
bytes::read_u8(buf, off).ok_or(BdError::UnexpectedEof)
}
pub(crate) fn u16_be(buf: &[u8], off: usize) -> Result<u16, BdError> {
bytes::read_u16_be(buf, off).ok_or(BdError::UnexpectedEof)
}
pub(crate) fn u32_be(buf: &[u8], off: usize) -> Result<u32, BdError> {
bytes::read_u32_be(buf, off).ok_or(BdError::UnexpectedEof)
}
pub(crate) fn u32_off(buf: &[u8], off: usize) -> Result<usize, BdError> {
let value = bytes::read_u32_be(buf, off).ok_or(BdError::UnexpectedEof)?;
Ok(usize::try_from(value).unwrap_or(usize::MAX))
}
pub(crate) fn ascii(buf: &[u8], off: usize, count: usize) -> Result<String, BdError> {
bytes::read_ascii(buf, off, count).ok_or(BdError::UnexpectedEof)
}
#[cfg(test)]
mod tests {
use super::{ascii, byte, u16_be, u32_be, u32_off};
#[test]
fn helpers_read_in_bounds_values() {
let buf = [0x12_u8, 0x34, 0x56, 0x78, b'e', b'n', b'g'];
assert_eq!(byte(&buf, 0).ok(), Some(0x12));
assert_eq!(u16_be(&buf, 0).ok(), Some(0x1234));
assert_eq!(u32_be(&buf, 0).ok(), Some(0x1234_5678));
assert_eq!(u32_off(&buf, 0).ok(), Some(0x1234_5678));
assert_eq!(ascii(&buf, 4, 3).ok().as_deref(), Some("eng"));
}
#[test]
fn helpers_map_short_reads_to_eof() {
let buf = [0x12_u8, 0x34];
let eof = "unexpected end of input";
assert_eq!(byte(&buf, 2).unwrap_err().to_string(), eof);
assert_eq!(u16_be(&buf, 1).unwrap_err().to_string(), eof);
assert_eq!(u32_be(&buf, 0).unwrap_err().to_string(), eof);
assert_eq!(u32_off(&buf, 0).unwrap_err().to_string(), eof);
assert_eq!(ascii(&buf, 1, 3).unwrap_err().to_string(), eof);
}
}