Skip to main content

dvb_si/descriptors/
metadata_format.rs

1//! Metadata_format — ISO/IEC 13818-1 §2.6.59, Table 2-87.
2//!
3//! 8-bit field that identifies the metadata encoding format.
4
5/// Metadata_format values (Table 2-87).
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize))]
8#[non_exhaustive]
9pub enum MetadataFormat {
10    /// 0x00–0x0F — Reserved.
11    Reserved0(u8),
12    /// 0x10 — ISO/IEC 15938-1 TeM.
13    TeM,
14    /// 0x11 — ISO/IEC 15938-1 BiM.
15    BiM,
16    /// 0x12–0x3E — Reserved.
17    Reserved1(u8),
18    /// 0x3F — Defined by metadata application format.
19    AppFormat,
20    /// 0x40–0xFE — Private use.
21    Private(u8),
22    /// 0xFF — Defined by metadata_format_identifier field.
23    Identifier,
24}
25
26impl MetadataFormat {
27    /// Construct from a raw byte.
28    #[must_use]
29    pub fn from_u8(v: u8) -> Self {
30        match v {
31            0x00..=0x0F => Self::Reserved0(v),
32            0x10 => Self::TeM,
33            0x11 => Self::BiM,
34            0x12..=0x3E => Self::Reserved1(v),
35            0x3F => Self::AppFormat,
36            0x40..=0xFE => Self::Private(v),
37            0xFF => Self::Identifier,
38        }
39    }
40
41    /// Return the raw byte value.
42    #[must_use]
43    pub fn to_u8(self) -> u8 {
44        match self {
45            Self::Reserved0(v) => v,
46            Self::TeM => 0x10,
47            Self::BiM => 0x11,
48            Self::Reserved1(v) => v,
49            Self::AppFormat => 0x3F,
50            Self::Private(v) => v,
51            Self::Identifier => 0xFF,
52        }
53    }
54
55    /// Returns a human-readable spec name for this value.
56    #[must_use]
57    pub fn name(self) -> &'static str {
58        match self {
59            Self::Reserved0(_) => "reserved",
60            Self::TeM => "TeM",
61            Self::BiM => "BiM",
62            Self::Reserved1(_) => "reserved",
63            Self::AppFormat => "app format",
64            Self::Private(_) => "private",
65            Self::Identifier => "identifier",
66        }
67    }
68}
69dvb_common::impl_spec_display!(MetadataFormat, Reserved0, Reserved1, Private);
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn round_trip_values() {
77        let values = [0x00u8, 0x05, 0x10, 0x11, 0x20, 0x3F, 0x80, 0xFE, 0xFF];
78        for v in values {
79            let mf = MetadataFormat::from_u8(v);
80            assert_eq!(mf.to_u8(), v, "value 0x{v:02X} round-trip mismatch");
81        }
82    }
83}