use crate::error::{Error, Result};
use crate::traits::Descriptor;
use dvb_common::{Parse, Serialize};
pub const TAG: u8 = 0x69;
pub const HEADER_LEN: usize = 2;
pub const BODY_LEN: usize = 3;
pub const PIL_MASK: u32 = 0x000F_FFFF;
pub const RESERVED_BITS: u32 = 0x00F0_0000;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct PdcDescriptor {
pub programme_identification_label: u32,
}
impl<'a> Parse<'a> for PdcDescriptor {
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: "PdcDescriptor header",
});
}
if bytes[0] != TAG {
return Err(Error::InvalidDescriptor {
tag: bytes[0],
reason: "unexpected tag for PDC_descriptor",
});
}
let length = bytes[1] as usize;
if length != BODY_LEN {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "PDC_descriptor length must be exactly 3",
});
}
let end = HEADER_LEN + length;
if bytes.len() < end {
return Err(Error::BufferTooShort {
need: end,
have: bytes.len(),
what: "PdcDescriptor body",
});
}
let raw = (u32::from(bytes[HEADER_LEN]) << 16)
| (u32::from(bytes[HEADER_LEN + 1]) << 8)
| u32::from(bytes[HEADER_LEN + 2]);
Ok(Self {
programme_identification_label: raw & PIL_MASK,
})
}
}
impl Serialize for PdcDescriptor {
type Error = crate::error::Error;
fn serialized_len(&self) -> usize {
HEADER_LEN + BODY_LEN
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
if self.programme_identification_label > PIL_MASK {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "programme_identification_label exceeds 20 bits",
});
}
let len = self.serialized_len();
if buf.len() < len {
return Err(Error::OutputBufferTooSmall {
need: len,
have: buf.len(),
});
}
buf[0] = TAG;
buf[1] = BODY_LEN as u8;
let raw = RESERVED_BITS | (self.programme_identification_label & PIL_MASK);
buf[HEADER_LEN] = (raw >> 16) as u8;
buf[HEADER_LEN + 1] = (raw >> 8) as u8;
buf[HEADER_LEN + 2] = raw as u8;
Ok(len)
}
}
impl<'a> Descriptor<'a> for PdcDescriptor {
const TAG: u8 = TAG;
fn descriptor_length(&self) -> u8 {
BODY_LEN as u8
}
}
impl<'a> crate::traits::DescriptorDef<'a> for PdcDescriptor {
const TAG: u8 = TAG;
const NAME: &'static str = "PDC";
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_extracts_pil() {
let bytes = [TAG, 3, 0x0A, 0xBC, 0xDE];
let d = PdcDescriptor::parse(&bytes).unwrap();
assert_eq!(d.programme_identification_label, 0x0A_BCDE);
}
#[test]
fn parse_ignores_reserved_bits() {
let bytes = [TAG, 3, 0xFA, 0xBC, 0xDE];
let d = PdcDescriptor::parse(&bytes).unwrap();
assert_eq!(d.programme_identification_label, 0x0A_BCDE);
}
#[test]
fn parse_rejects_wrong_tag() {
assert!(matches!(
PdcDescriptor::parse(&[0x6A, 3, 0, 0, 0]).unwrap_err(),
Error::InvalidDescriptor { tag: 0x6A, .. }
));
}
#[test]
fn parse_rejects_wrong_length() {
assert!(matches!(
PdcDescriptor::parse(&[TAG, 2, 0, 0]).unwrap_err(),
Error::InvalidDescriptor { tag: TAG, .. }
));
}
#[test]
fn parse_rejects_short_body() {
assert!(matches!(
PdcDescriptor::parse(&[TAG, 3, 0, 0]).unwrap_err(),
Error::BufferTooShort { .. }
));
}
#[test]
fn serialize_round_trip() {
let d = PdcDescriptor {
programme_identification_label: 0x0A_BCDE,
};
let mut buf = vec![0u8; d.serialized_len()];
d.serialize_into(&mut buf).unwrap();
assert_eq!(buf, [TAG, 3, 0xFA, 0xBC, 0xDE]);
assert_eq!(PdcDescriptor::parse(&buf).unwrap(), d);
}
#[test]
fn serialize_rejects_too_small_buffer() {
let d = PdcDescriptor {
programme_identification_label: 0,
};
let mut buf = vec![0u8; 2];
assert!(matches!(
d.serialize_into(&mut buf).unwrap_err(),
Error::OutputBufferTooSmall { .. }
));
}
#[test]
fn serialize_rejects_over_range_pil() {
let d = PdcDescriptor {
programme_identification_label: 0x10_0000, };
let mut buf = vec![0u8; d.serialized_len()];
assert!(matches!(
d.serialize_into(&mut buf).unwrap_err(),
Error::InvalidDescriptor { tag: TAG, .. }
));
}
#[cfg(feature = "serde")]
#[test]
fn serde_round_trip() {
let d = PdcDescriptor {
programme_identification_label: 0x0A_BCDE,
};
let json = serde_json::to_string(&d).unwrap();
let _v: serde_json::Value = serde_json::from_str(&json).unwrap();
}
}