dvb_si/descriptors/
private_data_specifier.rs1use super::descriptor_body;
8use crate::error::{Error, Result};
9use dvb_common::{Parse, Serialize};
10
11pub const TAG: u8 = 0x5F;
13const HEADER_LEN: usize = 2;
14const BODY_LEN: u8 = 4;
16
17#[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#[derive(Debug, Clone, PartialEq, Eq)]
28#[cfg_attr(feature = "serde", derive(serde::Serialize))]
29pub struct PrivateDataSpecifierDescriptor {
30 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 (hdr, _) = body
50 .split_first_chunk::<4>()
51 .ok_or(Error::InvalidDescriptor {
52 tag: TAG,
53 reason: "private_data_specifier_descriptor length must equal 4",
54 })?;
55 let private_data_specifier = u32::from_be_bytes(*hdr);
56 Ok(Self {
57 private_data_specifier,
58 })
59 }
60}
61
62impl Serialize for PrivateDataSpecifierDescriptor {
63 type Error = crate::error::Error;
64 fn serialized_len(&self) -> usize {
65 HEADER_LEN + BODY_LEN as usize
66 }
67
68 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
69 let len = self.serialized_len();
70 if buf.len() < len {
71 return Err(Error::OutputBufferTooSmall {
72 need: len,
73 have: buf.len(),
74 });
75 }
76 buf[0] = TAG;
77 buf[1] = BODY_LEN;
78 buf[HEADER_LEN..HEADER_LEN + 4].copy_from_slice(&self.private_data_specifier.to_be_bytes());
79 Ok(len)
80 }
81}
82impl<'a> crate::traits::DescriptorDef<'a> for PrivateDataSpecifierDescriptor {
83 const TAG: u8 = TAG;
84 const NAME: &'static str = "PRIVATE_DATA_SPECIFIER";
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90
91 #[test]
92 fn parse_extracts_specifier() {
93 let bytes = [TAG, 4, 0x00, 0x00, 0x00, 0x28];
94 let d = PrivateDataSpecifierDescriptor::parse(&bytes).unwrap();
95 assert_eq!(d.private_data_specifier, 0x0000_0028);
96 }
97
98 #[test]
99 fn parse_rejects_wrong_tag() {
100 let err = PrivateDataSpecifierDescriptor::parse(&[0x60, 4, 0, 0, 0, 0]).unwrap_err();
101 assert!(matches!(err, Error::InvalidDescriptor { tag: 0x60, .. }));
102 }
103
104 #[test]
105 fn parse_rejects_short_buffer() {
106 let err = PrivateDataSpecifierDescriptor::parse(&[TAG]).unwrap_err();
107 assert!(matches!(err, Error::BufferTooShort { .. }));
108 }
109
110 #[test]
111 fn parse_rejects_truncated_body() {
112 let err = PrivateDataSpecifierDescriptor::parse(&[TAG, 4, 0, 0]).unwrap_err();
114 assert!(matches!(err, Error::BufferTooShort { .. }));
115 }
116
117 #[test]
118 fn parse_rejects_wrong_length() {
119 let err = PrivateDataSpecifierDescriptor::parse(&[TAG, 3, 0, 0, 0]).unwrap_err();
120 assert!(matches!(err, Error::InvalidDescriptor { .. }));
121 }
122
123 #[test]
124 fn serialize_round_trip() {
125 let d = PrivateDataSpecifierDescriptor {
126 private_data_specifier: 0xDEAD_BEEF,
127 };
128 let mut buf = vec![0u8; d.serialized_len()];
129 d.serialize_into(&mut buf).unwrap();
130 let re = PrivateDataSpecifierDescriptor::parse(&buf).unwrap();
131 assert_eq!(d, re);
132 }
133
134 #[test]
135 fn serialize_rejects_too_small_buffer() {
136 let d = PrivateDataSpecifierDescriptor {
137 private_data_specifier: 1,
138 };
139 let mut tiny = [0u8; 3];
140 let err = d.serialize_into(&mut tiny).unwrap_err();
141 assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
142 }
143
144 #[test]
145 fn descriptor_length_matches_payload() {
146 let d = PrivateDataSpecifierDescriptor {
147 private_data_specifier: 0,
148 };
149 assert_eq!(d.serialized_len() - 2, 4);
150 }
151
152 #[cfg(feature = "serde")]
153 #[test]
154 fn serde_round_trip() {
155 let d = PrivateDataSpecifierDescriptor {
156 private_data_specifier: 0x0000_233A,
157 };
158 let json = serde_json::to_string(&d).unwrap();
159 let _v: serde_json::Value = serde_json::from_str(&json).unwrap();
161 }
162
163 #[test]
164 fn specifier_name_exact_entry() {
165 assert_eq!(private_data_specifier_name(0x0000_0001), Some("SES Astra"));
167 assert_eq!(
168 private_data_specifier_name(0x0000_0005),
169 Some("ARD, ZDF, ORF")
170 );
171 }
172
173 #[test]
174 fn specifier_name_range_entry() {
175 assert_eq!(private_data_specifier_name(0x0000_0002), Some("BskyB"));
177 assert_eq!(private_data_specifier_name(0x0000_0004), Some("BskyB"));
178 }
179
180 #[test]
181 fn specifier_name_unknown() {
182 assert_eq!(private_data_specifier_name(0x0000_0000), None);
183 assert_eq!(private_data_specifier_name(0x0000_003C), None);
184 assert_eq!(private_data_specifier_name(0xDEAD_BEEF), None);
185 }
186}