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    /// DFL field is outside the valid range. The enforced ceiling `DFL_MAX_BITS`
47    /// (64800) is the DVB-S2 normal-FECFRAME data-field bound (EN 302 307-1
48    /// §5.1.4); DVB-T2 is tighter still (0..=53760, EN 302 755 Table 2).
49    #[error(
50        "DFL={dfl} bits exceeds maximum {max} bits (EN 302 307-1 §5.1.4 S2 normal frame; \
51         DVB-T2 tighter per EN 302 755 Table 2)"
52    )]
53    DflOutOfRange {
54        /// DFL value that was rejected.
55        dfl: u16,
56        /// Maximum allowed DFL.
57        max: u16,
58    },
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn buffer_too_short_message_contains_values() {
67        let err = Error::BufferTooShort {
68            need: 10,
69            have: 5,
70            what: "BBHEADER",
71        };
72        let msg = format!("{err}");
73        assert!(msg.contains("10") && msg.contains("5") && msg.contains("BBHEADER"));
74    }
75
76    #[test]
77    fn invalid_mode_message_contains_clause_ref() {
78        let err = Error::InvalidMode { mode: 3 };
79        let msg = format!("{err}");
80        assert!(msg.contains("5.1.7"));
81    }
82}