Skip to main content

scte35_splice/
error.rs

1//! Error type returned by every parser + 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 SCTE 35 parsers and builders can return.
9///
10/// Spec references quote clauses from ANSI/SCTE 35 2023r1 where applicable.
11#[derive(Debug, Error, PartialEq, Eq)]
12#[non_exhaustive]
13pub enum Error {
14    /// Input buffer was shorter than the smallest valid encoding for the type.
15    #[error("buffer too short: need {need} bytes, have {have} (while parsing {what})")]
16    BufferTooShort {
17        /// Bytes required to proceed.
18        need: usize,
19        /// Bytes actually available.
20        have: usize,
21        /// Human-readable name of the type or field being parsed.
22        what: &'static str,
23    },
24
25    /// CRC-32 validation failed for a splice_info_section (§9.6.1).
26    #[error("CRC-32 mismatch: computed {computed:#010x}, expected {expected:#010x}")]
27    CrcMismatch {
28        /// CRC we calculated over the section bytes.
29        computed: u32,
30        /// CRC carried at the end of the section.
31        expected: u32,
32    },
33
34    /// `table_id` byte was not the expected `0xFC` (§9.6.1).
35    #[error("unexpected table_id {table_id:#04x} for splice_info_section (expected 0xFC)")]
36    UnexpectedTableId {
37        /// Byte value actually read.
38        table_id: u8,
39    },
40
41    /// A `splice_descriptor_tag` byte did not match the value the invoked
42    /// descriptor parser expected (§10.2.1).
43    #[error("unexpected splice_descriptor_tag {tag:#04x} for {what} (expected {expected:#04x})")]
44    UnexpectedDescriptorTag {
45        /// Tag byte actually read.
46        tag: u8,
47        /// Descriptor parser invoked.
48        what: &'static str,
49        /// Tag the parser expected.
50        expected: u8,
51    },
52
53    /// `splice_command_type` byte did not match the value the invoked command
54    /// parser expected (§9.6.1, Table 7).
55    #[error("unexpected splice_command_type {got:#04x} for {what} (expected {expected:#04x})")]
56    UnexpectedCommandType {
57        /// Command type byte actually read.
58        got: u8,
59        /// Command parser invoked.
60        what: &'static str,
61        /// Command type the parser expected.
62        expected: u8,
63    },
64
65    /// A field carried a value the structure cannot represent or that violates
66    /// a spec constraint (e.g. a 33-bit field given a value ≥ 2^33).
67    #[error("invalid value for {field}: {reason}")]
68    InvalidValue {
69        /// Field being validated.
70        field: &'static str,
71        /// Why the value is rejected.
72        reason: &'static str,
73    },
74
75    /// A declared length (section_length, descriptor_length, …) ran past the
76    /// bytes available in the buffer.
77    #[error("length {declared} exceeds remaining buffer ({available} bytes) for {what}")]
78    LengthOverflow {
79        /// Length declared inside the wire header.
80        declared: usize,
81        /// Bytes actually available.
82        available: usize,
83        /// What the length describes.
84        what: &'static str,
85    },
86
87    /// Write buffer passed to `serialize_into` was smaller than `serialized_len()`.
88    #[error("serialize: output buffer too small — need {need}, have {have}")]
89    OutputBufferTooSmall {
90        /// Required size.
91        need: usize,
92        /// Actual size.
93        have: usize,
94    },
95}