Skip to main content

irithyll_core/
error.rs

1//! Minimal error types for the packed binary format.
2//!
3//! No `thiserror`, no `alloc` — just `core::fmt::Display` impls.
4
5/// Errors that can occur when parsing or validating a packed ensemble binary.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub enum FormatError {
8    /// Magic bytes do not match `"IRIT"` (`0x54495249` LE).
9    BadMagic,
10    /// Format version is not supported by this build.
11    UnsupportedVersion,
12    /// Input buffer is too short to contain the declared structures.
13    Truncated,
14    /// Input buffer pointer is not aligned to 4 bytes.
15    Unaligned,
16    /// A node's child index points outside the node array bounds.
17    InvalidNodeIndex,
18    /// A node references a feature index >= `n_features`.
19    InvalidFeatureIndex,
20    /// A tree entry's byte offset is not aligned to `size_of::<PackedNode>()`.
21    MisalignedTreeOffset,
22}
23
24impl core::fmt::Display for FormatError {
25    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
26        match self {
27            FormatError::BadMagic => write!(f, "bad magic: expected \"IRIT\""),
28            FormatError::UnsupportedVersion => write!(f, "unsupported format version"),
29            FormatError::Truncated => write!(f, "buffer truncated"),
30            FormatError::Unaligned => write!(f, "buffer not 4-byte aligned"),
31            FormatError::InvalidNodeIndex => write!(f, "node child index out of bounds"),
32            FormatError::InvalidFeatureIndex => write!(f, "feature index exceeds n_features"),
33            FormatError::MisalignedTreeOffset => {
34                write!(f, "tree offset not aligned to node size")
35            }
36        }
37    }
38}