Skip to main content

dvb_t2mi/
error.rs

1//! Error type returned by every parser 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/// ETSI TS 102 773 v1.4.1 where applicable.
12#[derive(Debug, Error, PartialEq, Eq)]
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    /// `packet_type` byte is not in Table 1 (0x00..=0x02, 0x10..=0x12, 0x20..=0x21, 0x30..=0x33).
26    #[error("invalid T2-MI packet_type {found:#04x} — reserved per ETSI TS 102 773 Table 1")]
27    InvalidPacketType {
28        /// Byte value actually read.
29        found: u8,
30    },
31
32    /// A reserved bit was not in the expected state.
33    #[error("reserved bits violation in {field}: {reason}")]
34    ReservedBitsViolation {
35        /// Where.
36        field: &'static str,
37        /// Why.
38        reason: &'static str,
39    },
40
41    /// Write buffer passed to `serialize_into` was smaller than `serialized_len()`.
42    #[error("serialize: output buffer too small — need {need}, have {have}")]
43    OutputBufferTooSmall {
44        /// Required size.
45        need: usize,
46        /// Actual size.
47        have: usize,
48    },
49
50    /// Payload length declared more bits than remaining bytes.
51    #[error(
52        "payload length mismatch: {declared_bits} bits declared, {remaining_bytes} bytes remaining"
53    )]
54    PayloadLengthMismatch {
55        /// Declared payload_len_bits from header.
56        declared_bits: u16,
57        /// Actual remaining bytes.
58        remaining_bytes: usize,
59    },
60
61    /// Input buffer was shorter than required for CRC validation.
62    #[error("buffer too short for CRC validation")]
63    Truncated,
64
65    /// CRC-32 value did not match expected checksum.
66    #[error("CRC-32 mismatch: expected {expected:#010x}, computed {computed:#010x}")]
67    InvalidCrc {
68        /// Expected CRC from trailer.
69        expected: u32,
70        /// Computed CRC over payload.
71        computed: u32,
72    },
73}