Skip to main content

dvb_si/descriptors/
private_data_specifier.rs

1//! Private Data Specifier Descriptor — ETSI EN 300 468 §6.2.31 (tag 0x5F).
2//!
3//! Table 85 (PDF p. 98). A single 32-bit `private_data_specifier` value
4//! (registered in EN 300 468 Annex/TR 101 162) that scopes the interpretation
5//! of any subsequent private (user-defined) descriptors in the same loop.
6
7use super::descriptor_body;
8use crate::error::{Error, Result};
9use dvb_common::{Parse, Serialize};
10
11/// Descriptor tag for private_data_specifier_descriptor.
12pub const TAG: u8 = 0x5F;
13const HEADER_LEN: usize = 2;
14/// Fixed payload length: a single 32-bit specifier (EN 300 468 Table 85).
15const BODY_LEN: u8 = 4;
16
17/// Best-effort, non-exhaustive mapping from a 32-bit `private_data_specifier`
18/// to a human-readable organization name.  Generated at build time from
19/// vendored TSDuck `.names` data (`registries/tsPDS.names`); attribution in
20/// `registries/README.md`.
21#[must_use]
22pub fn private_data_specifier_name(v: u32) -> Option<&'static str> {
23    crate::registry_names::private_data_specifier_name_generated(v)
24}
25
26/// Private Data Specifier Descriptor (tag 0x5F).
27#[derive(Debug, Clone, PartialEq, Eq)]
28#[cfg_attr(feature = "serde", derive(serde::Serialize))]
29pub struct PrivateDataSpecifierDescriptor {
30    /// 32-bit registered private_data_specifier (ETSI Table 85, PDF p. 98).
31    pub private_data_specifier: u32,
32}
33
34impl<'a> Parse<'a> for PrivateDataSpecifierDescriptor {
35    type Error = crate::error::Error;
36    fn parse(bytes: &'a [u8]) -> Result<Self> {
37        let body = descriptor_body(
38            bytes,
39            TAG,
40            "PrivateDataSpecifierDescriptor",
41            "unexpected tag for private_data_specifier_descriptor",
42        )?;
43        if body.len() != BODY_LEN as usize {
44            return Err(Error::InvalidDescriptor {
45                tag: TAG,
46                reason: "private_data_specifier_descriptor length must equal 4",
47            });
48        }
49        let private_data_specifier = u32::from_be_bytes([body[0], body[1], body[2], body[3]]);
50        Ok(Self {
51            private_data_specifier,
52        })
53    }
54}
55
56impl Serialize for PrivateDataSpecifierDescriptor {
57    type Error = crate::error::Error;
58    fn serialized_len(&self) -> usize {
59        HEADER_LEN + BODY_LEN as usize
60    }
61
62    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
63        let len = self.serialized_len();
64        if buf.len() < len {
65            return Err(Error::OutputBufferTooSmall {
66                need: len,
67                have: buf.len(),
68            });
69        }
70        buf[0] = TAG;
71        buf[1] = BODY_LEN;
72        buf[HEADER_LEN..HEADER_LEN + 4].copy_from_slice(&self.private_data_specifier.to_be_bytes());
73        Ok(len)
74    }
75}
76impl<'a> crate::traits::DescriptorDef<'a> for PrivateDataSpecifierDescriptor {
77    const TAG: u8 = TAG;
78    const NAME: &'static str = "PRIVATE_DATA_SPECIFIER";
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn parse_extracts_specifier() {
87        let bytes = [TAG, 4, 0x00, 0x00, 0x00, 0x28];
88        let d = PrivateDataSpecifierDescriptor::parse(&bytes).unwrap();
89        assert_eq!(d.private_data_specifier, 0x0000_0028);
90    }
91
92    #[test]
93    fn parse_rejects_wrong_tag() {
94        let err = PrivateDataSpecifierDescriptor::parse(&[0x60, 4, 0, 0, 0, 0]).unwrap_err();
95        assert!(matches!(err, Error::InvalidDescriptor { tag: 0x60, .. }));
96    }
97
98    #[test]
99    fn parse_rejects_short_buffer() {
100        let err = PrivateDataSpecifierDescriptor::parse(&[TAG]).unwrap_err();
101        assert!(matches!(err, Error::BufferTooShort { .. }));
102    }
103
104    #[test]
105    fn parse_rejects_truncated_body() {
106        // length=4 but only 2 payload bytes present.
107        let err = PrivateDataSpecifierDescriptor::parse(&[TAG, 4, 0, 0]).unwrap_err();
108        assert!(matches!(err, Error::BufferTooShort { .. }));
109    }
110
111    #[test]
112    fn parse_rejects_wrong_length() {
113        let err = PrivateDataSpecifierDescriptor::parse(&[TAG, 3, 0, 0, 0]).unwrap_err();
114        assert!(matches!(err, Error::InvalidDescriptor { .. }));
115    }
116
117    #[test]
118    fn serialize_round_trip() {
119        let d = PrivateDataSpecifierDescriptor {
120            private_data_specifier: 0xDEAD_BEEF,
121        };
122        let mut buf = vec![0u8; d.serialized_len()];
123        d.serialize_into(&mut buf).unwrap();
124        let re = PrivateDataSpecifierDescriptor::parse(&buf).unwrap();
125        assert_eq!(d, re);
126    }
127
128    #[test]
129    fn serialize_rejects_too_small_buffer() {
130        let d = PrivateDataSpecifierDescriptor {
131            private_data_specifier: 1,
132        };
133        let mut tiny = [0u8; 3];
134        let err = d.serialize_into(&mut tiny).unwrap_err();
135        assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
136    }
137
138    #[test]
139    fn descriptor_length_matches_payload() {
140        let d = PrivateDataSpecifierDescriptor {
141            private_data_specifier: 0,
142        };
143        assert_eq!(d.serialized_len() - 2, 4);
144    }
145
146    #[cfg(feature = "serde")]
147    #[test]
148    fn serde_round_trip() {
149        let d = PrivateDataSpecifierDescriptor {
150            private_data_specifier: 0x0000_233A,
151        };
152        let json = serde_json::to_string(&d).unwrap();
153        // Serialize-only: assert the emitted JSON re-parses (serialize-stable).
154        let _v: serde_json::Value = serde_json::from_str(&json).unwrap();
155    }
156
157    #[test]
158    fn specifier_name_exact_entry() {
159        // Exact entry from tsPDS.names [PrivateDataSpecifier].
160        assert_eq!(private_data_specifier_name(0x0000_0001), Some("SES Astra"));
161        assert_eq!(
162            private_data_specifier_name(0x0000_0005),
163            Some("ARD, ZDF, ORF")
164        );
165    }
166
167    #[test]
168    fn specifier_name_range_entry() {
169        // Range entry 0x00000002-0x00000004 => "BskyB" from tsPDS.names.
170        assert_eq!(private_data_specifier_name(0x0000_0002), Some("BskyB"));
171        assert_eq!(private_data_specifier_name(0x0000_0004), Some("BskyB"));
172    }
173
174    #[test]
175    fn specifier_name_unknown() {
176        assert_eq!(private_data_specifier_name(0x0000_0000), None);
177        assert_eq!(private_data_specifier_name(0x0000_003C), None);
178        assert_eq!(private_data_specifier_name(0xDEAD_BEEF), None);
179    }
180}