use crate::error::{Error, Result};
use crate::time::SpliceScheduleTime;
use crate::traits::OperationDef;
use broadcast_common::{Parse, Serialize};
pub const OP_ID: u16 = 0x010E;
pub const LEN: usize = 16;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum SpliceScheduleCommand {
Reserved,
SpliceInsert,
SpliceReturn,
SpliceCancel,
Unknown(u8),
}
impl SpliceScheduleCommand {
#[must_use]
pub fn from_byte(b: u8) -> Self {
match b {
0 => Self::Reserved,
1 => Self::SpliceInsert,
3 => Self::SpliceReturn,
5 => Self::SpliceCancel,
_ => Self::Unknown(b),
}
}
#[must_use]
pub fn to_byte(self) -> u8 {
match self {
Self::Reserved => 0,
Self::SpliceInsert => 1,
Self::SpliceReturn => 3,
Self::SpliceCancel => 5,
Self::Unknown(b) => b,
}
}
#[must_use]
pub fn name(&self) -> &'static str {
match self {
Self::Reserved => "reserved",
Self::SpliceInsert => "splice insert",
Self::SpliceReturn => "splice return",
Self::SpliceCancel => "splice cancel",
Self::Unknown(_) => "unknown",
}
}
}
broadcast_common::impl_spec_display!(SpliceScheduleCommand, Unknown);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct ScheduleDefinition {
pub splice_schedule_command: SpliceScheduleCommand,
pub splice_event_id: u32,
pub time: SpliceScheduleTime,
pub unique_program_id: u16,
pub auto_return: u8,
pub break_duration: u16,
pub avail_num: u8,
pub avails_expected: u8,
}
impl Default for ScheduleDefinition {
fn default() -> Self {
Self {
splice_schedule_command: SpliceScheduleCommand::Reserved,
splice_event_id: 0,
time: SpliceScheduleTime { seconds: 0 },
unique_program_id: 0,
auto_return: 0,
break_duration: 0,
avail_num: 0,
avails_expected: 0,
}
}
}
impl<'a> Parse<'a> for ScheduleDefinition {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
if bytes.len() < LEN {
return Err(Error::BufferTooShort {
need: LEN,
have: bytes.len(),
what: "schedule_definition_data",
});
}
Ok(Self {
splice_schedule_command: SpliceScheduleCommand::from_byte(bytes[0]),
splice_event_id: u32::from_be_bytes([bytes[1], bytes[2], bytes[3], bytes[4]]),
time: SpliceScheduleTime::parse(&bytes[5..9])?,
unique_program_id: u16::from_be_bytes([bytes[9], bytes[10]]),
auto_return: bytes[11],
break_duration: u16::from_be_bytes([bytes[12], bytes[13]]),
avail_num: bytes[14],
avails_expected: bytes[15],
})
}
}
impl Serialize for ScheduleDefinition {
type Error = Error;
fn serialized_len(&self) -> usize {
LEN
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
if buf.len() < LEN {
return Err(Error::OutputBufferTooSmall {
need: LEN,
have: buf.len(),
});
}
buf[0] = self.splice_schedule_command.to_byte();
buf[1..5].copy_from_slice(&self.splice_event_id.to_be_bytes());
self.time.serialize_into(&mut buf[5..9])?;
buf[9..11].copy_from_slice(&self.unique_program_id.to_be_bytes());
buf[11] = self.auto_return;
buf[12..14].copy_from_slice(&self.break_duration.to_be_bytes());
buf[14] = self.avail_num;
buf[15] = self.avails_expected;
Ok(LEN)
}
}
impl<'a> OperationDef<'a> for ScheduleDefinition {
const OP_ID: u16 = OP_ID;
const NAME: &'static str = "SCHEDULE_DEFINITION";
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trip() {
let op = ScheduleDefinition {
splice_schedule_command: SpliceScheduleCommand::SpliceInsert,
splice_event_id: 0x42,
time: SpliceScheduleTime {
seconds: 0x6000_0000,
},
unique_program_id: 1,
auto_return: 1,
break_duration: 300,
avail_num: 0,
avails_expected: 0,
};
let bytes = op.to_bytes();
assert_eq!(bytes.len(), LEN);
let back = ScheduleDefinition::parse(&bytes).unwrap();
assert_eq!(op, back);
}
#[test]
fn mutate_field_changes_output() {
let op = ScheduleDefinition {
splice_event_id: 1,
..ScheduleDefinition::default()
};
let bytes = op.to_bytes();
let mut op2 = op;
op2.splice_event_id = 999;
assert_ne!(op2.to_bytes(), bytes);
}
}