Skip to main content

dvb_si/descriptors/
stream_identifier.rs

1//! Stream Identifier Descriptor — ETSI EN 300 468 §6.2.41 (tag 0x52).
2//!
3//! One-byte `component_tag` that anchors components named elsewhere (e.g. by
4//! component_descriptor).
5
6use crate::error::{Error, Result};
7use crate::traits::Descriptor;
8use dvb_common::{Parse, Serialize};
9
10/// Descriptor tag for stream_identifier_descriptor.
11pub const TAG: u8 = 0x52;
12const HEADER_LEN: usize = 2;
13const BODY_LEN: u8 = 1;
14
15/// Stream Identifier Descriptor.
16#[derive(Debug, Clone, PartialEq, Eq)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18pub struct StreamIdentifierDescriptor {
19    /// Component tag used to cross-reference a component_descriptor.
20    pub component_tag: u8,
21}
22
23impl<'a> Parse<'a> for StreamIdentifierDescriptor {
24    type Error = crate::error::Error;
25    fn parse(bytes: &'a [u8]) -> Result<Self> {
26        if bytes.len() < HEADER_LEN + BODY_LEN as usize {
27            return Err(Error::BufferTooShort {
28                need: HEADER_LEN + BODY_LEN as usize,
29                have: bytes.len(),
30                what: "StreamIdentifierDescriptor",
31            });
32        }
33        if bytes[0] != TAG {
34            return Err(Error::InvalidDescriptor {
35                tag: bytes[0],
36                reason: "unexpected tag for stream_identifier_descriptor",
37            });
38        }
39        if bytes[1] != BODY_LEN {
40            return Err(Error::InvalidDescriptor {
41                tag: TAG,
42                reason: "stream_identifier_descriptor length must equal 1",
43            });
44        }
45        Ok(Self {
46            component_tag: bytes[2],
47        })
48    }
49}
50
51impl Serialize for StreamIdentifierDescriptor {
52    type Error = crate::error::Error;
53    fn serialized_len(&self) -> usize {
54        HEADER_LEN + BODY_LEN as usize
55    }
56
57    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
58        let len = self.serialized_len();
59        if buf.len() < len {
60            return Err(Error::OutputBufferTooSmall {
61                need: len,
62                have: buf.len(),
63            });
64        }
65        buf[0] = TAG;
66        buf[1] = BODY_LEN;
67        buf[2] = self.component_tag;
68        Ok(len)
69    }
70}
71
72impl<'a> Descriptor<'a> for StreamIdentifierDescriptor {
73    const TAG: u8 = TAG;
74    fn descriptor_length(&self) -> u8 {
75        BODY_LEN
76    }
77}
78
79impl<'a> crate::traits::DescriptorDef<'a> for StreamIdentifierDescriptor {
80    const TAG: u8 = TAG;
81    const NAME: &'static str = "STREAM_IDENTIFIER";
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    #[test]
89    fn parse_extracts_component_tag() {
90        let d = StreamIdentifierDescriptor::parse(&[TAG, 1, 0x42]).unwrap();
91        assert_eq!(d.component_tag, 0x42);
92    }
93
94    #[test]
95    fn parse_rejects_wrong_tag() {
96        let err = StreamIdentifierDescriptor::parse(&[0x53, 1, 0]).unwrap_err();
97        assert!(matches!(err, Error::InvalidDescriptor { tag: 0x53, .. }));
98    }
99
100    #[test]
101    fn parse_rejects_wrong_length() {
102        let err = StreamIdentifierDescriptor::parse(&[TAG, 2, 0, 0]).unwrap_err();
103        assert!(matches!(err, Error::InvalidDescriptor { .. }));
104    }
105
106    #[test]
107    fn parse_rejects_short_buffer() {
108        let err = StreamIdentifierDescriptor::parse(&[TAG]).unwrap_err();
109        assert!(matches!(err, Error::BufferTooShort { .. }));
110    }
111
112    #[test]
113    fn serialize_round_trip() {
114        let d = StreamIdentifierDescriptor {
115            component_tag: 0xFE,
116        };
117        let mut buf = vec![0u8; d.serialized_len()];
118        d.serialize_into(&mut buf).unwrap();
119        assert_eq!(StreamIdentifierDescriptor::parse(&buf).unwrap(), d);
120    }
121}