dvb_si/descriptors/
country_availability.rs1use crate::error::{Error, Result};
10use crate::text::LangCode;
11use crate::traits::Descriptor;
12use dvb_common::{Parse, Serialize};
13
14pub const TAG: u8 = 0x49;
16const HEADER_LEN: usize = 2;
17const FLAG_LEN: usize = 1;
18const COUNTRY_CODE_LEN: usize = 3;
19const MIN_BODY_LEN: usize = FLAG_LEN;
20const MAX_BODY_LEN: usize = u8::MAX as usize;
22const AVAILABILITY_FLAG_MASK: u8 = 0x80;
24const RESERVED_MASK: u8 = 0x7F;
26
27#[derive(Debug, Clone, PartialEq, Eq)]
29#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
30pub struct CountryAvailabilityDescriptor {
31 pub country_availability_flag: bool,
34 pub country_codes: Vec<LangCode>,
36}
37
38impl<'a> Parse<'a> for CountryAvailabilityDescriptor {
39 type Error = crate::error::Error;
40 fn parse(bytes: &'a [u8]) -> Result<Self> {
41 if bytes.len() < HEADER_LEN {
42 return Err(Error::BufferTooShort {
43 need: HEADER_LEN,
44 have: bytes.len(),
45 what: "CountryAvailabilityDescriptor header",
46 });
47 }
48 if bytes[0] != TAG {
49 return Err(Error::InvalidDescriptor {
50 tag: bytes[0],
51 reason: "unexpected tag for country_availability_descriptor",
52 });
53 }
54 let length = bytes[1] as usize;
55 if length < MIN_BODY_LEN {
56 return Err(Error::InvalidDescriptor {
57 tag: TAG,
58 reason: "country_availability_descriptor missing flag byte",
59 });
60 }
61 if (length - FLAG_LEN) % COUNTRY_CODE_LEN != 0 {
62 return Err(Error::InvalidDescriptor {
63 tag: TAG,
64 reason: "country_code loop length must be a multiple of 3",
65 });
66 }
67 let end = HEADER_LEN + length;
68 if bytes.len() < end {
69 return Err(Error::BufferTooShort {
70 need: end,
71 have: bytes.len(),
72 what: "CountryAvailabilityDescriptor body",
73 });
74 }
75 let flags = bytes[HEADER_LEN];
76 let country_availability_flag = flags & AVAILABILITY_FLAG_MASK != 0;
78 let loop_body = &bytes[HEADER_LEN + FLAG_LEN..end];
79 let mut country_codes = Vec::with_capacity(loop_body.len() / COUNTRY_CODE_LEN);
80 for chunk in loop_body.chunks_exact(COUNTRY_CODE_LEN) {
81 country_codes.push(LangCode([chunk[0], chunk[1], chunk[2]]));
82 }
83 Ok(Self {
84 country_availability_flag,
85 country_codes,
86 })
87 }
88}
89
90impl Serialize for CountryAvailabilityDescriptor {
91 type Error = crate::error::Error;
92 fn serialized_len(&self) -> usize {
93 HEADER_LEN + FLAG_LEN + COUNTRY_CODE_LEN * self.country_codes.len()
94 }
95
96 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
97 let len = self.serialized_len();
98 if buf.len() < len {
99 return Err(Error::OutputBufferTooSmall {
100 need: len,
101 have: buf.len(),
102 });
103 }
104 let body_len = FLAG_LEN + COUNTRY_CODE_LEN * self.country_codes.len();
105 if body_len > MAX_BODY_LEN {
107 return Err(Error::SectionLengthOverflow {
108 declared: body_len,
109 available: MAX_BODY_LEN,
110 });
111 }
112 buf[0] = TAG;
113 buf[1] = body_len as u8;
114 let flag_bit = if self.country_availability_flag {
116 AVAILABILITY_FLAG_MASK
117 } else {
118 0
119 };
120 buf[HEADER_LEN] = flag_bit | RESERVED_MASK;
121 let mut pos = HEADER_LEN + FLAG_LEN;
122 for code in &self.country_codes {
123 buf[pos..pos + COUNTRY_CODE_LEN].copy_from_slice(&code.0);
124 pos += COUNTRY_CODE_LEN;
125 }
126 Ok(len)
127 }
128}
129
130impl<'a> Descriptor<'a> for CountryAvailabilityDescriptor {
131 const TAG: u8 = TAG;
132 fn descriptor_length(&self) -> u8 {
133 (FLAG_LEN + COUNTRY_CODE_LEN * self.country_codes.len()) as u8
134 }
135}
136
137impl<'a> crate::traits::DescriptorDef<'a> for CountryAvailabilityDescriptor {
138 const TAG: u8 = TAG;
139 const NAME: &'static str = "COUNTRY_AVAILABILITY";
140}
141
142#[cfg(test)]
143mod tests {
144 use super::*;
145
146 #[test]
147 fn parse_available_with_countries() {
148 let bytes = [TAG, 7, 0xFF, b'G', b'B', b'R', b'F', b'R', b'A'];
150 let d = CountryAvailabilityDescriptor::parse(&bytes).unwrap();
151 assert!(d.country_availability_flag);
152 assert_eq!(d.country_codes, vec![LangCode(*b"GBR"), LangCode(*b"FRA")]);
153 }
154
155 #[test]
156 fn parse_flag_clear() {
157 let bytes = [TAG, 4, 0x7F, b'D', b'E', b'U'];
158 let d = CountryAvailabilityDescriptor::parse(&bytes).unwrap();
159 assert!(!d.country_availability_flag);
160 assert_eq!(d.country_codes, vec![LangCode(*b"DEU")]);
161 }
162
163 #[test]
164 fn parse_flag_only_no_countries() {
165 let bytes = [TAG, 1, 0x80];
166 let d = CountryAvailabilityDescriptor::parse(&bytes).unwrap();
167 assert!(d.country_availability_flag);
168 assert!(d.country_codes.is_empty());
169 }
170
171 #[test]
172 fn parse_rejects_wrong_tag() {
173 assert!(matches!(
174 CountryAvailabilityDescriptor::parse(&[0x4A, 1, 0x80]).unwrap_err(),
175 Error::InvalidDescriptor { tag: 0x4A, .. }
176 ));
177 }
178
179 #[test]
180 fn parse_rejects_short_buffer() {
181 let bytes = [TAG, 7, 0x80, b'G', b'B'];
183 assert!(matches!(
184 CountryAvailabilityDescriptor::parse(&bytes).unwrap_err(),
185 Error::BufferTooShort { .. }
186 ));
187 }
188
189 #[test]
190 fn parse_rejects_loop_not_multiple_of_3() {
191 let bytes = [TAG, 3, 0x80, b'G', b'B'];
193 assert!(matches!(
194 CountryAvailabilityDescriptor::parse(&bytes).unwrap_err(),
195 Error::InvalidDescriptor { tag: TAG, .. }
196 ));
197 }
198
199 #[test]
200 fn parse_rejects_zero_length() {
201 let bytes = [TAG, 0];
202 assert!(matches!(
203 CountryAvailabilityDescriptor::parse(&bytes).unwrap_err(),
204 Error::InvalidDescriptor { tag: TAG, .. }
205 ));
206 }
207
208 #[test]
209 fn serialize_round_trip() {
210 let d = CountryAvailabilityDescriptor {
211 country_availability_flag: true,
212 country_codes: vec![LangCode(*b"GBR"), LangCode(*b"IRL")],
213 };
214 let mut buf = vec![0u8; d.serialized_len()];
215 d.serialize_into(&mut buf).unwrap();
216 assert_eq!(CountryAvailabilityDescriptor::parse(&buf).unwrap(), d);
217 }
218
219 #[test]
220 fn serialize_emits_reserved_ones() {
221 let d = CountryAvailabilityDescriptor {
222 country_availability_flag: false,
223 country_codes: vec![],
224 };
225 let mut buf = vec![0u8; d.serialized_len()];
226 d.serialize_into(&mut buf).unwrap();
227 assert_eq!(buf[HEADER_LEN], 0x7F);
229 }
230
231 #[cfg(feature = "serde")]
232 #[test]
233 fn serde_round_trip() {
234 let d = CountryAvailabilityDescriptor {
235 country_availability_flag: true,
236 country_codes: vec![LangCode(*b"FRA")],
237 };
238 let json = serde_json::to_string(&d).unwrap();
239 let back: CountryAvailabilityDescriptor = serde_json::from_str(&json).unwrap();
240 assert_eq!(d, back);
241 }
242}