dvb_si/descriptors/
service_identifier.rs1use super::descriptor_body;
9use crate::error::{Error, Result};
10use dvb_common::{Parse, Serialize};
11
12pub const TAG: u8 = 0x71;
14const HEADER_LEN: usize = 2;
15
16#[derive(Debug, Clone, PartialEq, Eq)]
18#[cfg_attr(feature = "serde", derive(serde::Serialize))]
19#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
20pub struct ServiceIdentifierDescriptor<'a> {
21 pub textual_service_identifier: &'a [u8],
23}
24
25impl<'a> Parse<'a> for ServiceIdentifierDescriptor<'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 "ServiceIdentifierDescriptor",
32 "unexpected tag for service_identifier_descriptor",
33 )?;
34 Ok(Self {
35 textual_service_identifier: body,
36 })
37 }
38}
39
40impl Serialize for ServiceIdentifierDescriptor<'_> {
41 type Error = crate::error::Error;
42 fn serialized_len(&self) -> usize {
43 HEADER_LEN + self.textual_service_identifier.len()
44 }
45
46 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
47 if self.textual_service_identifier.len() > u8::MAX as usize {
48 return Err(Error::InvalidDescriptor {
49 tag: TAG,
50 reason: "textual_service_identifier exceeds 255 bytes",
51 });
52 }
53 let len = self.serialized_len();
54 if buf.len() < len {
55 return Err(Error::OutputBufferTooSmall {
56 need: len,
57 have: buf.len(),
58 });
59 }
60 buf[0] = TAG;
61 buf[1] = self.textual_service_identifier.len() as u8;
62 buf[HEADER_LEN..len].copy_from_slice(self.textual_service_identifier);
63 Ok(len)
64 }
65}
66impl<'a> crate::traits::DescriptorDef<'a> for ServiceIdentifierDescriptor<'a> {
67 const TAG: u8 = TAG;
68 const NAME: &'static str = "SERVICE_IDENTIFIER";
69}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74
75 #[test]
76 fn parse_extracts_identifier_bytes() {
77 let bytes = [TAG, 6, b'B', b'B', b'C', b'O', b'N', b'E'];
78 let d = ServiceIdentifierDescriptor::parse(&bytes).unwrap();
79 assert_eq!(d.textual_service_identifier, b"BBCONE");
80 }
81
82 #[test]
83 fn parse_rejects_wrong_tag() {
84 assert!(matches!(
85 ServiceIdentifierDescriptor::parse(&[0x70, 1, 0]).unwrap_err(),
86 Error::InvalidDescriptor { tag: 0x70, .. }
87 ));
88 }
89
90 #[test]
91 fn parse_rejects_short_header() {
92 assert!(matches!(
93 ServiceIdentifierDescriptor::parse(&[TAG]).unwrap_err(),
94 Error::BufferTooShort { .. }
95 ));
96 }
97
98 #[test]
99 fn parse_rejects_length_overrunning_buffer() {
100 let bytes = [TAG, 5, 1, 2, 3];
101 assert!(matches!(
102 ServiceIdentifierDescriptor::parse(&bytes).unwrap_err(),
103 Error::BufferTooShort { .. }
104 ));
105 }
106
107 #[test]
108 fn empty_identifier_is_valid() {
109 let bytes = [TAG, 0];
110 let d = ServiceIdentifierDescriptor::parse(&bytes).unwrap();
111 assert!(d.textual_service_identifier.is_empty());
112 }
113
114 #[test]
115 fn serialize_round_trip() {
116 let d = ServiceIdentifierDescriptor {
117 textual_service_identifier: b"CH4-HD",
118 };
119 let mut buf = vec![0u8; d.serialized_len()];
120 d.serialize_into(&mut buf).unwrap();
121 assert_eq!(ServiceIdentifierDescriptor::parse(&buf).unwrap(), d);
122 }
123
124 #[test]
125 fn serialize_rejects_too_small_buffer() {
126 let d = ServiceIdentifierDescriptor {
127 textual_service_identifier: b"test",
128 };
129 let mut buf = vec![0u8; 1];
130 assert!(matches!(
131 d.serialize_into(&mut buf).unwrap_err(),
132 Error::OutputBufferTooSmall { .. }
133 ));
134 }
135
136 #[cfg(feature = "serde")]
137 #[test]
138 fn serde_serializes_to_stable_json() {
139 let d = ServiceIdentifierDescriptor {
144 textual_service_identifier: b"ITV1",
145 };
146 let j = serde_json::to_string(&d).unwrap();
147 let _v: serde_json::Value = serde_json::from_str(&j).unwrap();
150 assert!(j.contains("textual_service_identifier"));
151 }
152}