va_ts/descriptor/
desc_dvb_0x4d.rs

1use std::fmt;
2
3use crate::annex_a2::AnnexA2;
4
5/// ETSI EN 300 468 V1.15.1
6///
7/// Short event descriptor
8#[derive(Clone)]
9pub struct DescDVB0x4D<'buf> {
10    buf: &'buf [u8],
11}
12
13impl<'buf> DescDVB0x4D<'buf> {
14    const HEADER_SZ: usize = 4;
15
16    #[inline(always)]
17    pub fn new(buf: &'buf [u8]) -> DescDVB0x4D<'buf> {
18        DescDVB0x4D { buf }
19    }
20
21    #[inline(always)]
22    fn buf_pos_event_name(&self) -> usize {
23        Self::HEADER_SZ
24    }
25
26    #[inline(always)]
27    fn buf_pos_text_length(&self) -> usize {
28        self.buf_pos_event_name() + (self.event_name_length() as usize)
29    }
30
31    #[inline(always)]
32    fn buf_pos_text(&self) -> usize {
33        self.buf_pos_text_length() + 1
34    }
35
36    #[inline(always)]
37    fn event_name_length(&self) -> u8 {
38        self.buf[3]
39    }
40
41    #[inline(always)]
42    pub fn event_name(&self) -> &'buf [u8] {
43        &self.buf[self.buf_pos_event_name()..self.buf_pos_text_length()]
44    }
45
46    #[inline(always)]
47    fn text_length(&self) -> u8 {
48        self.buf[self.buf_pos_text_length()]
49    }
50
51    #[inline(always)]
52    pub fn text(&self) -> &'buf [u8] {
53        let lft = self.buf_pos_text();
54        let rght = lft + (self.text_length() as usize);
55        &self.buf[lft..rght]
56    }
57}
58
59impl<'buf> fmt::Debug for DescDVB0x4D<'buf> {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        write!(f, ":dvb-0x4d (")?;
62
63        let mut dst_buf = [0u8; 256];
64        let mut dst_str = std::str::from_utf8_mut(&mut dst_buf).unwrap();
65
66        write!(f, ":event-name")?;
67        match AnnexA2::decode(self.event_name(), &mut dst_str) {
68            Ok(..) => write!(f, r#" "{}""#, dst_str),
69            Err(err) => write!(f, " (error: {:?})", err),
70        }?;
71
72        dst_buf = [0u8; 256];
73        dst_str = std::str::from_utf8_mut(&mut dst_buf).unwrap();
74
75        write!(f, " :text")?;
76        match AnnexA2::decode(self.text(), &mut dst_str) {
77            Ok(..) => write!(f, r#" "{}""#, dst_str),
78            Err(err) => write!(f, " (error: {})", err),
79        }?;
80
81        write!(f, ")")
82    }
83}