use crate::error::{Error, Result};
use crate::text::LangCode;
use crate::traits::Descriptor;
use dvb_common::{Parse, Serialize};
pub const TAG: u8 = 0x49;
const HEADER_LEN: usize = 2;
const FLAG_LEN: usize = 1;
const COUNTRY_CODE_LEN: usize = 3;
const MIN_BODY_LEN: usize = FLAG_LEN;
const MAX_BODY_LEN: usize = u8::MAX as usize;
const AVAILABILITY_FLAG_MASK: u8 = 0x80;
const RESERVED_MASK: u8 = 0x7F;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CountryAvailabilityDescriptor {
pub country_availability_flag: bool,
pub country_codes: Vec<LangCode>,
}
impl<'a> Parse<'a> for CountryAvailabilityDescriptor {
type Error = crate::error::Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
if bytes.len() < HEADER_LEN {
return Err(Error::BufferTooShort {
need: HEADER_LEN,
have: bytes.len(),
what: "CountryAvailabilityDescriptor header",
});
}
if bytes[0] != TAG {
return Err(Error::InvalidDescriptor {
tag: bytes[0],
reason: "unexpected tag for country_availability_descriptor",
});
}
let length = bytes[1] as usize;
if length < MIN_BODY_LEN {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "country_availability_descriptor missing flag byte",
});
}
if (length - FLAG_LEN) % COUNTRY_CODE_LEN != 0 {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "country_code loop length must be a multiple of 3",
});
}
let end = HEADER_LEN + length;
if bytes.len() < end {
return Err(Error::BufferTooShort {
need: end,
have: bytes.len(),
what: "CountryAvailabilityDescriptor body",
});
}
let flags = bytes[HEADER_LEN];
let country_availability_flag = flags & AVAILABILITY_FLAG_MASK != 0;
let loop_body = &bytes[HEADER_LEN + FLAG_LEN..end];
let mut country_codes = Vec::with_capacity(loop_body.len() / COUNTRY_CODE_LEN);
for chunk in loop_body.chunks_exact(COUNTRY_CODE_LEN) {
country_codes.push(LangCode([chunk[0], chunk[1], chunk[2]]));
}
Ok(Self {
country_availability_flag,
country_codes,
})
}
}
impl Serialize for CountryAvailabilityDescriptor {
type Error = crate::error::Error;
fn serialized_len(&self) -> usize {
HEADER_LEN + FLAG_LEN + COUNTRY_CODE_LEN * self.country_codes.len()
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let len = self.serialized_len();
if buf.len() < len {
return Err(Error::OutputBufferTooSmall {
need: len,
have: buf.len(),
});
}
let body_len = FLAG_LEN + COUNTRY_CODE_LEN * self.country_codes.len();
if body_len > MAX_BODY_LEN {
return Err(Error::SectionLengthOverflow {
declared: body_len,
available: MAX_BODY_LEN,
});
}
buf[0] = TAG;
buf[1] = body_len as u8;
let flag_bit = if self.country_availability_flag {
AVAILABILITY_FLAG_MASK
} else {
0
};
buf[HEADER_LEN] = flag_bit | RESERVED_MASK;
let mut pos = HEADER_LEN + FLAG_LEN;
for code in &self.country_codes {
buf[pos..pos + COUNTRY_CODE_LEN].copy_from_slice(&code.0);
pos += COUNTRY_CODE_LEN;
}
Ok(len)
}
}
impl<'a> Descriptor<'a> for CountryAvailabilityDescriptor {
const TAG: u8 = TAG;
fn descriptor_length(&self) -> u8 {
(FLAG_LEN + COUNTRY_CODE_LEN * self.country_codes.len()) as u8
}
}
impl<'a> crate::traits::DescriptorDef<'a> for CountryAvailabilityDescriptor {
const TAG: u8 = TAG;
const NAME: &'static str = "COUNTRY_AVAILABILITY";
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_available_with_countries() {
let bytes = [TAG, 7, 0xFF, b'G', b'B', b'R', b'F', b'R', b'A'];
let d = CountryAvailabilityDescriptor::parse(&bytes).unwrap();
assert!(d.country_availability_flag);
assert_eq!(d.country_codes, vec![LangCode(*b"GBR"), LangCode(*b"FRA")]);
}
#[test]
fn parse_flag_clear() {
let bytes = [TAG, 4, 0x7F, b'D', b'E', b'U'];
let d = CountryAvailabilityDescriptor::parse(&bytes).unwrap();
assert!(!d.country_availability_flag);
assert_eq!(d.country_codes, vec![LangCode(*b"DEU")]);
}
#[test]
fn parse_flag_only_no_countries() {
let bytes = [TAG, 1, 0x80];
let d = CountryAvailabilityDescriptor::parse(&bytes).unwrap();
assert!(d.country_availability_flag);
assert!(d.country_codes.is_empty());
}
#[test]
fn parse_rejects_wrong_tag() {
assert!(matches!(
CountryAvailabilityDescriptor::parse(&[0x4A, 1, 0x80]).unwrap_err(),
Error::InvalidDescriptor { tag: 0x4A, .. }
));
}
#[test]
fn parse_rejects_short_buffer() {
let bytes = [TAG, 7, 0x80, b'G', b'B'];
assert!(matches!(
CountryAvailabilityDescriptor::parse(&bytes).unwrap_err(),
Error::BufferTooShort { .. }
));
}
#[test]
fn parse_rejects_loop_not_multiple_of_3() {
let bytes = [TAG, 3, 0x80, b'G', b'B'];
assert!(matches!(
CountryAvailabilityDescriptor::parse(&bytes).unwrap_err(),
Error::InvalidDescriptor { tag: TAG, .. }
));
}
#[test]
fn parse_rejects_zero_length() {
let bytes = [TAG, 0];
assert!(matches!(
CountryAvailabilityDescriptor::parse(&bytes).unwrap_err(),
Error::InvalidDescriptor { tag: TAG, .. }
));
}
#[test]
fn serialize_round_trip() {
let d = CountryAvailabilityDescriptor {
country_availability_flag: true,
country_codes: vec![LangCode(*b"GBR"), LangCode(*b"IRL")],
};
let mut buf = vec![0u8; d.serialized_len()];
d.serialize_into(&mut buf).unwrap();
assert_eq!(CountryAvailabilityDescriptor::parse(&buf).unwrap(), d);
}
#[test]
fn serialize_emits_reserved_ones() {
let d = CountryAvailabilityDescriptor {
country_availability_flag: false,
country_codes: vec![],
};
let mut buf = vec![0u8; d.serialized_len()];
d.serialize_into(&mut buf).unwrap();
assert_eq!(buf[HEADER_LEN], 0x7F);
}
#[cfg(feature = "serde")]
#[test]
fn serde_round_trip() {
let d = CountryAvailabilityDescriptor {
country_availability_flag: true,
country_codes: vec![LangCode(*b"FRA")],
};
let json = serde_json::to_string(&d).unwrap();
let _v: serde_json::Value = serde_json::from_str(&json).unwrap();
}
}