Skip to main content

wacore_binary/
error.rs

1use std::fmt;
2
3use crate::jid::JidError;
4
5#[derive(Debug)]
6pub enum BinaryError {
7    Io(std::io::Error),
8    InvalidToken(u8),
9    InvalidNode,
10    NonStringKey,
11    AttrParse(String),
12    MissingAttr(String),
13    InvalidUtf8(std::str::Utf8Error),
14    Zlib(String),
15    Jid(JidError),
16    UnexpectedEof,
17    EmptyData,
18    LeftoverData(usize),
19    AttrList(Vec<BinaryError>),
20    /// Node nesting exceeded the recursion cap — a hostile frame trying to
21    /// overflow the native stack, rejected before recursing.
22    MaxDepthExceeded,
23    /// The exact-marshal encode pass diverged from its size plan (byte count
24    /// or hint-tape consumption) — an internal encoder bug, distinct from a
25    /// malformed input node, surfaced instead of shipping corrupt bytes.
26    PlanMismatch,
27    /// A packed payload did not start with an uncompressed format byte, so it
28    /// is not what `marshal` produces. Most often node bytes handed to a path
29    /// that expects the format byte in front of them.
30    UnexpectedFormatByte(u8),
31}
32
33impl fmt::Display for BinaryError {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        match self {
36            BinaryError::Io(e) => write!(f, "I/O error: {e}"),
37            BinaryError::InvalidToken(t) => write!(f, "Invalid token read from stream: {t}"),
38            BinaryError::InvalidNode => write!(f, "Invalid node format"),
39            BinaryError::PlanMismatch => {
40                write!(f, "Exact-marshal encode diverged from its size plan")
41            }
42            BinaryError::NonStringKey => write!(f, "Attribute key was not a string"),
43            BinaryError::AttrParse(s) => write!(f, "Attribute parsing failed: {s}"),
44            BinaryError::MissingAttr(s) => write!(f, "Missing required attribute: {s}"),
45            BinaryError::InvalidUtf8(e) => write!(f, "Data is not valid UTF-8: {e}"),
46            BinaryError::Zlib(s) => write!(f, "Zlib decompression error: {s}"),
47            BinaryError::Jid(e) => write!(f, "JID parsing error: {e}"),
48            BinaryError::UnexpectedEof => write!(f, "Unexpected end of binary data"),
49            BinaryError::EmptyData => write!(f, "Received empty data where payload was expected"),
50            BinaryError::LeftoverData(n) => write!(f, "Leftover data after decoding: {n} bytes"),
51            BinaryError::AttrList(list) => write!(f, "Multiple attribute parsing errors: {list:?}"),
52            BinaryError::MaxDepthExceeded => write!(f, "Node nesting exceeded the maximum depth"),
53            BinaryError::UnexpectedFormatByte(b) => write!(
54                f,
55                "Packed payload starts with format byte {b:#04x}, expected an uncompressed 0x00"
56            ),
57        }
58    }
59}
60
61impl std::error::Error for BinaryError {
62    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
63        match self {
64            BinaryError::Io(e) => Some(e),
65            BinaryError::InvalidUtf8(e) => Some(e),
66            BinaryError::Jid(e) => Some(e),
67            _ => None,
68        }
69    }
70}
71
72impl From<std::io::Error> for BinaryError {
73    fn from(err: std::io::Error) -> Self {
74        BinaryError::Io(err)
75    }
76}
77impl From<std::str::Utf8Error> for BinaryError {
78    fn from(err: std::str::Utf8Error) -> Self {
79        BinaryError::InvalidUtf8(err)
80    }
81}
82impl From<JidError> for BinaryError {
83    fn from(err: JidError) -> Self {
84        BinaryError::Jid(err)
85    }
86}
87impl Clone for BinaryError {
88    fn clone(&self) -> Self {
89        match self {
90            BinaryError::Io(e) => BinaryError::Io(std::io::Error::new(e.kind(), e.to_string())),
91            BinaryError::InvalidToken(u) => BinaryError::InvalidToken(*u),
92            BinaryError::InvalidNode => BinaryError::InvalidNode,
93            BinaryError::NonStringKey => BinaryError::NonStringKey,
94            BinaryError::AttrParse(s) => BinaryError::AttrParse(s.clone()),
95            BinaryError::MissingAttr(s) => BinaryError::MissingAttr(s.clone()),
96            BinaryError::InvalidUtf8(e) => BinaryError::InvalidUtf8(*e),
97            BinaryError::Zlib(s) => BinaryError::Zlib(s.clone()),
98            BinaryError::Jid(e) => BinaryError::Jid(JidError::InvalidFormat(e.to_string())),
99            BinaryError::UnexpectedEof => BinaryError::UnexpectedEof,
100            BinaryError::EmptyData => BinaryError::EmptyData,
101            BinaryError::LeftoverData(n) => BinaryError::LeftoverData(*n),
102            BinaryError::AttrList(list) => BinaryError::AttrList(list.clone()),
103            BinaryError::MaxDepthExceeded => BinaryError::MaxDepthExceeded,
104            BinaryError::PlanMismatch => BinaryError::PlanMismatch,
105            BinaryError::UnexpectedFormatByte(b) => BinaryError::UnexpectedFormatByte(*b),
106        }
107    }
108}
109
110pub type Result<T> = std::result::Result<T, BinaryError>;