use crate::{Error, Result};
pub(crate) const OPEN_COMPOSITE_SENTINEL: u32 = 0xFFFF_FFFF;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CombineOp {
Replace,
Add,
}
impl CombineOp {
#[inline]
pub fn wire_byte(self) -> u8 {
match self {
Self::Replace => 0x01,
Self::Add => 0x02,
}
}
#[inline]
pub fn from_wire(byte: u8) -> Option<Self> {
match byte {
0x01 => Some(Self::Replace),
0x02 => Some(Self::Add),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CompositionRule {
Partition,
Overlay(CombineOp),
Group,
}
impl CompositionRule {
#[inline]
pub fn rule_byte(&self) -> u8 {
match self {
Self::Partition => 0x01,
Self::Overlay(_) => 0x02,
Self::Group => 0x03,
}
}
#[inline]
pub fn combine_op_byte(&self) -> u8 {
match self {
Self::Overlay(op) => op.wire_byte(),
Self::Partition | Self::Group => 0x00,
}
}
pub fn from_wire(rule: u8, combine_op: u8) -> Result<Self> {
match rule {
0xFF => return Err(Error::InvalidCompositionRule(rule)),
0x00 | 0x04..=0xEF => return Err(Error::ReservedCompositionRule(rule)),
0xF0..=0xFE => return Err(Error::PrivateCompositionRule(rule)),
0x01..=0x03 => {}
}
match (rule, combine_op) {
(0x01, 0x00) => Ok(Self::Partition),
(0x03, 0x00) => Ok(Self::Group),
(0x02, 0x01) => Ok(Self::Overlay(CombineOp::Replace)),
(0x02, 0x02) => Ok(Self::Overlay(CombineOp::Add)),
_ => Err(Error::InvalidCombineOp { rule, combine_op }),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct CompositeLayout {
pub rule: CompositionRule,
pub member_count: u32,
}
impl CompositeLayout {
pub fn new(rule: CompositionRule, member_count: u32) -> Result<Self> {
if member_count == OPEN_COMPOSITE_SENTINEL {
return Err(Error::OpenCompositeReserved);
}
Ok(Self { rule, member_count })
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::layout::LayoutDescriptor;
use crate::shape::DYNAMIC;
use crate::Shape;
#[test]
fn combine_op_wire_round_trip() {
for op in [CombineOp::Replace, CombineOp::Add] {
let byte = op.wire_byte();
assert_eq!(CombineOp::from_wire(byte), Some(op));
}
}
#[test]
fn combine_op_wire_bytes_match_spec() {
assert_eq!(CombineOp::Replace.wire_byte(), 0x01);
assert_eq!(CombineOp::Add.wire_byte(), 0x02);
}
#[test]
fn combine_op_from_wire_rejects_invalid_bytes() {
for byte in [0x00_u8, 0x03, 0xFF] {
assert!(
CombineOp::from_wire(byte).is_none(),
"0x{byte:02X} should not decode to a CombineOp"
);
}
}
#[test]
fn composition_rule_round_trip_partition() {
let rule = CompositionRule::from_wire(0x01, 0x00).unwrap();
assert_eq!(rule, CompositionRule::Partition);
assert_eq!(rule.rule_byte(), 0x01);
assert_eq!(rule.combine_op_byte(), 0x00);
}
#[test]
fn composition_rule_round_trip_group() {
let rule = CompositionRule::from_wire(0x03, 0x00).unwrap();
assert_eq!(rule, CompositionRule::Group);
assert_eq!(rule.rule_byte(), 0x03);
assert_eq!(rule.combine_op_byte(), 0x00);
}
#[test]
fn composition_rule_round_trip_overlay_replace() {
let rule = CompositionRule::from_wire(0x02, 0x01).unwrap();
assert_eq!(rule, CompositionRule::Overlay(CombineOp::Replace));
assert_eq!(rule.rule_byte(), 0x02);
assert_eq!(rule.combine_op_byte(), 0x01);
}
#[test]
fn composition_rule_round_trip_overlay_add() {
let rule = CompositionRule::from_wire(0x02, 0x02).unwrap();
assert_eq!(rule, CompositionRule::Overlay(CombineOp::Add));
assert_eq!(rule.rule_byte(), 0x02);
assert_eq!(rule.combine_op_byte(), 0x02);
}
#[test]
fn composition_rule_rejects_reserved_zero() {
assert!(matches!(
CompositionRule::from_wire(0x00, 0x00),
Err(Error::ReservedCompositionRule(0x00))
));
}
#[test]
fn composition_rule_rejects_invalid_0xff() {
assert!(matches!(
CompositionRule::from_wire(0xFF, 0x00),
Err(Error::InvalidCompositionRule(0xFF))
));
}
#[test]
fn composition_rule_rejects_reserved_range() {
for rule in [0x04_u8, 0x50, 0xEF] {
assert!(
matches!(
CompositionRule::from_wire(rule, 0x00),
Err(Error::ReservedCompositionRule(_))
),
"0x{rule:02X} should be ReservedCompositionRule"
);
}
}
#[test]
fn composition_rule_rejects_private_range() {
for rule in [0xF0_u8, 0xF7, 0xFE] {
assert!(
matches!(
CompositionRule::from_wire(rule, 0x00),
Err(Error::PrivateCompositionRule(_))
),
"0x{rule:02X} should be PrivateCompositionRule"
);
}
}
#[test]
fn composition_rule_rejects_nonzero_combine_op_for_partition() {
assert!(matches!(
CompositionRule::from_wire(0x01, 0x01),
Err(Error::InvalidCombineOp {
rule: 0x01,
combine_op: 0x01
})
));
}
#[test]
fn composition_rule_rejects_nonzero_combine_op_for_group() {
assert!(matches!(
CompositionRule::from_wire(0x03, 0x02),
Err(Error::InvalidCombineOp {
rule: 0x03,
combine_op: 0x02
})
));
}
#[test]
fn composition_rule_rejects_combine_op_outside_replace_add_for_overlay() {
for combine_op in [0x00_u8, 0x03, 0xFF] {
assert!(
matches!(
CompositionRule::from_wire(0x02, combine_op),
Err(Error::InvalidCombineOp { rule: 0x02, .. })
),
"combine_op 0x{combine_op:02X} should be rejected for overlay"
);
}
}
#[test]
fn composite_layout_new_rejects_open_composite_sentinel() {
assert!(matches!(
CompositeLayout::new(CompositionRule::Partition, OPEN_COMPOSITE_SENTINEL),
Err(Error::OpenCompositeReserved)
));
}
#[test]
fn composite_layout_new_accepts_definite_counts() {
let layout = CompositeLayout::new(CompositionRule::Group, 0).unwrap();
assert_eq!(layout.member_count, 0);
let layout =
CompositeLayout::new(CompositionRule::Partition, OPEN_COMPOSITE_SENTINEL - 1).unwrap();
assert_eq!(layout.member_count, OPEN_COMPOSITE_SENTINEL - 1);
}
#[test]
fn composite_layout_descriptor_tag_is_0x0b() {
let layout =
LayoutDescriptor::Composite(CompositeLayout::new(CompositionRule::Group, 0).unwrap());
assert_eq!(layout.tag(), 0x0B);
}
#[test]
fn composite_layout_descriptor_buffer_count_is_none() {
let layout = LayoutDescriptor::Composite(
CompositeLayout::new(CompositionRule::Partition, 2).unwrap(),
);
assert!(layout.buffer_count().is_none());
}
#[test]
fn composite_layout_descriptor_is_virtual() {
let composite =
LayoutDescriptor::Composite(CompositeLayout::new(CompositionRule::Group, 0).unwrap());
assert!(composite.is_virtual());
assert!(!LayoutDescriptor::RowMajor.is_virtual());
}
#[test]
fn composite_tag_is_not_reserved() {
assert!(!crate::layout::is_reserved_tag(0x0B));
}
#[test]
fn composite_tag_passes_strict_validation() {
assert!(crate::layout::validate_layout_tag_strict(0x0B).is_ok());
}
#[test]
fn composite_element_offset_returns_layout_is_virtual() {
let layout =
LayoutDescriptor::Composite(CompositeLayout::new(CompositionRule::Group, 0).unwrap());
let shape = Shape::new(vec![4u64]).unwrap();
let err = layout.element_offset(&[0], &shape).unwrap_err();
assert!(matches!(err, Error::LayoutIsVirtual { layout_tag: 0x0B }));
}
#[test]
fn composite_validate_against_shape_rejects_dynamic_for_partition() {
let layout = LayoutDescriptor::Composite(
CompositeLayout::new(CompositionRule::Partition, 0).unwrap(),
);
let shape = Shape::new(vec![DYNAMIC, 4]).unwrap();
assert!(matches!(
layout.validate_against_shape(&shape),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn composite_validate_against_shape_rejects_dynamic_for_overlay() {
let layout = LayoutDescriptor::Composite(
CompositeLayout::new(CompositionRule::Overlay(CombineOp::Replace), 0).unwrap(),
);
let shape = Shape::new(vec![DYNAMIC]).unwrap();
assert!(matches!(
layout.validate_against_shape(&shape),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn composite_validate_against_shape_accepts_dynamic_for_group() {
let layout =
LayoutDescriptor::Composite(CompositeLayout::new(CompositionRule::Group, 0).unwrap());
let shape = Shape::new(vec![DYNAMIC]).unwrap();
assert!(layout.validate_against_shape(&shape).is_ok());
}
#[test]
fn composite_validate_against_shape_accepts_static_shape_for_partition() {
let layout = LayoutDescriptor::Composite(
CompositeLayout::new(CompositionRule::Partition, 0).unwrap(),
);
let shape = Shape::new(vec![4u64, 4]).unwrap();
assert!(layout.validate_against_shape(&shape).is_ok());
}
}