Skip to main content

matter_codec/
error.rs

1//! Error type for `matter-codec`.
2//!
3//! `Error` covers every failure mode of `TlvReader` and `TlvWriter`. Use the
4//! crate's [`Result`] alias for return types.
5
6use thiserror::Error;
7
8/// All errors `matter-codec` can produce.
9#[derive(Debug, Error)]
10#[non_exhaustive]
11pub enum Error {
12    /// The reader reached the end of the input before completing an element.
13    #[error("unexpected end of input")]
14    UnexpectedEof,
15
16    /// The reader encountered a tag-control byte that does not match any
17    /// defined tag form.
18    #[error("invalid tag control bits 0x{0:02x}")]
19    InvalidTagControl(u8),
20
21    /// The reader encountered an element-type code that is not defined by
22    /// the spec or is not yet supported by this crate.
23    #[error("invalid element type 0x{0:02x}")]
24    InvalidElementType(u8),
25
26    /// A UTF-8 string element contained an invalid byte sequence.
27    ///
28    /// Produced by `TlvReader::next` when reading a UTF-8 element whose
29    /// payload bytes fail `core::str::from_utf8` validation.
30    #[error("invalid UTF-8 in TLV string: {0}")]
31    InvalidUtf8(#[from] core::str::Utf8Error),
32
33    /// An integer payload could not be represented in the declared width.
34    #[error("integer value out of range for declared width")]
35    IntegerOutOfRange,
36
37    /// The writer was asked to emit into a buffer that has no room.
38    ///
39    /// Reserved for the fixed-buffer writer variant; the `Vec<u8>`-backed
40    /// writer used in phase 1 cannot trigger this.
41    #[error("output buffer too small")]
42    BufferTooSmall,
43
44    /// A length field would exceed `usize::MAX`.
45    #[error("length field overflows usize")]
46    LengthOverflow,
47
48    /// A bare end-of-container marker (`0x18`) was read at the top level,
49    /// with no container open.
50    #[error("unexpected end-of-container marker at top level")]
51    UnexpectedEndOfContainer,
52
53    /// End of input was reached while reading the children of a container,
54    /// before a closing end-of-container marker arrived.
55    #[error("container body truncated before end-of-container marker")]
56    UnclosedContainer,
57
58    /// A container was opened at a depth that exceeds the reader's nesting
59    /// limit (32 levels per the Matter spec recommendation).
60    #[error("container nesting exceeds depth limit")]
61    ContainerTooDeep,
62
63    /// An array child carried a non-anonymous tag. The Matter spec requires
64    /// every array element to be anonymous; the decoder fails closed rather
65    /// than silently discarding the offending tag.
66    #[error("array element carried a non-anonymous tag")]
67    NonAnonymousArrayTag,
68
69    /// A single tree-builder decode produced more elements than the
70    /// configured budget allows. This bounds memory amplification from
71    /// inputs packed with many tiny scalars.
72    #[error("decoded element count exceeds the per-decode budget")]
73    ElementBudgetExceeded,
74
75    /// An internal fixed-width byte-slice conversion failed. The preceding
76    /// bounds check guarantees the slice length, so this branch is
77    /// effectively unreachable; it exists so the conversion is never
78    /// mislabeled as a different failure mode.
79    #[error("internal fixed-width conversion failure")]
80    InternalSliceConversion,
81}
82
83/// `Result<T, Error>` for convenience.
84pub type Result<T> = core::result::Result<T, Error>;