Skip to main content

dvb_si/
table_id.rs

1//! `TableId` enum: typed `table_id` byte values.
2//!
3//! Source: ETSI EN 300 468 §5.1.3 Table 2 plus ISO/IEC 13818-1 for MPEG tables.
4
5use num_enum::TryFromPrimitive;
6
7/// Typed `table_id` enumeration.
8///
9/// Variants use Rust CamelCase in 4.0. They intentionally do not carry the
10/// `Section` suffix; that suffix belongs to parser types such as
11/// [`crate::tables::nit::NitSection`].
12///
13/// Tables that occupy a range of values (EIT schedule 0x50..=0x5F and 0x60..=0x6F)
14/// are not listed as enum variants; see [`Self::eit_schedule_actual_segment`] and
15/// [`Self::eit_schedule_other_segment`] instead.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize))]
18#[repr(u8)]
19#[allow(missing_docs)]
20#[non_exhaustive]
21#[rustfmt::skip] // deliberate column-aligned discriminants
22pub enum TableId {
23    // ── MPEG-2 tables ──────────────────────────────────────────────────────
24    Pat                        = 0x00,
25    Cat                        = 0x01,
26    Pmt                        = 0x02,
27    TransportStreamDescription = 0x03,
28
29    // ── DVB SI tables ──────────────────────────────────────────────────────
30    NetworkInformationActual   = 0x40,
31    NetworkInformationOther    = 0x41,
32    ServiceDescriptionActual   = 0x42,
33    ServiceDescriptionOther    = 0x46,
34    BouquetAssociation         = 0x4A,
35    UpdateNotification         = 0x4B,
36    IpMacNotification          = 0x4C,
37    SatelliteAccess            = 0x4D,
38    EventInformationPfActual   = 0x4E,
39    EventInformationPfOther    = 0x4F,
40
41    // EIT schedule actual covers 0x50..=0x5F (16 segments).
42    // EIT schedule other covers 0x60..=0x6F.
43
44    TimeAndDate                = 0x70,
45    RunningStatus              = 0x71,
46    Stuffing                   = 0x72,
47    TimeOffset                 = 0x73,
48    ApplicationInformation     = 0x74,
49    Container                  = 0x75,
50    RelatedContent             = 0x76,
51    ContentIdentifier          = 0x77,
52    MpeFec                     = 0x78,
53    ResolutionNotification     = 0x79,
54    MpeIfec                    = 0x7A,
55    ProtectionMessage          = 0x7B,
56    DownloadableFontInfo       = 0x7C,
57    DiscontinuityInformation   = 0x7E,
58    SelectionInformation       = 0x7F,
59}
60
61impl TableId {
62    /// If `v` is an EIT-schedule-actual `table_id` (0x50..=0x5F), return its
63    /// segment index 0..=15. Otherwise `None`.
64    #[must_use]
65    pub const fn eit_schedule_actual_segment(v: u8) -> Option<u8> {
66        if v >= 0x50 && v <= 0x5F {
67            Some(v - 0x50)
68        } else {
69            None
70        }
71    }
72
73    /// If `v` is an EIT-schedule-other `table_id` (0x60..=0x6F), return its
74    /// segment index 0..=15. Otherwise `None`.
75    #[must_use]
76    pub const fn eit_schedule_other_segment(v: u8) -> Option<u8> {
77        if v >= 0x60 && v <= 0x6F {
78            Some(v - 0x60)
79        } else {
80            None
81        }
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn known_values_round_trip() {
91        for id in [
92            TableId::Pat,
93            TableId::NetworkInformationActual,
94            TableId::ServiceDescriptionActual,
95            TableId::EventInformationPfActual,
96            TableId::TimeAndDate,
97            TableId::SelectionInformation,
98        ] {
99            let byte = id as u8;
100            assert_eq!(TableId::try_from(byte), Ok(id));
101        }
102    }
103
104    #[test]
105    fn eit_schedule_actual_segment_range() {
106        assert_eq!(TableId::eit_schedule_actual_segment(0x4F), None);
107        assert_eq!(TableId::eit_schedule_actual_segment(0x50), Some(0));
108        assert_eq!(TableId::eit_schedule_actual_segment(0x5F), Some(0x0F));
109        assert_eq!(TableId::eit_schedule_actual_segment(0x60), None);
110    }
111
112    #[test]
113    fn eit_schedule_other_segment_range() {
114        assert_eq!(TableId::eit_schedule_other_segment(0x5F), None);
115        assert_eq!(TableId::eit_schedule_other_segment(0x60), Some(0));
116        assert_eq!(TableId::eit_schedule_other_segment(0x6F), Some(0x0F));
117        assert_eq!(TableId::eit_schedule_other_segment(0x70), None);
118    }
119
120    #[test]
121    fn unknown_value_rejected() {
122        assert!(TableId::try_from(0x99).is_err());
123    }
124
125    #[test]
126    fn exhaustive_byte_sweep() {
127        let mut matched = 0u16;
128        for byte in 0u8..=0xFF {
129            if let Ok(id) = TableId::try_from(byte) {
130                assert_eq!(id as u8, byte, "round-trip failed for {byte:#04x}");
131                matched += 1;
132            }
133        }
134        // 29 defined variants (added ProtectionMessage 0x7B, DownloadableFontInfo 0x7C)
135        assert_eq!(matched, 29, "expected 29 matched variants");
136    }
137}