1use alloc::boxed::Box;
50#[cfg(feature = "serde")]
51use alloc::string::ToString;
52
53macro_rules! declare_descriptors {
61 (
62 $lt:lifetime;
63 $( $variant:ident = $tag:literal => $($path:ident)::+ $(<$plt:lifetime>)? ),+ $(,)?
64 $( ; @no_dispatch $( $nd_variant:ident => $($nd_path:ident)::+ $(<$nd_plt:lifetime>)? ),+ $(,)? )?
65 ) => {
66 #[derive(Debug)]
73 #[cfg_attr(feature = "serde", derive(serde::Serialize))]
74 #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
75 #[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
80 #[non_exhaustive]
81 pub enum AnyDescriptor<$lt> {
82 $(
83 #[allow(missing_docs)]
84 $variant($($path)::+ $(<$plt>)?),
85 )+
86 $($(
87 #[allow(missing_docs)]
88 $nd_variant($($nd_path)::+ $(<$nd_plt>)?),
89 )+)?
90 Other {
94 tag: u8,
96 #[cfg_attr(
100 feature = "serde",
101 serde(serialize_with = "crate::descriptors::registry::serialize_erased")
102 )]
103 value: Box<dyn crate::descriptors::registry::DescriptorObject>,
104 },
105 Unknown {
108 tag: u8,
110 body: &$lt [u8],
112 },
113 }
114
115 $(
116 impl<$lt> From<$($path)::+ $(<$plt>)?> for AnyDescriptor<$lt> {
117 fn from(d: $($path)::+ $(<$plt>)?) -> Self {
118 Self::$variant(d)
119 }
120 }
121 )+
122 $($(
123 impl<$lt> From<$($nd_path)::+ $(<$nd_plt>)?> for AnyDescriptor<$lt> {
124 fn from(d: $($nd_path)::+ $(<$nd_plt>)?) -> Self {
125 Self::$nd_variant(d)
126 }
127 }
128 )+)?
129
130 impl<$lt> AnyDescriptor<$lt> {
131 pub const DISPATCHED_TAGS: &'static [u8] = &[$($tag),+];
134
135 #[must_use]
141 pub fn name(&self) -> &'static str {
142 match self {
143 $(
144 Self::$variant(_) =>
145 <$($path)::+ as crate::traits::DescriptorDef>::NAME,
146 )+
147 $($(
148 Self::$nd_variant(_) =>
149 <$($nd_path)::+ as crate::traits::DescriptorDef>::NAME,
150 )+)?
151 Self::Other { .. } => "CUSTOM",
152 Self::Unknown { .. } => "UNKNOWN",
153 }
154 }
155
156 pub(crate) fn dispatch(tag: u8, full: &$lt [u8]) -> Option<crate::Result<Self>> {
162 use dvb_common::Parse;
163 match tag {
164 $(
165 $tag => Some(<$($path)::+>::parse(full).map(Self::$variant)),
166 )+
167 _ => None,
168 }
169 }
170 }
171
172 #[cfg(test)]
173 mod macro_drift {
174 #[test]
175 fn tag_literals_match_descriptor_def() {
176 use crate::traits::DescriptorDef;
177 $(
178 assert_eq!(
179 $tag,
180 <$($path)::+ as DescriptorDef>::TAG,
181 concat!("tag literal drift for ", stringify!($variant)),
182 );
183 assert!(
184 !<$($path)::+ as DescriptorDef>::NAME.is_empty(),
185 concat!("empty NAME for ", stringify!($variant)),
186 );
187 )+
188 $($(
189 assert!(
190 !<$($nd_path)::+ as DescriptorDef>::NAME.is_empty(),
191 concat!("empty NAME for ", stringify!($nd_variant)),
192 );
193 )+)?
194 }
195 }
196 };
197}
198
199declare_descriptors! {'a;
200 VideoStream = 0x02 => crate::descriptors::video_stream::VideoStreamDescriptor,
202 AudioStream = 0x03 => crate::descriptors::audio_stream::AudioStreamDescriptor,
203 Hierarchy = 0x04 => crate::descriptors::hierarchy::HierarchyDescriptor,
204 Registration = 0x05 => crate::descriptors::registration::RegistrationDescriptor<'a>,
205 DataStreamAlignment = 0x06 => crate::descriptors::data_stream_alignment::DataStreamAlignmentDescriptor,
206 TargetBackgroundGrid = 0x07 => crate::descriptors::target_background_grid::TargetBackgroundGridDescriptor,
207 VideoWindow = 0x08 => crate::descriptors::video_window::VideoWindowDescriptor,
208 Ca = 0x09 => crate::descriptors::ca::CaDescriptor<'a>,
209 Iso639Language = 0x0A => crate::descriptors::iso_639_language::Iso639LanguageDescriptor,
210 SystemClock = 0x0B => crate::descriptors::system_clock::SystemClockDescriptor,
211 MultiplexBufferUtilization = 0x0C => crate::descriptors::multiplex_buffer_utilization::MultiplexBufferUtilizationDescriptor,
212 Copyright = 0x0D => crate::descriptors::copyright::CopyrightDescriptor<'a>,
213 MaximumBitrate = 0x0E => crate::descriptors::maximum_bitrate::MaximumBitrateDescriptor,
214 PrivateDataIndicator = 0x0F => crate::descriptors::private_data_indicator::PrivateDataIndicatorDescriptor,
215 SmoothingBuffer = 0x10 => crate::descriptors::smoothing_buffer::SmoothingBufferDescriptor,
216 Std = 0x11 => crate::descriptors::std::StdDescriptor,
217 Ibp = 0x12 => crate::descriptors::ibp::IbpDescriptor,
218 CarouselIdentifier = 0x13 => crate::descriptors::carousel_identifier::CarouselIdentifierDescriptor<'a>,
219 Mpeg4Video = 0x1B => crate::descriptors::mpeg4_video::Mpeg4VideoDescriptor,
220 Mpeg4Audio = 0x1C => crate::descriptors::mpeg4_audio::Mpeg4AudioDescriptor,
221 Iod = 0x1D => crate::descriptors::iod::IodDescriptor<'a>,
222 Sl = 0x1E => crate::descriptors::sl::SlDescriptor,
223 Fmc = 0x1F => crate::descriptors::fmc::FmcDescriptor,
224 ExternalEsId = 0x20 => crate::descriptors::external_es_id::ExternalEsIdDescriptor,
225 Muxcode = 0x21 => crate::descriptors::muxcode::MuxcodeDescriptor<'a>,
226 FmxBufferSize = 0x22 => crate::descriptors::fmx_buffer_size::FmxBufferSizeDescriptor<'a>,
227 MultiplexBuffer = 0x23 => crate::descriptors::multiplex_buffer::MultiplexBufferDescriptor,
228 ContentLabeling = 0x24 => crate::descriptors::content_labeling::ContentLabelingDescriptor<'a>,
229 MetadataPointer = 0x25 => crate::descriptors::metadata_pointer::MetadataPointerDescriptor<'a>,
230 Metadata = 0x26 => crate::descriptors::metadata::MetadataDescriptor<'a>,
231 MetadataStd = 0x27 => crate::descriptors::metadata_std::MetadataStdDescriptor,
232 AvcVideo = 0x28 => crate::descriptors::avc_video::AvcVideoDescriptor,
233 AvcTimingAndHrd = 0x2A => crate::descriptors::avc_timing_and_hrd::AvcTimingAndHrdDescriptor,
234 Mpeg2AacAudio = 0x2B => crate::descriptors::mpeg2_aac_audio::Mpeg2AacAudioDescriptor,
235 FlexMuxTiming = 0x2C => crate::descriptors::flex_mux_timing::FlexMuxTimingDescriptor,
236 AuxiliaryVideoStream = 0x2F => crate::descriptors::auxiliary_video_stream::AuxiliaryVideoStreamDescriptor<'a>,
237 SvcExtension = 0x30 => crate::descriptors::svc_extension::SvcExtensionDescriptor,
238 MvcExtension = 0x31 => crate::descriptors::mvc_extension::MvcExtensionDescriptor,
239 J2kVideo = 0x32 => crate::descriptors::j2k_video::J2kVideoDescriptor<'a>,
240 HevcVideo = 0x38 => crate::descriptors::hevc_video::HevcVideoDescriptor,
241 NetworkName = 0x40 => crate::descriptors::network_name::NetworkNameDescriptor<'a>,
243 ServiceList = 0x41 => crate::descriptors::service_list::ServiceListDescriptor,
244 Stuffing = 0x42 => crate::descriptors::stuffing::StuffingDescriptor<'a>,
245 SatelliteDeliverySystem = 0x43 => crate::descriptors::satellite_delivery_system::SatelliteDeliverySystemDescriptor,
246 CableDeliverySystem = 0x44 => crate::descriptors::cable_delivery_system::CableDeliverySystemDescriptor,
247 VbiData = 0x45 => crate::descriptors::vbi_data::VbiDataDescriptor<'a>,
248 VbiTeletext = 0x46 => crate::descriptors::vbi_teletext::VbiTeletextDescriptor,
249 BouquetName = 0x47 => crate::descriptors::bouquet_name::BouquetNameDescriptor<'a>,
250 Service = 0x48 => crate::descriptors::service::ServiceDescriptor<'a>,
251 CountryAvailability = 0x49 => crate::descriptors::country_availability::CountryAvailabilityDescriptor,
252 Linkage = 0x4A => crate::descriptors::linkage::LinkageDescriptor<'a>,
253 NvodReference = 0x4B => crate::descriptors::nvod_reference::NvodReferenceDescriptor,
254 TimeShiftedService = 0x4C => crate::descriptors::time_shifted_service::TimeShiftedServiceDescriptor,
255 ShortEvent = 0x4D => crate::descriptors::short_event::ShortEventDescriptor<'a>,
256 ExtendedEvent = 0x4E => crate::descriptors::extended_event::ExtendedEventDescriptor<'a>,
257 TimeShiftedEvent = 0x4F => crate::descriptors::time_shifted_event::TimeShiftedEventDescriptor,
258 Component = 0x50 => crate::descriptors::component::ComponentDescriptor<'a>,
259 Mosaic = 0x51 => crate::descriptors::mosaic::MosaicDescriptor,
260 StreamIdentifier = 0x52 => crate::descriptors::stream_identifier::StreamIdentifierDescriptor,
261 CaIdentifier = 0x53 => crate::descriptors::ca_identifier::CaIdentifierDescriptor,
262 Content = 0x54 => crate::descriptors::content::ContentDescriptor,
263 ParentalRating = 0x55 => crate::descriptors::parental_rating::ParentalRatingDescriptor,
264 Teletext = 0x56 => crate::descriptors::teletext::TeletextDescriptor,
265 Telephone = 0x57 => crate::descriptors::telephone::TelephoneDescriptor<'a>,
266 LocalTimeOffset = 0x58 => crate::descriptors::local_time_offset::LocalTimeOffsetDescriptor,
267 Subtitling = 0x59 => crate::descriptors::subtitling::SubtitlingDescriptor,
268 TerrestrialDeliverySystem = 0x5A => crate::descriptors::terrestrial_delivery_system::TerrestrialDeliverySystemDescriptor,
269 MultilingualNetworkName = 0x5B => crate::descriptors::multilingual_network_name::MultilingualNetworkNameDescriptor<'a>,
270 MultilingualBouquetName = 0x5C => crate::descriptors::multilingual_bouquet_name::MultilingualBouquetNameDescriptor<'a>,
271 MultilingualServiceName = 0x5D => crate::descriptors::multilingual_service_name::MultilingualServiceNameDescriptor<'a>,
272 MultilingualComponent = 0x5E => crate::descriptors::multilingual_component::MultilingualComponentDescriptor<'a>,
273 PrivateDataSpecifier = 0x5F => crate::descriptors::private_data_specifier::PrivateDataSpecifierDescriptor,
274 ServiceMove = 0x60 => crate::descriptors::service_move::ServiceMoveDescriptor,
275 ShortSmoothingBuffer = 0x61 => crate::descriptors::short_smoothing_buffer::ShortSmoothingBufferDescriptor<'a>,
276 FrequencyList = 0x62 => crate::descriptors::frequency_list::FrequencyListDescriptor,
277 PartialTransportStream = 0x63 => crate::descriptors::partial_transport_stream::PartialTransportStreamDescriptor,
278 DataBroadcast = 0x64 => crate::descriptors::data_broadcast::DataBroadcastDescriptor<'a>,
279 Scrambling = 0x65 => crate::descriptors::scrambling::ScramblingDescriptor,
280 DataBroadcastId = 0x66 => crate::descriptors::data_broadcast_id::DataBroadcastIdDescriptor<'a>,
281 TransportStream = 0x67 => crate::descriptors::transport_stream::TransportStreamDescriptor<'a>,
282 Dsng = 0x68 => crate::descriptors::dsng::DsngDescriptor<'a>,
283 Pdc = 0x69 => crate::descriptors::pdc::PdcDescriptor,
284 Ac3 = 0x6A => crate::descriptors::ac3::Ac3Descriptor<'a>,
285 AncillaryData = 0x6B => crate::descriptors::ancillary_data::AncillaryDataDescriptor,
286 CellList = 0x6C => crate::descriptors::cell_list::CellListDescriptor,
287 CellFrequencyLink = 0x6D => crate::descriptors::cell_frequency_link::CellFrequencyLinkDescriptor,
288 AnnouncementSupport = 0x6E => crate::descriptors::announcement_support::AnnouncementSupportDescriptor,
289 ApplicationSignalling = 0x6F => crate::descriptors::application_signalling::ApplicationSignallingDescriptor,
290 AdaptationFieldData = 0x70 => crate::descriptors::adaptation_field_data::AdaptationFieldDataDescriptor,
291 ServiceIdentifier = 0x71 => crate::descriptors::service_identifier::ServiceIdentifierDescriptor<'a>,
292 ServiceAvailability = 0x72 => crate::descriptors::service_availability::ServiceAvailabilityDescriptor,
293 DefaultAuthority = 0x73 => crate::descriptors::default_authority::DefaultAuthorityDescriptor<'a>,
294 RelatedContent = 0x74 => crate::descriptors::related_content::RelatedContentDescriptor,
295 TvaId = 0x75 => crate::descriptors::tva_id::TvaIdDescriptor,
296 ContentIdentifier = 0x76 => crate::descriptors::content_identifier::ContentIdentifierDescriptor<'a>,
297 TimeSliceFecIdentifier = 0x77 => crate::descriptors::time_slice_fec_identifier::TimeSliceFecIdentifierDescriptor<'a>,
298 EcmRepetitionRate = 0x78 => crate::descriptors::ecm_repetition_rate::EcmRepetitionRateDescriptor<'a>,
299 S2SatelliteDeliverySystem = 0x79 => crate::descriptors::s2_satellite_delivery_system::S2SatelliteDeliverySystemDescriptor,
300 EnhancedAc3 = 0x7A => crate::descriptors::enhanced_ac3::EnhancedAc3Descriptor<'a>,
301 Dts = 0x7B => crate::descriptors::dts::DtsDescriptor<'a>,
302 Aac = 0x7C => crate::descriptors::aac::AacDescriptor<'a>,
303 XaitLocation = 0x7D => crate::descriptors::xait_location::XaitLocationDescriptor,
304 FtaContentManagement = 0x7E => crate::descriptors::fta_content_management::FtaContentManagementDescriptor,
305 Extension = 0x7F => crate::descriptors::extension::ExtensionDescriptor<'a>;
306 @no_dispatch
310 LogicalChannel => crate::descriptors::logical_channel::LogicalChannelDescriptor,
311 NordigLogicalChannelV1 => crate::descriptors::nordig::NordigLogicalChannelV1,
312 NordigLogicalChannelV2 => crate::descriptors::nordig::NordigLogicalChannelV2,
313}
314
315#[must_use]
322pub fn parse_loop(bytes: &[u8]) -> DescriptorIter<'_> {
323 DescriptorIter {
324 bytes,
325 pos: 0,
326 fused: false,
327 }
328}
329
330pub(crate) fn next_loop_entry<'a>(
338 bytes: &'a [u8],
339 pos: &mut usize,
340 fused: &mut bool,
341) -> Option<crate::Result<(u8, &'a [u8])>> {
342 if *fused || *pos >= bytes.len() {
343 return None;
344 }
345 let rem = &bytes[*pos..];
346 if rem.len() < 2 {
347 *fused = true;
348 return Some(Err(crate::Error::BufferTooShort {
349 need: 2,
350 have: rem.len(),
351 what: "descriptor header in loop",
352 }));
353 }
354 let tag = rem[0];
355 let len = rem[1] as usize;
356 let total = 2 + len;
357 if rem.len() < total {
358 *fused = true;
359 return Some(Err(crate::Error::BufferTooShort {
360 need: total,
361 have: rem.len(),
362 what: "descriptor body in loop",
363 }));
364 }
365 let full = &rem[..total];
366 *pos += total;
367 Some(Ok((tag, full)))
368}
369
370#[derive(Debug, Clone)]
372pub struct DescriptorIter<'a> {
373 bytes: &'a [u8],
374 pos: usize,
375 fused: bool,
376}
377
378impl<'a> Iterator for DescriptorIter<'a> {
379 type Item = crate::Result<AnyDescriptor<'a>>;
380
381 fn next(&mut self) -> Option<Self::Item> {
382 let (tag, full) = match next_loop_entry(self.bytes, &mut self.pos, &mut self.fused)? {
383 Ok(v) => v,
384 Err(e) => return Some(Err(e)),
385 };
386 Some(match AnyDescriptor::dispatch(tag, full) {
387 Some(res) => res,
388 None => Ok(AnyDescriptor::Unknown {
389 tag,
390 body: &full[2..],
391 }),
392 })
393 }
394}
395
396impl core::iter::FusedIterator for DescriptorIter<'_> {}
397
398#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
422#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
423pub struct DescriptorLoop<'a>(&'a [u8]);
424
425const DESC_HEADER_LEN: usize = 2;
427
428impl<'a> DescriptorLoop<'a> {
429 #[must_use]
432 pub const fn new(raw: &'a [u8]) -> Self {
433 Self(raw)
434 }
435
436 #[must_use]
439 pub const fn raw(&self) -> &'a [u8] {
440 self.0
441 }
442
443 #[must_use]
447 pub fn iter(&self) -> DescriptorIter<'a> {
448 parse_loop(self.0)
449 }
450
451 pub fn raw_tags(&self) -> impl Iterator<Item = (u8, &'a [u8])> {
461 let b = self.0;
462 let mut pos = 0usize;
463 core::iter::from_fn(move || {
464 if pos + DESC_HEADER_LEN > b.len() {
465 return None;
466 }
467 let tag = b[pos];
468 let len = b[pos + 1] as usize;
469 let end = pos + DESC_HEADER_LEN + len;
470 if end > b.len() {
471 return None; }
473 let body = &b[pos + DESC_HEADER_LEN..end];
474 pos = end;
475 Some((tag, body))
476 })
477 }
478
479 #[must_use]
484 pub fn contains_tag(&self, tag: u8) -> bool {
485 let b = self.0;
486 let mut pos = 0usize;
487 while pos < b.len() {
488 if b[pos] == tag {
489 return true;
490 }
491 if pos + 1 >= b.len() {
494 break;
495 }
496 pos += DESC_HEADER_LEN + b[pos + 1] as usize;
497 }
498 false
499 }
500
501 #[must_use]
509 pub fn iter_with<'r>(
510 &self,
511 registry: &'r crate::descriptors::registry::DescriptorRegistry,
512 ) -> crate::descriptors::registry::RegistryIter<'r, 'a> {
513 registry.parse_loop(self.0)
514 }
515
516 #[must_use]
529 pub fn iter_with_extensions<'r>(
530 &self,
531 desc_reg: &'r crate::descriptors::registry::DescriptorRegistry,
532 ext_reg: &'r crate::descriptors::extension::registry::ExtensionRegistry,
533 ) -> crate::descriptors::registry::ExtRegistryIter<'r, 'a> {
534 crate::descriptors::registry::ExtRegistryIter::new(desc_reg, ext_reg, self.0)
535 }
536}
537
538impl<'a> core::ops::Deref for DescriptorLoop<'a> {
539 type Target = [u8];
543 fn deref(&self) -> &[u8] {
544 self.0
545 }
546}
547
548impl<'a> From<&'a [u8]> for DescriptorLoop<'a> {
549 fn from(raw: &'a [u8]) -> Self {
550 Self(raw)
551 }
552}
553
554impl core::fmt::Debug for DescriptorLoop<'_> {
555 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
557 write!(f, "DescriptorLoop(<{} bytes>)", self.0.len())
558 }
559}
560
561impl<'a> IntoIterator for &DescriptorLoop<'a> {
562 type Item = crate::Result<AnyDescriptor<'a>>;
563 type IntoIter = DescriptorIter<'a>;
564 fn into_iter(self) -> Self::IntoIter {
565 self.iter()
566 }
567}
568
569#[cfg(feature = "serde")]
570impl serde::Serialize for DescriptorLoop<'_> {
571 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
576 struct Entry<'a>(crate::Result<AnyDescriptor<'a>>);
577 impl serde::Serialize for Entry<'_> {
578 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
579 match &self.0 {
580 Ok(d) => d.serialize(s),
581 Err(e) => {
582 use serde::ser::SerializeMap;
583 let mut m = s.serialize_map(Some(1))?;
584 m.serialize_entry("parseError", &e.to_string())?;
585 m.end()
586 }
587 }
588 }
589 }
590 s.collect_seq(self.iter().map(Entry))
591 }
592}
593#[cfg(test)]
599mod tests {
600 use super::*;
601
602 #[test]
603 fn contains_tag_detects_present_tag_regardless_of_body() {
604 assert!(DescriptorLoop::new(&[0x6A, 0x01, 0x80]).contains_tag(0x6A));
606 assert!(DescriptorLoop::new(&[0x6A, 0x00]).contains_tag(0x6A));
608 assert!(DescriptorLoop::new(&[0x6A, 0x01]).contains_tag(0x6A));
610 assert!(DescriptorLoop::new(&[0x09, 0x02, 0x00, 0x00, 0x7A, 0x00]).contains_tag(0x7A));
612 assert!(!DescriptorLoop::new(&[0x09, 0x02, 0x00, 0x00]).contains_tag(0x6A));
614 assert!(!DescriptorLoop::new(&[]).contains_tag(0x6A));
615 }
616
617 #[test]
618 fn raw_tags_walks_tlv_without_typed_parsing() {
619 let loop_ = DescriptorLoop::new(&[0x6A, 0x00, 0x40, 0x02, 0xAA, 0xBB]);
621 let pairs: Vec<_> = loop_.raw_tags().collect();
622 assert_eq!(pairs, vec![(0x6A, &[][..]), (0x40, &[0xAA, 0xBB][..])]);
623 let trunc = DescriptorLoop::new(&[0x40, 0x01, 0xAA, 0x6A, 0x05]);
626 let tags: Vec<u8> = trunc.raw_tags().map(|(t, _)| t).collect();
627 assert_eq!(tags, vec![0x40]); assert!(trunc.contains_tag(0x6A));
630 }
631
632 #[test]
633 fn unknown_tag_yields_unknown_with_body_sans_header() {
634 let bytes = [0xA7, 0x02, 0xDE, 0xAD];
636 let items: Vec<_> = parse_loop(&bytes).collect();
637 assert_eq!(items.len(), 1);
638 match items[0].as_ref().unwrap() {
639 AnyDescriptor::Unknown { tag, body } => {
640 assert_eq!(*tag, 0xA7);
641 assert_eq!(*body, &[0xDE, 0xAD]);
642 }
643 other => panic!("expected Unknown, got {other:?}"),
644 }
645 }
646
647 #[test]
648 fn empty_loop_yields_nothing() {
649 assert_eq!(parse_loop(&[]).count(), 0);
650 }
651
652 #[test]
653 fn logical_channel_0x83_is_not_dispatched() {
654 let bytes = [0x83, 0x04, 0x00, 0x01, 0xFC, 0x01];
656 let items: Vec<_> = parse_loop(&bytes).collect();
657 assert_eq!(items.len(), 1);
658 assert!(matches!(
659 items[0].as_ref().unwrap(),
660 AnyDescriptor::Unknown { tag: 0x83, .. }
661 ));
662 }
663
664 #[test]
665 fn descriptor_loop_iter_matches_parse_loop() {
666 let raw = [
667 0x4D, 0x07, b'e', b'n', b'g', 0x02, b'H', b'i', 0x00, 0xA7, 0x02, 0xCA, 0xFE, ];
670 let via_loop: Vec<_> = DescriptorLoop::new(&raw)
671 .iter()
672 .map(|r| format!("{r:?}"))
673 .collect();
674 let via_fn: Vec<_> = parse_loop(&raw).map(|r| format!("{r:?}")).collect();
675 assert_eq!(via_loop, via_fn);
676 assert_eq!(DescriptorLoop::new(&raw).raw(), &raw[..]);
678 assert_eq!(DescriptorLoop::new(&raw).len(), raw.len());
679 let count = (&DescriptorLoop::new(&raw)).into_iter().count();
681 assert_eq!(count, 2);
682 }
683
684 #[test]
685 fn descriptor_loop_debug_is_cheap() {
686 let raw = [0x4D, 0x02, 0x01, 0x02];
687 assert_eq!(
688 format!("{:?}", DescriptorLoop::new(&raw)),
689 "DescriptorLoop(<4 bytes>)"
690 );
691 }
692
693 #[test]
694 fn iter_with_custom_tag_yields_other() {
695 use crate::descriptors::registry::DescriptorRegistry;
696 use crate::traits::DescriptorDef;
697
698 #[derive(Debug, PartialEq, Eq)]
699 #[cfg_attr(feature = "serde", derive(serde::Serialize))]
700 struct MyTag0xA7 {
701 x: u8,
702 }
703
704 impl<'a> dvb_common::Parse<'a> for MyTag0xA7 {
705 type Error = crate::error::Error;
706 fn parse(bytes: &'a [u8]) -> crate::Result<Self> {
707 if bytes.len() < 3 {
708 return Err(crate::error::Error::BufferTooShort {
709 need: 3,
710 have: bytes.len(),
711 what: "MyTag0xA7",
712 });
713 }
714 Ok(Self { x: bytes[2] })
715 }
716 }
717
718 impl<'a> DescriptorDef<'a> for MyTag0xA7 {
719 const TAG: u8 = 0xA7;
720 const NAME: &'static str = "MY_TAG_0xA7";
721 }
722
723 let mut reg = DescriptorRegistry::new();
724 reg.register::<MyTag0xA7>();
725
726 let raw = [
727 0x4D, 0x07, b'e', b'n', b'g', 0x02, b'H', b'i', 0x00, 0xA7, 0x02, 0xCA, 0xFE,
728 ];
729 let loop_ = DescriptorLoop::new(&raw);
730 let items: Vec<_> = loop_.iter_with(®).collect::<Result<_, _>>().unwrap();
731 assert_eq!(items.len(), 2);
732 assert!(matches!(items[0], AnyDescriptor::ShortEvent(_)));
733 match &items[1] {
734 AnyDescriptor::Other { tag, value } => {
735 assert_eq!(*tag, 0xA7);
736 assert_eq!(value.downcast_ref::<MyTag0xA7>().unwrap().x, 0xCA);
737 }
738 other => panic!("expected Other, got {other:?}"),
739 }
740 }
741
742 #[test]
743 fn iter_with_empty_registry_matches_iter_for_builtin() {
744 let raw = [
745 0x4D, 0x07, b'e', b'n', b'g', 0x02, b'H', b'i', 0x00, 0xA7, 0x02, 0xCA, 0xFE,
746 ];
747 let loop_ = DescriptorLoop::new(&raw);
748 let reg = crate::descriptors::registry::DescriptorRegistry::new();
749 let via_iter: Vec<_> = loop_.iter().collect();
750 let via_iter_with: Vec<_> = loop_.iter_with(®).collect();
751
752 assert_eq!(via_iter.len(), via_iter_with.len());
753 for (a, b) in via_iter.iter().zip(via_iter_with.iter()) {
754 match (a, b) {
755 (Ok(AnyDescriptor::ShortEvent(_)), Ok(AnyDescriptor::ShortEvent(_))) => {}
756 (
757 Ok(AnyDescriptor::Unknown { tag: t1, body: b1 }),
758 Ok(AnyDescriptor::Unknown { tag: t2, body: b2 }),
759 ) => {
760 assert_eq!(t1, t2);
761 assert_eq!(b1, b2);
762 }
763 (Err(_), Err(_)) => {}
764 (l, r) => panic!("mismatch: {l:?} vs {r:?}"),
765 }
766 }
767 }
768
769 #[cfg(feature = "serde")]
770 #[test]
771 fn descriptor_loop_serializes_typed_unknown_and_parse_error() {
772 let raw = [
776 0x4D, 0x07, b'e', b'n', b'g', 0x02, b'H', b'i', 0x00, 0xA7, 0x02, 0xCA, 0xFE, 0x55, 0x05, 0x00, ];
780 let v = serde_json::to_value(DescriptorLoop::new(&raw)).unwrap();
781 let arr = v.as_array().expect("sequence");
782 assert_eq!(arr.len(), 3);
783 assert!(arr[0].get("shortEvent").is_some(), "got {}", arr[0]);
785 assert_eq!(arr[0]["shortEvent"]["event_name"], "Hi");
786 let unknown = arr[1].get("unknown").expect("unknown variant");
788 assert_eq!(unknown["tag"], 0xA7);
789 assert_eq!(unknown["body"], serde_json::json!([0xCA, 0xFE]));
790 assert!(
792 arr[2].get("parseError").is_some(),
793 "expected parseError, got {}",
794 arr[2]
795 );
796 }
797}