Skip to main content

dvb_si/descriptors/
content_time_base_indicator.rs

1//! Content_time_base_indicator — ISO/IEC 13818-1 §2.6.57, Table 2-85.
2//!
3//! 4-bit field in the content_labeling_descriptor that selects the
4//! time-base semantics for the associated content.
5
6/// Content_time_base_indicator values (Table 2-85).
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize))]
9#[non_exhaustive]
10pub enum ContentTimeBaseIndicator {
11    /// 0 — No content time base defined in this descriptor.
12    None,
13    /// 1 — STC (System Time Clock).
14    Stc,
15    /// 2 — NPT (Normal Play Time).
16    Npt,
17    /// 3–7 — Reserved.
18    Reserved(u8),
19    /// 8–15 — Privately defined.
20    Private(u8),
21}
22
23impl ContentTimeBaseIndicator {
24    /// Construct from a raw 4-bit nibble.
25    #[must_use]
26    pub fn from_u8(v: u8) -> Self {
27        match v {
28            0 => Self::None,
29            1 => Self::Stc,
30            2 => Self::Npt,
31            3..=7 => Self::Reserved(v),
32            v => Self::Private(v),
33        }
34    }
35
36    /// Return the raw 4-bit value.
37    #[must_use]
38    pub fn to_u8(self) -> u8 {
39        match self {
40            Self::None => 0,
41            Self::Stc => 1,
42            Self::Npt => 2,
43            Self::Reserved(v) | Self::Private(v) => v,
44        }
45    }
46
47    /// Returns a human-readable spec name for this value.
48    #[must_use]
49    pub fn name(self) -> &'static str {
50        match self {
51            Self::None => "none",
52            Self::Stc => "STC",
53            Self::Npt => "NPT",
54            Self::Reserved(_) => "reserved",
55            Self::Private(_) => "private",
56        }
57    }
58}
59dvb_common::impl_spec_display!(ContentTimeBaseIndicator, Reserved, Private);
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn round_trip_values() {
67        for v in 0u8..=15 {
68            let indicator = ContentTimeBaseIndicator::from_u8(v);
69            assert_eq!(indicator.to_u8(), v, "value {v} round-trip mismatch");
70        }
71    }
72}