indexbus_msg/
errors.rs

1/// Errors returned by v1 envelope encode/decode helpers.
2///
3/// These errors reflect structural problems with the envelope (magic bytes, lengths, and flag
4/// bits). They do not indicate anything about the correctness or meaning of the payload bytes
5/// beyond the declared length.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum Error {
8    /// Output buffer is smaller than required.
9    BufferTooSmall {
10        /// Minimum number of bytes required.
11        required: usize,
12        /// Number of bytes provided by the caller.
13        provided: usize,
14    },
15    /// Magic bytes did not match the expected v1 envelope prefix.
16    BadMagic,
17    /// Header length is invalid or unsupported.
18    BadHeaderLen {
19        /// Decoded/declared header length in bytes.
20        header_len: usize,
21    },
22    /// Payload length is invalid (e.g. would overflow slot capacity).
23    BadPayloadLen {
24        /// Decoded/declared payload length in bytes.
25        payload_len: usize,
26    },
27    /// Input did not contain enough bytes to decode a full header/payload.
28    Truncated,
29    /// Header contains unknown/unsupported flag bits.
30    UnknownFlags {
31        /// Flag bits that were not recognized.
32        flags: u16,
33    },
34}
35
36#[cfg(feature = "std")]
37impl std::fmt::Display for Error {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        match self {
40            Self::BufferTooSmall { required, provided } => {
41                write!(
42                    f,
43                    "buffer too small: required {required}, provided {provided}"
44                )
45            }
46            Self::BadMagic => write!(f, "bad magic"),
47            Self::BadHeaderLen { header_len } => write!(f, "bad header_len: {header_len}"),
48            Self::BadPayloadLen { payload_len } => write!(f, "bad payload_len: {payload_len}"),
49            Self::Truncated => write!(f, "truncated input"),
50            Self::UnknownFlags { flags } => write!(f, "unknown flags: 0x{flags:04x}"),
51        }
52    }
53}
54
55#[cfg(feature = "std")]
56impl std::error::Error for Error {}