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)]
10pub enum Error {
11    /// Input buffer was shorter than the smallest valid encoding.
12    #[error("buffer too short: need {need} bytes, have {have}")]
13    BufferTooShort {
14        /// Bytes required to proceed.
15        need: usize,
16        /// Bytes actually available.
17        have: usize,
18    },
19
20    /// MODE field is neither 0 (NM) nor 1 (HEM).
21    #[error("invalid MODE: {mode} (must be 0 or 1 per EN 302 755 §5.1.7)")]
22    InvalidMode {
23        /// MODE value that was rejected.
24        mode: u8,
25    },
26
27    /// TS/GS input stream type is not supported.
28    #[error("unsupported TS/GS: 0x{ts_gs:02X}")]
29    UnsupportedTsGs {
30        /// The invalid TS/GS value.
31        ts_gs: u8,
32    },
33
34    /// Write buffer passed to `serialize_into` was smaller than `serialized_len()`.
35    #[error("serialize: output buffer too small — need {need}, have {have}")]
36    OutputBufferTooSmall {
37        /// Required size.
38        need: usize,
39        /// Actual size.
40        have: usize,
41    },
42
43    /// DFL field is outside the valid range. The enforced ceiling `DFL_MAX_BITS`
44    /// (64800) is the DVB-S2 normal-FECFRAME data-field bound (EN 302 307-1
45    /// §5.1.4); DVB-T2 is tighter still (0..=53760, EN 302 755 Table 2).
46    #[error(
47        "DFL={dfl} bits exceeds maximum {max} bits (EN 302 307-1 §5.1.4 S2 normal frame; \
48         DVB-T2 tighter per EN 302 755 Table 2)"
49    )]
50    DflOutOfRange {
51        /// DFL value that was rejected.
52        dfl: u16,
53        /// Maximum allowed DFL.
54        max: u16,
55    },
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn buffer_too_short_message_contains_values() {
64        let err = Error::BufferTooShort { need: 10, have: 5 };
65        let msg = format!("{err}");
66        assert!(msg.contains("10") && msg.contains("5"));
67    }
68
69    #[test]
70    fn invalid_mode_message_contains_clause_ref() {
71        let err = Error::InvalidMode { mode: 3 };
72        let msg = format!("{err}");
73        assert!(msg.contains("5.1.7"));
74    }
75}