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, PartialEq, Eq, Clone)]
13#[non_exhaustive]
14pub enum Error {
15    /// Input buffer was shorter than the smallest valid encoding for the type.
16    #[error(transparent)]
17    BufferTooShort(#[from] broadcast_common::error::BufferTooShort),
18
19    /// CRC-32 validation failed for a table section.
20    #[error(transparent)]
21    CrcMismatch(#[from] broadcast_common::error::CrcMismatch),
22
23    /// `table_id` byte doesn't match any expected value for the parser invoked.
24    #[error("unexpected table_id {table_id:#04x} for {what} (expected one of {expected:?})")]
25    UnexpectedTableId {
26        /// Byte value actually read.
27        table_id: u8,
28        /// Table names or parser expecting it.
29        what: &'static str,
30        /// The permitted set.
31        expected: &'static [u8],
32    },
33
34    /// Descriptor payload failed semantic validation.
35    #[error("invalid descriptor (tag {tag:#04x}): {reason}")]
36    InvalidDescriptor {
37        /// Descriptor tag being parsed.
38        tag: u8,
39        /// Specific failure reason.
40        reason: &'static str,
41    },
42
43    /// A decoded value passed to a `set_*` accessor could not be encoded to the
44    /// field's fixed wire representation (e.g. a duration ≥ 100 hours, or a date
45    /// outside the 16-bit MJD range).
46    #[error("value out of range for {field}: {reason}")]
47    ValueOutOfRange {
48        /// Field being set.
49        field: &'static str,
50        /// Why the value is not representable.
51        reason: &'static str,
52    },
53
54    /// Text in a Table A.3 character set whose codec needs the
55    /// `legacy-charsets` feature, decoded with that feature off.
56    #[error("{charset} text needs the `legacy-charsets` feature")]
57    CharsetNotEnabled {
58        /// The character set the selector names.
59        charset: crate::text::LegacyCharset,
60    },
61
62    /// A `section_length` declared more bytes than the containing buffer could hold.
63    #[error("section_length {declared} exceeds remaining buffer ({available} bytes)")]
64    SectionLengthOverflow {
65        /// Length bytes declared inside the section header.
66        declared: usize,
67        /// Bytes actually available after the header.
68        available: usize,
69    },
70
71    /// A reserved or fixed-value bit group was not in the state a spec clause
72    /// requires. Most parsers are permissive about reserved bits; this is only
73    /// emitted when a clause specifically requires a value.
74    #[error(transparent)]
75    ReservedBits(#[from] broadcast_common::error::ReservedBits),
76
77    /// A field held a value its spec clause forbids.
78    #[error(transparent)]
79    InvalidValue(#[from] broadcast_common::error::InvalidValue),
80
81    /// A field is structurally invalid with no single offending number.
82    #[error(transparent)]
83    InvalidField(#[from] broadcast_common::error::InvalidField),
84
85    /// Write buffer passed to `serialize_into` was smaller than `serialized_len()`.
86    #[error(transparent)]
87    OutputBufferTooSmall(#[from] broadcast_common::error::OutputBufferTooSmall),
88
89    /// A fixed marker did not match (TS sync byte, expected `0x47`).
90    #[error(transparent)]
91    InvalidSync(#[from] broadcast_common::error::InvalidSync),
92
93    /// A serializer's length or count value did not fit its wire field.
94    /// Returned instead of silently narrowing (`as u8`/`as u16`/masking),
95    /// which would emit a misframed section (see #1129).
96    #[error(transparent)]
97    FieldOverflow(#[from] broadcast_common::len::FieldOverflow),
98}
99
100impl From<mpeg_ts::Error> for Error {
101    fn from(e: mpeg_ts::Error) -> Self {
102        match e {
103            mpeg_ts::Error::BufferTooShort(e) => Error::BufferTooShort(e),
104            mpeg_ts::Error::CrcMismatch(e) => Error::CrcMismatch(e),
105            mpeg_ts::Error::InvalidSync(e) => Error::InvalidSync(e),
106            mpeg_ts::Error::OutputBufferTooSmall(e) => Error::OutputBufferTooSmall(e),
107            mpeg_ts::Error::SectionLengthOverflow {
108                declared,
109                available,
110            } => Error::SectionLengthOverflow {
111                declared,
112                available,
113            },
114            // mpeg_ts::Error is #[non_exhaustive]; catch future variants generically.
115            _ => Error::BufferTooShort(broadcast_common::error::BufferTooShort {
116                need: 0,
117                have: 0,
118                what: "mpeg_ts (unknown error)",
119            }),
120        }
121    }
122}