1#[derive(Debug, Clone, PartialEq, Eq)]
3pub enum TlvError {
4 UnexpectedEof,
6 UnknownCriticalType(u64),
8 InvalidLength {
10 typ: u64,
11 expected: usize,
12 got: usize,
13 },
14 InvalidUtf8 { typ: u64 },
16 MissingField(&'static str),
18 DuplicateField(u64),
20 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 {}