Skip to main content

dvb_si/descriptors/ait/
simple_application_location.rs

1//! Simple Application Location Descriptor — ETSI TS 102 809 §5.3.7.0, Table 33
2//! (AIT tag 0x15).
3//!
4//! Carried in the AIT per-application descriptor loop. The body is the
5//! initial path string (e.g. `/index.html`).
6
7use crate::descriptors::descriptor_body;
8use crate::error::{Error, Result};
9use crate::text::DvbText;
10use dvb_common::{Parse, Serialize};
11
12/// Descriptor tag for simple_application_location_descriptor (AIT namespace).
13pub const TAG: u8 = 0x15;
14const HEADER_LEN: usize = 2;
15
16/// Simple Application Location Descriptor (AIT tag 0x15).
17#[derive(Debug, Clone, PartialEq, Eq)]
18#[cfg_attr(feature = "serde", derive(serde::Serialize))]
19#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
20pub struct SimpleApplicationLocationDescriptor<'a> {
21    /// Initial path bytes (DVB Annex-A encoded; typically ASCII/UTF-8).
22    pub initial_path_bytes: DvbText<'a>,
23}
24
25impl<'a> Parse<'a> for SimpleApplicationLocationDescriptor<'a> {
26    type Error = crate::error::Error;
27    fn parse(bytes: &'a [u8]) -> Result<Self> {
28        let body = descriptor_body(
29            bytes,
30            TAG,
31            "SimpleApplicationLocationDescriptor",
32            "unexpected tag for simple_application_location_descriptor",
33        )?;
34        Ok(Self {
35            initial_path_bytes: DvbText::new(body),
36        })
37    }
38}
39
40impl Serialize for SimpleApplicationLocationDescriptor<'_> {
41    type Error = crate::error::Error;
42    fn serialized_len(&self) -> usize {
43        HEADER_LEN + self.initial_path_bytes.len()
44    }
45
46    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
47        let body_len = self.initial_path_bytes.len();
48        if body_len > u8::MAX as usize {
49            return Err(Error::InvalidDescriptor {
50                tag: TAG,
51                reason: "simple_application_location_descriptor body exceeds 255 bytes",
52            });
53        }
54        let len = self.serialized_len();
55        if buf.len() < len {
56            return Err(Error::OutputBufferTooSmall {
57                need: len,
58                have: buf.len(),
59            });
60        }
61        buf[0] = TAG;
62        buf[1] = body_len as u8;
63        buf[HEADER_LEN..len].copy_from_slice(self.initial_path_bytes.raw());
64        Ok(len)
65    }
66}
67
68impl<'a> crate::traits::DescriptorDef<'a> for SimpleApplicationLocationDescriptor<'a> {
69    const TAG: u8 = TAG;
70    const NAME: &'static str = "SIMPLE_APPLICATION_LOCATION";
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn parse_path() {
79        let path = b"/index.html";
80        let mut bytes = vec![TAG, path.len() as u8];
81        bytes.extend_from_slice(path);
82        let d = SimpleApplicationLocationDescriptor::parse(&bytes).unwrap();
83        assert_eq!(d.initial_path_bytes.raw(), b"/index.html");
84    }
85
86    #[test]
87    fn parse_empty_path() {
88        let bytes = [TAG, 0];
89        let d = SimpleApplicationLocationDescriptor::parse(&bytes).unwrap();
90        assert!(d.initial_path_bytes.raw().is_empty());
91    }
92
93    #[test]
94    fn serialize_round_trip() {
95        let d = SimpleApplicationLocationDescriptor {
96            initial_path_bytes: DvbText::new(b"/app/main.html"),
97        };
98        let mut buf = vec![0u8; d.serialized_len()];
99        d.serialize_into(&mut buf).unwrap();
100        let re = SimpleApplicationLocationDescriptor::parse(&buf).unwrap();
101        assert_eq!(d, re);
102    }
103
104    #[test]
105    fn serialize_byte_identical() {
106        let bytes = [TAG, 5, b'/', b'a', b'p', b'p', b'/'];
107        let d = SimpleApplicationLocationDescriptor::parse(&bytes).unwrap();
108        let mut buf = vec![0u8; d.serialized_len()];
109        d.serialize_into(&mut buf).unwrap();
110        assert_eq!(buf.as_slice(), &bytes[..]);
111    }
112}