ndn_tlv/
error.rs

1/// Errors produced by TLV decoding.
2#[derive(Debug, Clone, PartialEq, Eq)]
3pub enum TlvError {
4    /// Packet was truncated before the expected end.
5    UnexpectedEof,
6    /// Unknown TLV type with the critical bit set (odd type number) — must drop.
7    UnknownCriticalType(u64),
8    /// A field had the wrong encoded length.
9    InvalidLength {
10        typ: u64,
11        expected: usize,
12        got: usize,
13    },
14    /// A UTF-8 field contained invalid bytes.
15    InvalidUtf8 { typ: u64 },
16    /// A required field was absent.
17    MissingField(&'static str),
18    /// The same TLV type appeared more than once where only one is allowed.
19    DuplicateField(u64),
20    /// VarNumber is not in shortest encoding form.
21    NonMinimalVarNumber,
22}
23
24impl core::fmt::Display for TlvError {
25    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
26        match self {
27            TlvError::UnexpectedEof => write!(f, "unexpected end of buffer"),
28            TlvError::UnknownCriticalType(t) => {
29                write!(f, "unknown critical TLV type {t:#x}")
30            }
31            TlvError::InvalidLength { typ, expected, got } => {
32                write!(
33                    f,
34                    "TLV type {typ:#x}: expected length {expected}, got {got}"
35                )
36            }
37            TlvError::InvalidUtf8 { typ } => {
38                write!(f, "TLV type {typ:#x} contains invalid UTF-8")
39            }
40            TlvError::MissingField(name) => write!(f, "required field '{name}' missing"),
41            TlvError::DuplicateField(t) => {
42                write!(f, "TLV type {t:#x} appeared more than once")
43            }
44            TlvError::NonMinimalVarNumber => {
45                write!(f, "VarNumber not in shortest encoding form")
46            }
47        }
48    }
49}
50
51impl core::error::Error for TlvError {}