Skip to main content

dvb_si/
error.rs

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