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_core::error::BufferTooShort),
18
19    /// CRC-32 validation failed for a table section.
20    #[error(transparent)]
21    CrcMismatch(#[from] broadcast_core::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    #[non_exhaustive]
26    UnexpectedTableId {
27        /// Byte value actually read.
28        table_id: u8,
29        /// Table names or parser expecting it.
30        what: &'static str,
31        /// The permitted set.
32        expected: &'static [u8],
33    },
34
35    /// Descriptor payload failed semantic validation.
36    #[error("invalid descriptor (tag {tag:#04x}): {reason}")]
37    #[non_exhaustive]
38    InvalidDescriptor {
39        /// Descriptor tag being parsed.
40        tag: u8,
41        /// Specific failure reason.
42        reason: &'static str,
43    },
44
45    /// A decoded value passed to a `set_*` accessor could not be encoded to the
46    /// field's fixed wire representation (e.g. a duration ≥ 100 hours, or a date
47    /// outside the 16-bit MJD range).
48    #[error("value out of range for {field}: {reason}")]
49    #[non_exhaustive]
50    ValueOutOfRange {
51        /// Field being set.
52        field: &'static str,
53        /// Why the value is not representable.
54        reason: &'static str,
55    },
56
57    /// Text in a Table A.3 character set whose codec needs the
58    /// `legacy-charsets` feature, decoded with that feature off.
59    #[error("{charset} text needs the `legacy-charsets` feature")]
60    #[non_exhaustive]
61    CharsetNotEnabled {
62        /// The character set the selector names.
63        charset: crate::text::LegacyCharset,
64    },
65
66    /// A `section_length` declared more bytes than the containing buffer could hold.
67    #[error("section_length {declared} exceeds remaining buffer ({available} bytes)")]
68    #[non_exhaustive]
69    SectionLengthOverflow {
70        /// Length bytes declared inside the section header.
71        declared: usize,
72        /// Bytes actually available after the header.
73        available: usize,
74    },
75
76    /// A whole document exceeded the named byte limit it was read under.
77    #[error("{limit_name} exceeds the configured limit of {limit_bytes} bytes")]
78    #[non_exhaustive]
79    TooLarge {
80        /// Name of the document being read.
81        limit_name: &'static str,
82        /// The ceiling it exceeded, in bytes.
83        limit_bytes: u64,
84    },
85
86    /// A reserved or fixed-value bit group was not in the state a spec clause
87    /// requires. Most parsers are permissive about reserved bits; this is only
88    /// emitted when a clause specifically requires a value.
89    #[error(transparent)]
90    ReservedBits(#[from] broadcast_core::error::ReservedBits),
91
92    /// A field held a value its spec clause forbids.
93    #[error(transparent)]
94    InvalidValue(#[from] broadcast_core::error::InvalidValue),
95
96    /// A field is structurally invalid with no single offending number.
97    #[error(transparent)]
98    InvalidField(#[from] broadcast_core::error::InvalidField),
99
100    /// Write buffer passed to `serialize_into` was smaller than `serialized_len()`.
101    #[error(transparent)]
102    OutputBufferTooSmall(#[from] broadcast_core::error::OutputBufferTooSmall),
103
104    /// A fixed marker did not match (TS sync byte, expected `0x47`).
105    #[error(transparent)]
106    InvalidSync(#[from] broadcast_core::error::InvalidSync),
107
108    /// A serializer's length or count value did not fit its wire field.
109    /// Returned instead of silently narrowing (`as u8`/`as u16`/masking),
110    /// which would emit a misframed section (see #1129).
111    #[error(transparent)]
112    FieldOverflow(#[from] broadcast_core::error::FieldOverflow),
113}
114
115impl Error {
116    /// Builds [`Self::TooLarge`].
117    #[must_use]
118    pub fn too_large(limit_name: &'static str, limit_bytes: u64) -> Self {
119        Self::TooLarge {
120            limit_name,
121            limit_bytes,
122        }
123    }
124    /// Builds [`Self::InvalidDescriptor`].
125    #[must_use]
126    pub fn invalid_descriptor(tag: u8, reason: &'static str) -> Self {
127        Self::InvalidDescriptor { tag, reason }
128    }
129
130    /// Builds [`Self::SectionLengthOverflow`].
131    #[must_use]
132    pub fn section_length_overflow(declared: usize, available: usize) -> Self {
133        Self::SectionLengthOverflow {
134            declared,
135            available,
136        }
137    }
138
139    /// Builds [`Self::ValueOutOfRange`].
140    #[must_use]
141    pub fn value_out_of_range(field: &'static str, reason: &'static str) -> Self {
142        Self::ValueOutOfRange { field, reason }
143    }
144}
145
146impl From<mpeg_ts::Error> for Error {
147    fn from(e: mpeg_ts::Error) -> Self {
148        match e {
149            mpeg_ts::Error::BufferTooShort(e) => Error::BufferTooShort(e),
150            mpeg_ts::Error::CrcMismatch(e) => Error::CrcMismatch(e),
151            mpeg_ts::Error::InvalidSync(e) => Error::InvalidSync(e),
152            mpeg_ts::Error::OutputBufferTooSmall(e) => Error::OutputBufferTooSmall(e),
153            mpeg_ts::Error::SectionLengthOverflow {
154                declared,
155                available,
156                ..
157            } => Error::SectionLengthOverflow {
158                declared,
159                available,
160            },
161            // mpeg_ts::Error is #[non_exhaustive]; catch future variants generically.
162            _ => Error::BufferTooShort(broadcast_core::error::BufferTooShort::new(
163                0,
164                0,
165                "mpeg_ts (unknown error)",
166            )),
167        }
168    }
169}