Skip to main content

mpeg_ts/
error.rs

1//! Error type returned by every parser and builder 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/// ITU-T H.222.0 (= ISO/IEC 13818-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    /// A fixed marker did not match (TS sync byte, expected `0x47`).
24    #[error(transparent)]
25    InvalidSync(#[from] broadcast_core::error::InvalidSync),
26
27    /// Write buffer passed to `serialize_into` was smaller than `serialized_len()`.
28    #[error(transparent)]
29    OutputBufferTooSmall(#[from] broadcast_core::error::OutputBufferTooSmall),
30
31    /// A `section_length` declared more bytes than the containing buffer could hold.
32    #[error("section_length {declared} exceeds remaining buffer ({available} bytes)")]
33    #[non_exhaustive]
34    SectionLengthOverflow {
35        /// Length bytes declared inside the section header.
36        declared: usize,
37        /// Bytes actually available after the header.
38        available: usize,
39    },
40
41    /// A payload was longer than a single TS packet's fixed payload capacity
42    /// (see `OwnedTsPacket::serialize_with_payload`, #1129).
43    #[error("payload {len} bytes exceeds the {max}-byte TS packet payload capacity")]
44    #[non_exhaustive]
45    PayloadTooLarge {
46        /// The payload length that did not fit.
47        len: usize,
48        /// The maximum payload capacity.
49        max: usize,
50    },
51
52    /// A PCR or ESCR value did not fit the 27 MHz clock: a base wider than
53    /// 33 bits, or an extension `>= 300` (the 9-bit wire field can carry
54    /// `300..=511`).
55    #[error(transparent)]
56    ClockOutOfRange(#[from] broadcast_common::clock33::ClockOutOfRange),
57
58    /// A length or count field's value does not fit its wire field
59    /// (see `broadcast_core::len`, #1129).
60    #[error(transparent)]
61    FieldOverflow(#[from] broadcast_core::error::FieldOverflow),
62
63    /// The packet has no adaptation field, so it has no PCR (and no OPCR)
64    /// slot to rewrite.
65    #[error("packet has no adaptation field")]
66    NoAdaptationField,
67
68    /// The adaptation field has no PCR (or OPCR) slot: either it is shorter
69    /// than the flags byte plus the 6-byte field, or the packet's own flag
70    /// byte does not set the corresponding flag.
71    #[error("packet has no {which} slot")]
72    #[non_exhaustive]
73    NoClockReference {
74        /// The missing field: `"PCR"` or `"OPCR"`.
75        which: &'static str,
76    },
77
78    /// The adaptation field is shorter than the field being written into it.
79    #[error("adaptation field is {have} bytes, {need} needed")]
80    #[non_exhaustive]
81    AdaptationFieldTooShort {
82        /// Bytes the write needs.
83        need: usize,
84        /// `adaptation_field_length` the packet declares.
85        have: usize,
86    },
87
88    /// A descriptor loop ends with fewer bytes than a `descriptor()` header
89    /// (ISO/IEC 13818-1 §2.6).
90    #[error("descriptor header at loop offset {offset} needs {need} bytes, {have} remain")]
91    #[non_exhaustive]
92    DescriptorHeaderTruncated {
93        /// Offset of the truncated header inside the loop.
94        offset: usize,
95        /// Bytes the header needs.
96        need: usize,
97        /// Bytes left in the loop from that offset.
98        have: usize,
99    },
100
101    /// A descriptor declares a `descriptor_length` that runs past the end of
102    /// its loop (ISO/IEC 13818-1 §2.6).
103    #[error(
104        "descriptor {tag:#04X} at loop offset {offset} declares {declared} body bytes, {have} remain"
105    )]
106    #[non_exhaustive]
107    DescriptorBodyTruncated {
108        /// Offset of the descriptor header inside the loop.
109        offset: usize,
110        /// Its `descriptor_tag`.
111        tag: u8,
112        /// Its declared `descriptor_length`.
113        declared: usize,
114        /// Body bytes actually left in the loop.
115        have: usize,
116    },
117}