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_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    /// A fixed marker did not match (TS sync byte, expected `0x47`).
24    #[error(transparent)]
25    InvalidSync(#[from] broadcast_common::error::InvalidSync),
26
27    /// Write buffer passed to `serialize_into` was smaller than `serialized_len()`.
28    #[error(transparent)]
29    OutputBufferTooSmall(#[from] broadcast_common::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    SectionLengthOverflow {
34        /// Length bytes declared inside the section header.
35        declared: usize,
36        /// Bytes actually available after the header.
37        available: usize,
38    },
39
40    /// A payload was longer than a single TS packet's fixed payload capacity
41    /// (see `OwnedTsPacket::serialize_with_payload`, #1129).
42    #[error("payload {len} bytes exceeds the {max}-byte TS packet payload capacity")]
43    PayloadTooLarge {
44        /// The payload length that did not fit.
45        len: usize,
46        /// The maximum payload capacity.
47        max: usize,
48    },
49
50    /// A length or count field's value does not fit its wire field
51    /// (see `broadcast_common::len`, #1129).
52    #[error(transparent)]
53    FieldOverflow(#[from] broadcast_common::len::FieldOverflow),
54
55    /// `Section::payload` did not match what `section_length` implies (see
56    /// `Section::serialize_into`, #1074 W5). Serializing a hand-built
57    /// `Section` whose fields disagree used to index past `serialized_len()`
58    /// and panic (or write the CRC over payload bytes) instead of erroring.
59    #[error(
60        "section payload is {actual} bytes but section_length ({section_length}) implies {expected}"
61    )]
62    SectionPayloadLengthMismatch {
63        /// `section_length` field the caller set.
64        section_length: u16,
65        /// Payload length `section_length` implies (accounting for the
66        /// extension header + CRC on long-form sections).
67        expected: usize,
68        /// Actual `payload.len()`.
69        actual: usize,
70    },
71}