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)]
13#[non_exhaustive]
14pub enum Error {
15    /// Input buffer was shorter than the smallest valid encoding for the type.
16    #[error("buffer too short: need {need} bytes, have {have} (while parsing {what})")]
17    BufferTooShort {
18        /// Bytes required to proceed.
19        need: usize,
20        /// Bytes actually available.
21        have: usize,
22        /// Human-readable name of the type or field being parsed.
23        what: &'static str,
24    },
25
26    /// CRC-32 validation failed for a table section.
27    #[error("CRC-32 mismatch: computed {computed:#010x}, expected {expected:#010x}")]
28    CrcMismatch {
29        /// CRC we calculated over the section bytes.
30        computed: u32,
31        /// CRC carried at the end of the section.
32        expected: u32,
33    },
34
35    /// TS sync byte was not the expected `0x47`.
36    #[error("invalid TS sync byte: expected 0x47, got {found:#04x}")]
37    InvalidSyncByte {
38        /// The byte actually read at position 0.
39        found: u8,
40    },
41
42    /// Write buffer passed to `serialize_into` was smaller than `serialized_len()`.
43    #[error("serialize: output buffer too small — need {need}, have {have}")]
44    OutputBufferTooSmall {
45        /// Required size.
46        need: usize,
47        /// Actual size.
48        have: usize,
49    },
50
51    /// A `section_length` declared more bytes than the containing buffer could hold.
52    #[error("section_length {declared} exceeds remaining buffer ({available} bytes)")]
53    SectionLengthOverflow {
54        /// Length bytes declared inside the section header.
55        declared: usize,
56        /// Bytes actually available after the header.
57        available: usize,
58    },
59
60    /// A payload was longer than a single TS packet's fixed payload capacity
61    /// (see `OwnedTsPacket::serialize_with_payload`, #1129).
62    #[error("payload {len} bytes exceeds the {max}-byte TS packet payload capacity")]
63    PayloadTooLarge {
64        /// The payload length that did not fit.
65        len: usize,
66        /// The maximum payload capacity.
67        max: usize,
68    },
69
70    /// A length or count field's value does not fit its wire field
71    /// (see `broadcast_common::len`, #1129).
72    #[error(transparent)]
73    FieldOverflow(#[from] broadcast_common::len::FieldOverflow),
74
75    /// `Section::payload` did not match what `section_length` implies (see
76    /// `Section::serialize_into`, #1074 W5). Serializing a hand-built
77    /// `Section` whose fields disagree used to index past `serialized_len()`
78    /// and panic (or write the CRC over payload bytes) instead of erroring.
79    #[error(
80        "section payload is {actual} bytes but section_length ({section_length}) implies {expected}"
81    )]
82    SectionPayloadLengthMismatch {
83        /// `section_length` field the caller set.
84        section_length: u16,
85        /// Payload length `section_length` implies (accounting for the
86        /// extension header + CRC on long-form sections).
87        expected: usize,
88        /// Actual `payload.len()`.
89        actual: usize,
90    },
91}