use crate::descriptors::{descriptor_body, write_descriptor_header};
use crate::error::{Error, Result};
use crate::text::DvbText;
use broadcast_common::{Parse, Serialize};
pub const TAG: u8 = 0x09;
const HEADER_LEN: usize = 2;
const ROOT_LEN_FIELD: usize = 1;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct DvbHtmlApplicationLocationDescriptor<'a> {
pub physical_root: DvbText<'a>,
pub initial_path: DvbText<'a>,
}
impl<'a> Parse<'a> for DvbHtmlApplicationLocationDescriptor<'a> {
type Error = crate::error::Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let body = descriptor_body(
bytes,
TAG,
"DvbHtmlApplicationLocationDescriptor",
"unexpected tag for dvb_html_application_location_descriptor",
)?;
let root_len = *body.first().ok_or(Error::InvalidDescriptor {
tag: TAG,
reason: "dvb_html_application_location_descriptor missing physical_root_length",
})? as usize;
let end = ROOT_LEN_FIELD + root_len;
if body.len() < end {
return Err(Error::InvalidDescriptor {
tag: TAG,
reason: "dvb_html_application_location_descriptor physical_root overruns body",
});
}
Ok(Self {
physical_root: DvbText::new(&body[ROOT_LEN_FIELD..end]),
initial_path: DvbText::new(&body[end..]),
})
}
}
impl Serialize for DvbHtmlApplicationLocationDescriptor<'_> {
type Error = crate::error::Error;
fn serialized_len(&self) -> usize {
HEADER_LEN + ROOT_LEN_FIELD + self.physical_root.len() + self.initial_path.len()
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let len = self.serialized_len();
let root_len =
broadcast_common::len::fit_u8(self.physical_root.len(), "physical_root_length")?;
if buf.len() < len {
return Err(Error::OutputBufferTooSmall {
need: len,
have: buf.len(),
});
}
write_descriptor_header(buf, TAG, len - HEADER_LEN)?;
buf[HEADER_LEN] = root_len;
let root_end = HEADER_LEN + ROOT_LEN_FIELD + self.physical_root.len();
buf[HEADER_LEN + ROOT_LEN_FIELD..root_end].copy_from_slice(self.physical_root.raw());
buf[root_end..len].copy_from_slice(self.initial_path.raw());
Ok(len)
}
}
impl<'a> crate::traits::DescriptorDef<'a> for DvbHtmlApplicationLocationDescriptor<'a> {
const TAG: u8 = TAG;
const NAME: &'static str = "DVB_HTML_APPLICATION_LOCATION";
}
#[cfg(test)]
mod tests {
use super::*;
fn spec_example() -> Vec<u8> {
let mut v = vec![TAG, 0, 12];
v.extend_from_slice(b"application/");
v.extend_from_slice(b"main/index.htm");
v[1] = u8::try_from(v.len() - 2).unwrap();
v
}
#[test]
fn parse_spec_example() {
let bytes = spec_example();
let d = DvbHtmlApplicationLocationDescriptor::parse(&bytes).unwrap();
assert_eq!(d.physical_root.raw(), b"application/");
assert_eq!(d.initial_path.raw(), b"main/index.htm");
}
#[test]
fn round_trip_spec_example_and_empty_root() {
for bytes in [
spec_example(),
vec![TAG, 1, 0],
vec![TAG, 4, 0, b'a', b'/', b'b'],
] {
let d = DvbHtmlApplicationLocationDescriptor::parse(&bytes).unwrap();
let mut out = vec![0u8; d.serialized_len()];
d.serialize_into(&mut out).unwrap();
assert_eq!(out, bytes);
assert_eq!(
DvbHtmlApplicationLocationDescriptor::parse(&out).unwrap(),
d
);
}
}
#[test]
fn malformed_rejected() {
assert!(DvbHtmlApplicationLocationDescriptor::parse(&[TAG, 0]).is_err());
assert!(DvbHtmlApplicationLocationDescriptor::parse(&[TAG, 2, 5, b'a']).is_err());
let big = vec![b'x'; 256];
let d = DvbHtmlApplicationLocationDescriptor {
physical_root: DvbText::new(&big),
initial_path: DvbText::new(b""),
};
let mut out = vec![0u8; d.serialized_len()];
assert!(d.serialize_into(&mut out).is_err());
}
}