Skip to main content

dvb_subtitle/
error.rs

1//! Error type for DVB subtitle parsing.
2
3/// Result alias for DVB subtitle parsing.
4pub type Result<T> = core::result::Result<T, Error>;
5
6/// A DVB subtitle parse or serialize error.
7#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
8#[non_exhaustive]
9pub enum Error {
10    /// Input shorter than required.
11    #[error("buffer too short: need {need}, have {have} ({what})")]
12    BufferTooShort {
13        /// Bytes required.
14        need: usize,
15        /// Bytes available.
16        have: usize,
17        /// What was being parsed.
18        what: &'static str,
19    },
20    /// The `data_identifier` was not the required `0x20`.
21    #[error("bad data_identifier: {0:#04X} (expected 0x20)")]
22    BadDataIdentifier(u8),
23    /// The `sync_byte` was not the required `0x0F`.
24    #[error("bad sync_byte: {0:#04X} (expected 0x0F)")]
25    BadSyncByte(u8),
26    /// The `end_of_PES_data_field_marker` was not `0xFF`.
27    #[error("bad end_of_PES_data_field_marker: {0:#04X} (expected 0xFF)")]
28    BadEndOfPesMarker(u8),
29    /// An unrecognised or invalid segment_type was encountered.
30    #[error("unknown segment_type: {0:#04X}")]
31    UnknownSegmentType(u8),
32    /// An unrecognised or invalid data_type in a pixel-data sub-block.
33    #[error("unknown data_type: {0:#04X}")]
34    UnknownDataType(u8),
35    /// An unrecognised or invalid object_coding_method.
36    #[error("unknown object_coding_method: {0:#02X}")]
37    UnknownObjectCodingMethod(u8),
38    /// Stuffing byte was not 0x00.
39    #[error("non-zero stuffing byte: {0:#04X} (expected 0x00)")]
40    BadStuffingByte(u8),
41    /// Segment length too large for parsed data.
42    #[error("segment too large to serialize: segment_length oversized")]
43    SegmentTooLarge,
44    /// A field value did not fit its wire width (#1129/#1044): compared
45    /// before narrowing, so an over-range value is rejected instead of
46    /// silently masked/wrapped.
47    #[error(transparent)]
48    FieldOverflow(#[from] broadcast_common::len::FieldOverflow),
49    /// A `DisparityRegion` had 0 or more than 4 subregions — the wire
50    /// `number_of_subregions_minus_1` field (Table 29) is 2 bits, so only
51    /// 1..=4 subregions are representable.
52    #[error("invalid subregion count: {0} (must be 1..=4)")]
53    InvalidSubregionCount(usize),
54    /// A `Subregion`'s `subregion_horizontal_position`/`subregion_width`
55    /// presence didn't match the region's subregion count (Table 29: both
56    /// are present for every subregion when, and only when, the region has
57    /// more than one).
58    #[error("subregion position presence inconsistent with subregion count")]
59    SubregionPositionMismatch,
60    /// A repeating-entry loop (Table 31's CLUT entry loop; Table 21's page
61    /// composition region loop) had leftover bytes that don't form a whole
62    /// entry, where the spec's own loop condition never leaves a remainder.
63    #[error("{extra} trailing byte(s) in {what}: not a whole entry ({entry_len} bytes each)")]
64    TrailingEntryBytes {
65        /// What loop this was (e.g. `"alternative_CLUT_segment entries"`).
66        what: &'static str,
67        /// The fixed size of one entry.
68        entry_len: usize,
69        /// How many bytes were left over.
70        extra: usize,
71    },
72    /// A field value does not fit the bits the wire format gives it (e.g. a
73    /// reduced-range CLUT colour component wider than its 6/4/2-bit slot).
74    #[error("invalid value for {field}: {reason}")]
75    InvalidValue {
76        /// The field being serialized.
77        field: &'static str,
78        /// Why the value is rejected.
79        reason: &'static str,
80    },
81}