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}