use crate::error::{Error, Result};
use crate::traits::OperationDef;
use broadcast_common::{Parse, Serialize};
pub const OP_ID: u16 = 0x0100;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct InjectSectionData<'a> {
pub scte35_command_length: u16,
pub scte35_protocol_version: u8,
pub scte35_command_type: u8,
#[cfg_attr(feature = "serde", serde(borrow))]
pub scte35_command_contents: &'a [u8],
}
pub const FIXED_LEN: usize = 4;
impl<'a> Parse<'a> for InjectSectionData<'a> {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
if bytes.len() < FIXED_LEN {
return Err(Error::BufferTooShort {
need: FIXED_LEN,
have: bytes.len(),
what: "inject_section_data header",
});
}
let scte35_command_length = u16::from_be_bytes([bytes[0], bytes[1]]);
let scte35_protocol_version = bytes[2];
let scte35_command_type = bytes[3];
let contents_len = scte35_command_length as usize;
if bytes.len() < FIXED_LEN + contents_len {
return Err(Error::BufferTooShort {
need: FIXED_LEN + contents_len,
have: bytes.len(),
what: "inject_section_data contents",
});
}
Ok(Self {
scte35_command_length,
scte35_protocol_version,
scte35_command_type,
scte35_command_contents: &bytes[FIXED_LEN..FIXED_LEN + contents_len],
})
}
}
impl Serialize for InjectSectionData<'_> {
type Error = Error;
fn serialized_len(&self) -> usize {
FIXED_LEN + self.scte35_command_contents.len()
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let need = self.serialized_len();
if buf.len() < need {
return Err(Error::OutputBufferTooSmall {
need,
have: buf.len(),
});
}
buf[0..2].copy_from_slice(&self.scte35_command_length.to_be_bytes());
buf[2] = self.scte35_protocol_version;
buf[3] = self.scte35_command_type;
buf[FIXED_LEN..need].copy_from_slice(self.scte35_command_contents);
Ok(need)
}
}
impl<'a> OperationDef<'a> for InjectSectionData<'a> {
const OP_ID: u16 = OP_ID;
const NAME: &'static str = "INJECT_SECTION_DATA";
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trip() {
let op = InjectSectionData {
scte35_command_length: 4,
scte35_protocol_version: 0,
scte35_command_type: 0x05,
scte35_command_contents: &[0xAA, 0xBB, 0xCC, 0xDD],
};
let bytes = op.to_bytes();
let back = InjectSectionData::parse(&bytes).unwrap();
assert_eq!(op, back);
}
#[test]
fn mutate_field_changes_output() {
let op = InjectSectionData {
scte35_command_length: 2,
scte35_protocol_version: 0,
scte35_command_type: 0,
scte35_command_contents: &[0x01, 0x02],
};
let bytes = op.to_bytes();
let mut op2 = op.clone();
op2.scte35_command_type = 0xFF;
assert_ne!(op2.to_bytes(), bytes);
}
}