use crate::{Error, Result};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct UnknownLayout {
pub tag: u8,
pub raw_bytes: Vec<u8>,
}
impl UnknownLayout {
pub fn new(tag: u8, raw_bytes: Vec<u8>) -> Result<Self> {
if super::is_invalid_tag(tag) {
return Err(Error::InvalidLayoutTag(tag));
}
if super::is_named_tag(tag) {
return Err(Error::NamedLayoutTag(tag));
}
if super::is_private_tag(tag) {
return Err(Error::PrivateLayoutTag(tag));
}
Ok(Self { tag, raw_bytes })
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{layout::LayoutDescriptor, Error};
#[test]
fn unknown_tag_passthrough() {
let layout = LayoutDescriptor::Unknown(UnknownLayout::new(0x0C, vec![]).unwrap());
assert_eq!(layout.tag(), 0x0C);
}
#[test]
fn unknown_buffer_count_is_none() {
let layout = LayoutDescriptor::Unknown(UnknownLayout::new(0x0C, vec![1, 2, 3]).unwrap());
assert!(layout.buffer_count().is_none());
}
#[test]
fn rejects_invalid_sentinel_0x00() {
assert!(matches!(
UnknownLayout::new(0x00, vec![]),
Err(Error::InvalidLayoutTag(0x00))
));
}
#[test]
fn rejects_invalid_sentinel_0xff() {
assert!(matches!(
UnknownLayout::new(0xFF, vec![]),
Err(Error::InvalidLayoutTag(0xFF))
));
}
#[test]
fn accepts_reserved_range_tag_in_permissive_mode() {
assert!(UnknownLayout::new(0x0C, vec![]).is_ok());
assert!(UnknownLayout::new(0x3F, vec![]).is_ok());
assert!(UnknownLayout::new(0xEF, vec![]).is_ok());
}
#[test]
fn rejects_every_named_tag() {
for tag in [
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x40,
] {
assert!(
matches!(UnknownLayout::new(tag, vec![]), Err(Error::NamedLayoutTag(t)) if t == tag),
"tag 0x{tag:02X} must be rejected as named"
);
}
}
#[test]
fn rejects_private_tags_in_favour_of_the_extension_type() {
assert!(matches!(
UnknownLayout::new(0xF0, vec![]),
Err(Error::PrivateLayoutTag(0xF0))
));
assert!(matches!(
UnknownLayout::new(0xFE, vec![]),
Err(Error::PrivateLayoutTag(0xFE))
));
}
}