use std::collections::HashSet;
use std::fmt;
use crate::bytes::{In, Out, fits_u32};
use crate::container::ChunkTag;
use crate::scene::Scene;
pub const ROLE_TAG: ChunkTag = ChunkTag(*b"ROLE");
pub const ROLE_VERSION: u16 = 1;
pub const ROLE_SUPPORTS: u8 = 1;
pub const ROLE_RAFT: u8 = 2;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct RoleEntry {
pub leaf: usize,
pub role: u8,
}
impl RoleEntry {
pub fn is_known(&self) -> bool {
matches!(self.role, ROLE_SUPPORTS | ROLE_RAFT)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EncodeRolesError {
NotALeaf,
SameLeaf,
ReservedRole,
TooLarge,
}
impl fmt::Display for EncodeRolesError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotALeaf => f.write_str(NOT_A_LEAF),
Self::SameLeaf => f.write_str(SAME_LEAF),
Self::ReservedRole => f.write_str("a role entry has a reserved role"),
Self::TooLarge => f.write_str("the roles are too many for their chunk"),
}
}
}
impl std::error::Error for EncodeRolesError {}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DecodeRolesError {
Malformed,
NotALeaf,
SameLeaf,
}
impl fmt::Display for DecodeRolesError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Malformed => f.write_str("the role data is malformed"),
Self::NotALeaf => f.write_str(NOT_A_LEAF),
Self::SameLeaf => f.write_str(SAME_LEAF),
}
}
}
impl std::error::Error for DecodeRolesError {}
const NOT_A_LEAF: &str = "a role names a scene part that is not a leaf";
const SAME_LEAF: &str = "two role entries name the same leaf";
enum Broken {
NotALeaf,
SameLeaf,
}
impl From<Broken> for EncodeRolesError {
fn from(broken: Broken) -> Self {
match broken {
Broken::NotALeaf => Self::NotALeaf,
Broken::SameLeaf => Self::SameLeaf,
}
}
}
impl From<Broken> for DecodeRolesError {
fn from(broken: Broken) -> Self {
match broken {
Broken::NotALeaf => Self::NotALeaf,
Broken::SameLeaf => Self::SameLeaf,
}
}
}
pub fn encode_roles(entries: &[RoleEntry], scene: &Scene) -> Result<Vec<u8>, EncodeRolesError> {
check(entries, scene)?;
if entries.iter().any(|entry| !entry.is_known()) {
return Err(EncodeRolesError::ReservedRole);
}
if !fits_u32(entries.len()) {
return Err(EncodeRolesError::TooLarge);
}
let mut out = Out::default();
out.len(entries.len());
for entry in entries {
out.len(entry.leaf);
out.u8(entry.role);
}
Ok(out.0)
}
pub fn decode_roles(payload: &[u8], scene: &Scene) -> Result<Vec<RoleEntry>, DecodeRolesError> {
let mut input = In(payload);
let malformed = DecodeRolesError::Malformed;
let count = input.count(5).ok_or(malformed.clone())?;
let mut entries = Vec::with_capacity(count);
for _ in 0..count {
let leaf = input.len().ok_or(malformed.clone())?;
let role = input.u8().ok_or(malformed.clone())?;
entries.push(RoleEntry { leaf, role });
}
if !input.is_empty() {
return Err(malformed);
}
check(&entries, scene)?;
Ok(entries)
}
fn check(entries: &[RoleEntry], scene: &Scene) -> Result<(), Broken> {
let mut seen = HashSet::new();
for entry in entries {
scene.leaf_mesh(entry.leaf).ok_or(Broken::NotALeaf)?;
if !seen.insert(entry.leaf) {
return Err(Broken::SameLeaf);
}
}
Ok(())
}