Skip to main content

coip_format/
error.rs

1use alloc::string::String;
2use core::fmt;
3
4/// Errors returned while parsing or validating COI binary structures.
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub enum FormatError {
7    UnexpectedEof,
8    InvalidMagic,
9    UnsupportedVersion { major: u16, minor: u16 },
10    InvalidCrc { expected: u32, actual: u32 },
11    InvalidCodec(u8),
12    InvalidSectionKind(u16),
13    InvalidPType(u8),
14    InvalidIdWidth(u8),
15    InvalidRidMode(u8),
16    InvalidNullMode(u8),
17    InvalidEncMode(u8),
18    InvalidColumnTag(u8),
19    InvalidDictKind(u8),
20    InvalidValueTag(u8),
21    VarintOverflow,
22    InvalidUtf8,
23    InvalidAlignment { offset: u64 },
24    InvalidSchema(String),
25    OutOfBounds(&'static str),
26}
27
28impl fmt::Display for FormatError {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            Self::UnexpectedEof => write!(f, "unexpected end of COI buffer"),
32            Self::InvalidMagic => write!(f, "invalid COI magic bytes"),
33            Self::UnsupportedVersion { major, minor } => {
34                write!(f, "unsupported COI format version {major}.{minor}")
35            }
36            Self::InvalidCrc { expected, actual } => {
37                write!(
38                    f,
39                    "CRC32C mismatch: expected 0x{expected:08x}, got 0x{actual:08x}"
40                )
41            }
42            Self::InvalidCodec(c) => write!(f, "unknown codec id {c}"),
43            Self::InvalidSectionKind(k) => write!(f, "unknown section kind {k}"),
44            Self::InvalidPType(t) => write!(f, "unknown primitive type tag {t}"),
45            Self::InvalidIdWidth(w) => write!(f, "invalid dict id width {w}"),
46            Self::InvalidRidMode(m) => write!(f, "unknown rid encoding mode {m}"),
47            Self::InvalidNullMode(m) => write!(f, "unknown chunk null mode {m}"),
48            Self::InvalidEncMode(m) => write!(f, "unknown chunk encoding mode {m}"),
49            Self::InvalidColumnTag(t) => write!(f, "unknown schema column type tag {t}"),
50            Self::InvalidDictKind(k) => write!(f, "unknown dict kind {k}"),
51            Self::InvalidValueTag(t) => write!(f, "unknown dynamic value tag {t}"),
52            Self::VarintOverflow => write!(f, "varint overflow (> 64 bits)"),
53            Self::InvalidUtf8 => write!(f, "invalid UTF-8 string"),
54            Self::InvalidAlignment { offset } => {
55                write!(f, "section offset {offset} is not 8-byte aligned")
56            }
57            Self::InvalidSchema(msg) => write!(f, "invalid schema: {msg}"),
58            Self::OutOfBounds(ctx) => write!(f, "out of bounds: {ctx}"),
59        }
60    }
61}
62
63impl core::error::Error for FormatError {}