use crate::packet;
use log::warn;
pub const PAT_PID: packet::Pid = packet::Pid::new(0);
#[derive(Clone, Debug)]
pub enum ProgramDescriptor {
Network {
pid: packet::Pid,
},
Program {
program_number: u16,
pid: packet::Pid,
},
}
impl ProgramDescriptor {
pub fn from_bytes(data: &[u8]) -> ProgramDescriptor {
let program_number = (u16::from(data[0]) << 8) | u16::from(data[1]);
let pid = packet::Pid::new((u16::from(data[2]) & 0b0001_1111) << 8 | u16::from(data[3]));
if program_number == 0 {
ProgramDescriptor::Network { pid }
} else {
ProgramDescriptor::Program {
program_number,
pid,
}
}
}
pub fn pid(&self) -> packet::Pid {
match *self {
ProgramDescriptor::Network { pid } => pid,
ProgramDescriptor::Program { pid, .. } => pid,
}
}
}
#[derive(Clone, Debug)]
pub struct PatSection<'buf> {
data: &'buf [u8],
}
impl<'buf> PatSection<'buf> {
pub fn new(data: &'buf [u8]) -> PatSection<'buf> {
PatSection { data }
}
pub fn programs(&self) -> impl Iterator<Item = ProgramDescriptor> + 'buf {
ProgramIter { buf: self.data }
}
}
struct ProgramIter<'buf> {
buf: &'buf [u8],
}
impl<'buf> Iterator for ProgramIter<'buf> {
type Item = ProgramDescriptor;
fn next(&mut self) -> Option<Self::Item> {
if self.buf.is_empty() {
return None;
}
if self.buf.len() < 4 {
warn!(
"too few bytes remaining for PAT descriptor: {}",
self.buf.len()
);
return None;
}
let (head, tail) = self.buf.split_at(4);
self.buf = tail;
Some(ProgramDescriptor::from_bytes(head))
}
}