Skip to main content

facet_msgpack/
error.rs

1//! Error types for MsgPack Tier-2 JIT parsing.
2
3use core::fmt;
4
5/// MsgPack parsing error.
6#[derive(Debug, Clone)]
7#[non_exhaustive]
8pub struct MsgPackError {
9    /// Error code from JIT
10    pub code: i32,
11    /// Position in input where error occurred
12    pub pos: usize,
13    /// Human-readable message
14    pub message: String,
15}
16
17impl fmt::Display for MsgPackError {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        write!(f, "{} at position {}", self.message, self.pos)
20    }
21}
22
23impl std::error::Error for MsgPackError {}
24
25/// MsgPack JIT error codes.
26pub mod codes {
27    /// Unexpected end of input
28    pub const UNEXPECTED_EOF: i32 = -100;
29    /// Invalid type tag for expected bool
30    pub const EXPECTED_BOOL: i32 = -101;
31    /// Invalid type tag for expected array
32    pub const EXPECTED_ARRAY: i32 = -102;
33    /// Invalid type tag for expected bin/bytes
34    pub const EXPECTED_BIN: i32 = -103;
35    /// Invalid type tag for expected integer
36    pub const EXPECTED_INT: i32 = -104;
37    /// Integer value doesn't fit in target type
38    pub const INT_OVERFLOW: i32 = -105;
39    /// Array/bin count doesn't fit in usize
40    pub const COUNT_OVERFLOW: i32 = -106;
41    /// Sequence underflow (decrement when remaining is 0)
42    pub const SEQ_UNDERFLOW: i32 = -107;
43    /// Unsupported operation (triggers fallback)
44    pub const UNSUPPORTED: i32 = -1;
45}
46
47impl MsgPackError {
48    /// Create an error from a JIT error code and position.
49    pub fn from_code(code: i32, pos: usize) -> Self {
50        let message = match code {
51            codes::UNEXPECTED_EOF => "unexpected end of input".to_string(),
52            codes::EXPECTED_BOOL => "expected bool (0xC2 or 0xC3)".to_string(),
53            codes::EXPECTED_ARRAY => "expected array tag (fixarray/array16/array32)".to_string(),
54            codes::EXPECTED_BIN => "expected bin tag (bin8/bin16/bin32)".to_string(),
55            codes::EXPECTED_INT => "expected integer tag".to_string(),
56            codes::INT_OVERFLOW => "integer value overflows target type".to_string(),
57            codes::COUNT_OVERFLOW => "count too large for platform".to_string(),
58            codes::SEQ_UNDERFLOW => "sequence underflow (internal error)".to_string(),
59            codes::UNSUPPORTED => "unsupported operation".to_string(),
60            _ => format!("unknown error code {}", code),
61        };
62        Self { code, pos, message }
63    }
64}