dvb_si/descriptors/
data_broadcast_id.rs1use crate::error::{Error, Result};
8use crate::traits::Descriptor;
9use dvb_common::{Parse, Serialize};
10
11pub const TAG: u8 = 0x66;
13const HEADER_LEN: usize = 2;
14const ID_LEN: usize = 2;
16
17#[derive(Debug, Clone, PartialEq, Eq)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize))]
20pub struct DataBroadcastIdDescriptor<'a> {
21 pub data_broadcast_id: u16,
23 pub id_selector: &'a [u8],
25}
26
27impl<'a> Parse<'a> for DataBroadcastIdDescriptor<'a> {
28 type Error = crate::error::Error;
29 fn parse(bytes: &'a [u8]) -> Result<Self> {
30 if bytes.len() < HEADER_LEN {
31 return Err(Error::BufferTooShort {
32 need: HEADER_LEN,
33 have: bytes.len(),
34 what: "DataBroadcastIdDescriptor header",
35 });
36 }
37 if bytes[0] != TAG {
38 return Err(Error::InvalidDescriptor {
39 tag: bytes[0],
40 reason: "unexpected tag for data_broadcast_id_descriptor",
41 });
42 }
43 let length = bytes[1] as usize;
44 let end = HEADER_LEN + length;
45 if bytes.len() < end {
46 return Err(Error::BufferTooShort {
47 need: end,
48 have: bytes.len(),
49 what: "DataBroadcastIdDescriptor body",
50 });
51 }
52 if length < ID_LEN {
53 return Err(Error::InvalidDescriptor {
54 tag: TAG,
55 reason: "data_broadcast_id_descriptor body shorter than 2 bytes",
56 });
57 }
58 let data_broadcast_id = u16::from_be_bytes([bytes[HEADER_LEN], bytes[HEADER_LEN + 1]]);
59 let id_selector = &bytes[HEADER_LEN + ID_LEN..end];
60 Ok(Self {
61 data_broadcast_id,
62 id_selector,
63 })
64 }
65}
66
67impl Serialize for DataBroadcastIdDescriptor<'_> {
68 type Error = crate::error::Error;
69 fn serialized_len(&self) -> usize {
70 HEADER_LEN + ID_LEN + self.id_selector.len()
71 }
72
73 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
74 let len = self.serialized_len();
75 let body = ID_LEN + self.id_selector.len();
76 if body > u8::MAX as usize {
77 return Err(Error::InvalidDescriptor {
78 tag: TAG,
79 reason: "data_broadcast_id_descriptor body exceeds 255 bytes",
80 });
81 }
82 if buf.len() < len {
83 return Err(Error::OutputBufferTooSmall {
84 need: len,
85 have: buf.len(),
86 });
87 }
88 buf[0] = TAG;
89 buf[1] = body as u8;
90 buf[HEADER_LEN..HEADER_LEN + ID_LEN].copy_from_slice(&self.data_broadcast_id.to_be_bytes());
91 let sel_start = HEADER_LEN + ID_LEN;
92 buf[sel_start..sel_start + self.id_selector.len()].copy_from_slice(self.id_selector);
93 Ok(len)
94 }
95}
96
97impl<'a> Descriptor<'a> for DataBroadcastIdDescriptor<'a> {
98 const TAG: u8 = TAG;
99 fn descriptor_length(&self) -> u8 {
100 (ID_LEN + self.id_selector.len()) as u8
101 }
102}
103
104impl<'a> crate::traits::DescriptorDef<'a> for DataBroadcastIdDescriptor<'a> {
105 const TAG: u8 = TAG;
106 const NAME: &'static str = "DATA_BROADCAST_ID";
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112
113 #[test]
114 fn parse_extracts_id_and_selector() {
115 let bytes = [TAG, 0x05, 0x00, 0x0B, 0xAA, 0xBB, 0xCC];
116 let d = DataBroadcastIdDescriptor::parse(&bytes).unwrap();
117 assert_eq!(d.data_broadcast_id, 0x000B);
118 assert_eq!(d.id_selector, &[0xAA, 0xBB, 0xCC]);
119 }
120
121 #[test]
122 fn parse_accepts_empty_selector() {
123 let bytes = [TAG, 0x02, 0x00, 0x0A];
124 let d = DataBroadcastIdDescriptor::parse(&bytes).unwrap();
125 assert_eq!(d.data_broadcast_id, 0x000A);
126 assert!(d.id_selector.is_empty());
127 }
128
129 #[test]
130 fn parse_rejects_wrong_tag() {
131 let err = DataBroadcastIdDescriptor::parse(&[0x65, 0x02, 0x00, 0x0A]).unwrap_err();
132 assert!(matches!(err, Error::InvalidDescriptor { tag: 0x65, .. }));
133 }
134
135 #[test]
136 fn parse_rejects_short_buffer() {
137 let err = DataBroadcastIdDescriptor::parse(&[TAG]).unwrap_err();
138 assert!(matches!(err, Error::BufferTooShort { .. }));
139 }
140
141 #[test]
142 fn parse_rejects_body_too_short() {
143 let err = DataBroadcastIdDescriptor::parse(&[TAG, 0x01, 0x00]).unwrap_err();
145 assert!(matches!(err, Error::InvalidDescriptor { .. }));
146 }
147
148 #[test]
149 fn parse_rejects_length_overrun() {
150 let err = DataBroadcastIdDescriptor::parse(&[TAG, 0x05, 0x00, 0x0B, 0xAA]).unwrap_err();
152 assert!(matches!(err, Error::BufferTooShort { .. }));
153 }
154
155 #[test]
156 fn serialize_round_trip() {
157 let d = DataBroadcastIdDescriptor {
158 data_broadcast_id: 0x0123,
159 id_selector: &[0xDE, 0xAD, 0xBE, 0xEF],
160 };
161 let mut buf = vec![0u8; d.serialized_len()];
162 d.serialize_into(&mut buf).unwrap();
163 let re = DataBroadcastIdDescriptor::parse(&buf).unwrap();
164 assert_eq!(d, re);
165 }
166
167 #[test]
168 fn serialize_rejects_too_small_buffer() {
169 let d = DataBroadcastIdDescriptor {
170 data_broadcast_id: 0x0001,
171 id_selector: &[0x01],
172 };
173 let mut tiny = [0u8; 2];
174 let err = d.serialize_into(&mut tiny).unwrap_err();
175 assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
176 }
177
178 #[test]
179 fn serialize_rejects_over_range_body() {
180 let sel = vec![0u8; 254];
182 let d = DataBroadcastIdDescriptor {
183 data_broadcast_id: 0x0001,
184 id_selector: &sel,
185 };
186 let mut buf = vec![0u8; d.serialized_len()];
187 let err = d.serialize_into(&mut buf).unwrap_err();
188 assert!(matches!(err, Error::InvalidDescriptor { tag: TAG, .. }));
189 }
190
191 #[cfg(feature = "serde")]
192 #[test]
193 fn serde_serialize_is_stable() {
194 let d = DataBroadcastIdDescriptor {
198 data_broadcast_id: 0x000B,
199 id_selector: &[0x01, 0x02],
200 };
201 let json = serde_json::to_string(&d).unwrap();
202 assert_eq!(json, serde_json::to_string(&d.clone()).unwrap());
203 }
204}