Skip to main content

dvb_bbframe/
error.rs

1//! Error type for BBFrame parsing and serialization.
2
3/// Crate-wide result alias.
4pub type Result<T> = core::result::Result<T, Error>;
5
6/// Error type for BBFrame parsing and serialization.
7///
8/// All variants carry spec-clause references in their display messages.
9#[derive(Debug, thiserror::Error, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum Error {
12    /// Input buffer was shorter than the smallest valid encoding.
13    #[error("buffer too short: need {need} bytes, have {have} (while parsing {what})")]
14    BufferTooShort {
15        /// Bytes required to proceed.
16        need: usize,
17        /// Bytes actually available.
18        have: usize,
19        /// Human-readable name of the type or field being parsed.
20        what: &'static str,
21    },
22
23    /// MODE field is neither 0 (NM) nor 1 (HEM).
24    #[error("invalid MODE: {mode} (must be 0 or 1 per EN 302 755 §5.1.7)")]
25    InvalidMode {
26        /// MODE value that was rejected.
27        mode: u8,
28    },
29
30    /// TS/GS input stream type is not supported.
31    #[error("unsupported TS/GS: 0x{ts_gs:02X}")]
32    UnsupportedTsGs {
33        /// The invalid TS/GS value.
34        ts_gs: u8,
35    },
36
37    /// Write buffer passed to `serialize_into` was smaller than `serialized_len()`.
38    #[error("serialize: output buffer too small — need {need}, have {have}")]
39    OutputBufferTooSmall {
40        /// Required size.
41        need: usize,
42        /// Actual size.
43        have: usize,
44    },
45
46    /// ISSY form/prefix bit does not match the decoder called.
47    #[error("invalid ISSY form: {reason} (EN 302 755 Annex C)")]
48    InvalidIssyForm {
49        /// Why the form was rejected.
50        reason: &'static str,
51    },
52
53    /// DFL field is outside the valid range. The enforced ceiling `DFL_MAX_BITS`
54    /// (64800) is the DVB-S2 normal-FECFRAME data-field bound (EN 302 307-1
55    /// §5.1.4); DVB-T2 is tighter still (0..=53760, EN 302 755 Table 2).
56    #[error(
57        "DFL={dfl} bits exceeds maximum {max} bits (EN 302 307-1 §5.1.4 S2 normal frame; \
58         DVB-T2 tighter per EN 302 755 Table 2)"
59    )]
60    DflOutOfRange {
61        /// DFL value that was rejected.
62        dfl: u16,
63        /// Maximum allowed DFL.
64        max: u16,
65    },
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    #[test]
73    fn buffer_too_short_message_contains_values() {
74        let err = Error::BufferTooShort {
75            need: 10,
76            have: 5,
77            what: "BBHEADER",
78        };
79        let msg = format!("{err}");
80        assert!(msg.contains("10") && msg.contains("5") && msg.contains("BBHEADER"));
81    }
82
83    #[test]
84    fn invalid_mode_message_contains_clause_ref() {
85        let err = Error::InvalidMode { mode: 3 };
86        let msg = format!("{err}");
87        assert!(msg.contains("5.1.7"));
88    }
89}