dvb_si/descriptors/
default_authority.rs1use super::descriptor_body;
7use crate::error::{Error, Result};
8use dvb_common::{Parse, Serialize};
9
10pub const TAG: u8 = 0x73;
12const HEADER_LEN: usize = 2;
13
14#[derive(Debug, Clone, PartialEq, Eq)]
16#[cfg_attr(feature = "serde", derive(serde::Serialize))]
17#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
18pub struct DefaultAuthorityDescriptor<'a> {
19 pub default_authority: &'a [u8],
21}
22
23impl<'a> Parse<'a> for DefaultAuthorityDescriptor<'a> {
24 type Error = crate::error::Error;
25 fn parse(bytes: &'a [u8]) -> Result<Self> {
26 let body = descriptor_body(
27 bytes,
28 TAG,
29 "DefaultAuthorityDescriptor",
30 "unexpected tag for default authority descriptor",
31 )?;
32 Ok(Self {
33 default_authority: body,
34 })
35 }
36}
37
38impl Serialize for DefaultAuthorityDescriptor<'_> {
39 type Error = crate::error::Error;
40 fn serialized_len(&self) -> usize {
41 HEADER_LEN + self.default_authority.len()
42 }
43
44 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
45 let len = self.serialized_len();
46 if buf.len() < len {
47 return Err(Error::OutputBufferTooSmall {
48 need: len,
49 have: buf.len(),
50 });
51 }
52 buf[0] = TAG;
53 buf[1] = self.default_authority.len() as u8;
54 buf[HEADER_LEN..len].copy_from_slice(self.default_authority);
55 Ok(len)
56 }
57}
58impl<'a> crate::traits::DescriptorDef<'a> for DefaultAuthorityDescriptor<'a> {
59 const TAG: u8 = TAG;
60 const NAME: &'static str = "DEFAULT_AUTHORITY";
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn parse_extracts_authority_bytes() {
69 let bytes = [TAG, 9, b'b', b'b', b'c', b'.', b'c', b'o', b'.', b'u', b'k'];
70 let d = DefaultAuthorityDescriptor::parse(&bytes).unwrap();
71 assert_eq!(d.default_authority, b"bbc.co.uk");
72 }
73
74 #[test]
75 fn parse_rejects_wrong_tag() {
76 assert!(matches!(
77 DefaultAuthorityDescriptor::parse(&[0x7A, 1, 0]).unwrap_err(),
78 Error::InvalidDescriptor { tag: 0x7A, .. }
79 ));
80 }
81
82 #[test]
83 fn parse_rejects_short_header() {
84 assert!(matches!(
85 DefaultAuthorityDescriptor::parse(&[TAG]).unwrap_err(),
86 Error::BufferTooShort { .. }
87 ));
88 }
89
90 #[test]
91 fn parse_rejects_length_overrunning_buffer() {
92 let bytes = [TAG, 5, 1, 2, 3];
93 assert!(matches!(
94 DefaultAuthorityDescriptor::parse(&bytes).unwrap_err(),
95 Error::BufferTooShort { .. }
96 ));
97 }
98
99 #[test]
100 fn empty_authority_is_valid() {
101 let bytes = [TAG, 0];
102 let d = DefaultAuthorityDescriptor::parse(&bytes).unwrap();
103 assert!(d.default_authority.is_empty());
104 }
105
106 #[test]
107 fn serialize_round_trip() {
108 let d = DefaultAuthorityDescriptor {
109 default_authority: b"example.com",
110 };
111 let mut buf = vec![0u8; d.serialized_len()];
112 d.serialize_into(&mut buf).unwrap();
113 assert_eq!(DefaultAuthorityDescriptor::parse(&buf).unwrap(), d);
114 }
115
116 #[test]
117 fn serialize_rejects_too_small_buffer() {
118 let d = DefaultAuthorityDescriptor {
119 default_authority: b"test",
120 };
121 let mut buf = vec![0u8; 1];
122 assert!(matches!(
123 d.serialize_into(&mut buf).unwrap_err(),
124 Error::OutputBufferTooSmall { .. }
125 ));
126 }
127}