1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//! Error type returned by every parser in this crate.
use thiserror::Error;
/// Crate-wide result alias.
pub type Result<T> = core::result::Result<T, Error>;
/// Error variants that parsers + builders can return.
///
/// Spec references inside `#[error(...)]` strings quote clauses from
/// ETSI EN 300 468 v1.19.1 where applicable.
#[derive(Debug, Error)]
pub enum Error {
/// Input buffer was shorter than the smallest valid encoding for the type.
#[error("buffer too short: need {need} bytes, have {have} (while parsing {what})")]
BufferTooShort {
/// Bytes required to proceed.
need: usize,
/// Bytes actually available.
have: usize,
/// Human-readable name of the type or field being parsed.
what: &'static str,
},
/// CRC-32 validation failed for a table section.
#[error("CRC-32 mismatch: computed {computed:#010x}, expected {expected:#010x}")]
CrcMismatch {
/// CRC we calculated over the section bytes.
computed: u32,
/// CRC carried at the end of the section.
expected: u32,
},
/// `table_id` byte doesn't match any expected value for the parser invoked.
#[error("unexpected table_id {table_id:#04x} for {what} (expected one of {expected:?})")]
UnexpectedTableId {
/// Byte value actually read.
table_id: u8,
/// Table names or parser expecting it.
what: &'static str,
/// The permitted set.
expected: &'static [u8],
},
/// Descriptor payload failed semantic validation.
#[error("invalid descriptor (tag {tag:#04x}): {reason}")]
InvalidDescriptor {
/// Descriptor tag being parsed.
tag: u8,
/// Specific failure reason.
reason: &'static str,
},
/// BCD-encoded value is out of valid range.
#[error("invalid BCD in {field}: bytes {bytes:02x?}")]
InvalidBcd {
/// Field name where the BCD sits.
field: &'static str,
/// The (up to 4) raw bytes inspected.
bytes: [u8; 4],
},
/// A `section_length` declared more bytes than the containing buffer could hold.
#[error("section_length {declared} exceeds remaining buffer ({available} bytes)")]
SectionLengthOverflow {
/// Length bytes declared inside the section header.
declared: usize,
/// Bytes actually available after the header.
available: usize,
},
/// A reserved bit was not in the expected state. Most parsers are permissive
/// about reserved bits; this variant is only emitted when a spec clause
/// specifically requires a value.
#[error("reserved bits violation in {field}: {reason}")]
ReservedBitsViolation {
/// Where.
field: &'static str,
/// Why.
reason: &'static str,
},
/// Write buffer passed to `serialize_into` was smaller than `serialized_len()`.
#[error("serialize: output buffer too small — need {need}, have {have}")]
OutputBufferTooSmall {
/// Required size.
need: usize,
/// Actual size.
have: usize,
},
/// TS sync byte was not the expected `0x47`.
#[error("invalid TS sync byte: expected 0x47, got {found:#04x}")]
InvalidSyncByte {
/// The byte actually read at position 0.
found: u8,
},
}