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 AssociationTag = 0x14 => crate::descriptors::association_tag::AssociationTagDescriptor<'a>,
220 Mpeg4Video = 0x1B => crate::descriptors::mpeg4_video::Mpeg4VideoDescriptor,
221 Mpeg4Audio = 0x1C => crate::descriptors::mpeg4_audio::Mpeg4AudioDescriptor,
222 Iod = 0x1D => crate::descriptors::iod::IodDescriptor<'a>,
223 Sl = 0x1E => crate::descriptors::sl::SlDescriptor,
224 Fmc = 0x1F => crate::descriptors::fmc::FmcDescriptor,
225 ExternalEsId = 0x20 => crate::descriptors::external_es_id::ExternalEsIdDescriptor,
226 Muxcode = 0x21 => crate::descriptors::muxcode::MuxcodeDescriptor<'a>,
227 FmxBufferSize = 0x22 => crate::descriptors::fmx_buffer_size::FmxBufferSizeDescriptor<'a>,
228 MultiplexBuffer = 0x23 => crate::descriptors::multiplex_buffer::MultiplexBufferDescriptor,
229 ContentLabeling = 0x24 => crate::descriptors::content_labeling::ContentLabelingDescriptor<'a>,
230 MetadataPointer = 0x25 => crate::descriptors::metadata_pointer::MetadataPointerDescriptor<'a>,
231 Metadata = 0x26 => crate::descriptors::metadata::MetadataDescriptor<'a>,
232 MetadataStd = 0x27 => crate::descriptors::metadata_std::MetadataStdDescriptor,
233 AvcVideo = 0x28 => crate::descriptors::avc_video::AvcVideoDescriptor,
234 AvcTimingAndHrd = 0x2A => crate::descriptors::avc_timing_and_hrd::AvcTimingAndHrdDescriptor,
235 Mpeg2AacAudio = 0x2B => crate::descriptors::mpeg2_aac_audio::Mpeg2AacAudioDescriptor,
236 FlexMuxTiming = 0x2C => crate::descriptors::flex_mux_timing::FlexMuxTimingDescriptor,
237 AuxiliaryVideoStream = 0x2F => crate::descriptors::auxiliary_video_stream::AuxiliaryVideoStreamDescriptor<'a>,
238 SvcExtension = 0x30 => crate::descriptors::svc_extension::SvcExtensionDescriptor,
239 MvcExtension = 0x31 => crate::descriptors::mvc_extension::MvcExtensionDescriptor,
240 J2kVideo = 0x32 => crate::descriptors::j2k_video::J2kVideoDescriptor<'a>,
241 HevcVideo = 0x38 => crate::descriptors::hevc_video::HevcVideoDescriptor,
242 NetworkName = 0x40 => crate::descriptors::network_name::NetworkNameDescriptor<'a>,
244 ServiceList = 0x41 => crate::descriptors::service_list::ServiceListDescriptor,
245 Stuffing = 0x42 => crate::descriptors::stuffing::StuffingDescriptor<'a>,
246 SatelliteDeliverySystem = 0x43 => crate::descriptors::satellite_delivery_system::SatelliteDeliverySystemDescriptor,
247 CableDeliverySystem = 0x44 => crate::descriptors::cable_delivery_system::CableDeliverySystemDescriptor,
248 VbiData = 0x45 => crate::descriptors::vbi_data::VbiDataDescriptor<'a>,
249 VbiTeletext = 0x46 => crate::descriptors::vbi_teletext::VbiTeletextDescriptor,
250 BouquetName = 0x47 => crate::descriptors::bouquet_name::BouquetNameDescriptor<'a>,
251 Service = 0x48 => crate::descriptors::service::ServiceDescriptor<'a>,
252 CountryAvailability = 0x49 => crate::descriptors::country_availability::CountryAvailabilityDescriptor,
253 Linkage = 0x4A => crate::descriptors::linkage::LinkageDescriptor<'a>,
254 NvodReference = 0x4B => crate::descriptors::nvod_reference::NvodReferenceDescriptor,
255 TimeShiftedService = 0x4C => crate::descriptors::time_shifted_service::TimeShiftedServiceDescriptor,
256 ShortEvent = 0x4D => crate::descriptors::short_event::ShortEventDescriptor<'a>,
257 ExtendedEvent = 0x4E => crate::descriptors::extended_event::ExtendedEventDescriptor<'a>,
258 TimeShiftedEvent = 0x4F => crate::descriptors::time_shifted_event::TimeShiftedEventDescriptor,
259 Component = 0x50 => crate::descriptors::component::ComponentDescriptor<'a>,
260 Mosaic = 0x51 => crate::descriptors::mosaic::MosaicDescriptor,
261 StreamIdentifier = 0x52 => crate::descriptors::stream_identifier::StreamIdentifierDescriptor,
262 CaIdentifier = 0x53 => crate::descriptors::ca_identifier::CaIdentifierDescriptor,
263 Content = 0x54 => crate::descriptors::content::ContentDescriptor,
264 ParentalRating = 0x55 => crate::descriptors::parental_rating::ParentalRatingDescriptor,
265 Teletext = 0x56 => crate::descriptors::teletext::TeletextDescriptor,
266 Telephone = 0x57 => crate::descriptors::telephone::TelephoneDescriptor<'a>,
267 LocalTimeOffset = 0x58 => crate::descriptors::local_time_offset::LocalTimeOffsetDescriptor,
268 Subtitling = 0x59 => crate::descriptors::subtitling::SubtitlingDescriptor,
269 TerrestrialDeliverySystem = 0x5A => crate::descriptors::terrestrial_delivery_system::TerrestrialDeliverySystemDescriptor,
270 MultilingualNetworkName = 0x5B => crate::descriptors::multilingual_network_name::MultilingualNetworkNameDescriptor<'a>,
271 MultilingualBouquetName = 0x5C => crate::descriptors::multilingual_bouquet_name::MultilingualBouquetNameDescriptor<'a>,
272 MultilingualServiceName = 0x5D => crate::descriptors::multilingual_service_name::MultilingualServiceNameDescriptor<'a>,
273 MultilingualComponent = 0x5E => crate::descriptors::multilingual_component::MultilingualComponentDescriptor<'a>,
274 PrivateDataSpecifier = 0x5F => crate::descriptors::private_data_specifier::PrivateDataSpecifierDescriptor,
275 ServiceMove = 0x60 => crate::descriptors::service_move::ServiceMoveDescriptor,
276 ShortSmoothingBuffer = 0x61 => crate::descriptors::short_smoothing_buffer::ShortSmoothingBufferDescriptor<'a>,
277 FrequencyList = 0x62 => crate::descriptors::frequency_list::FrequencyListDescriptor,
278 PartialTransportStream = 0x63 => crate::descriptors::partial_transport_stream::PartialTransportStreamDescriptor,
279 DataBroadcast = 0x64 => crate::descriptors::data_broadcast::DataBroadcastDescriptor<'a>,
280 Scrambling = 0x65 => crate::descriptors::scrambling::ScramblingDescriptor,
281 DataBroadcastId = 0x66 => crate::descriptors::data_broadcast_id::DataBroadcastIdDescriptor<'a>,
282 TransportStream = 0x67 => crate::descriptors::transport_stream::TransportStreamDescriptor<'a>,
283 Dsng = 0x68 => crate::descriptors::dsng::DsngDescriptor<'a>,
284 Pdc = 0x69 => crate::descriptors::pdc::PdcDescriptor,
285 Ac3 = 0x6A => crate::descriptors::ac3::Ac3Descriptor<'a>,
286 AncillaryData = 0x6B => crate::descriptors::ancillary_data::AncillaryDataDescriptor,
287 CellList = 0x6C => crate::descriptors::cell_list::CellListDescriptor,
288 CellFrequencyLink = 0x6D => crate::descriptors::cell_frequency_link::CellFrequencyLinkDescriptor,
289 AnnouncementSupport = 0x6E => crate::descriptors::announcement_support::AnnouncementSupportDescriptor,
290 ApplicationSignalling = 0x6F => crate::descriptors::application_signalling::ApplicationSignallingDescriptor,
291 AdaptationFieldData = 0x70 => crate::descriptors::adaptation_field_data::AdaptationFieldDataDescriptor,
292 ServiceIdentifier = 0x71 => crate::descriptors::service_identifier::ServiceIdentifierDescriptor<'a>,
293 ServiceAvailability = 0x72 => crate::descriptors::service_availability::ServiceAvailabilityDescriptor,
294 DefaultAuthority = 0x73 => crate::descriptors::default_authority::DefaultAuthorityDescriptor<'a>,
295 RelatedContent = 0x74 => crate::descriptors::related_content::RelatedContentDescriptor,
296 TvaId = 0x75 => crate::descriptors::tva_id::TvaIdDescriptor,
297 ContentIdentifier = 0x76 => crate::descriptors::content_identifier::ContentIdentifierDescriptor<'a>,
298 TimeSliceFecIdentifier = 0x77 => crate::descriptors::time_slice_fec_identifier::TimeSliceFecIdentifierDescriptor<'a>,
299 EcmRepetitionRate = 0x78 => crate::descriptors::ecm_repetition_rate::EcmRepetitionRateDescriptor<'a>,
300 S2SatelliteDeliverySystem = 0x79 => crate::descriptors::s2_satellite_delivery_system::S2SatelliteDeliverySystemDescriptor,
301 EnhancedAc3 = 0x7A => crate::descriptors::enhanced_ac3::EnhancedAc3Descriptor<'a>,
302 Dts = 0x7B => crate::descriptors::dts::DtsDescriptor<'a>,
303 Aac = 0x7C => crate::descriptors::aac::AacDescriptor<'a>,
304 XaitLocation = 0x7D => crate::descriptors::xait_location::XaitLocationDescriptor,
305 FtaContentManagement = 0x7E => crate::descriptors::fta_content_management::FtaContentManagementDescriptor,
306 Extension = 0x7F => crate::descriptors::extension::ExtensionDescriptor<'a>;
307 @no_dispatch
311 LogicalChannel => crate::descriptors::logical_channel::LogicalChannelDescriptor,
312 NordigLogicalChannelV1 => crate::descriptors::nordig::NordigLogicalChannelV1,
313 NordigLogicalChannelV2 => crate::descriptors::nordig::NordigLogicalChannelV2,
314}
315
316#[must_use]
323pub fn parse_loop(bytes: &[u8]) -> DescriptorIter<'_> {
324 DescriptorIter {
325 bytes,
326 pos: 0,
327 fused: false,
328 }
329}
330
331pub(crate) fn next_loop_entry<'a>(
339 bytes: &'a [u8],
340 pos: &mut usize,
341 fused: &mut bool,
342) -> Option<crate::Result<(u8, &'a [u8])>> {
343 if *fused || *pos >= bytes.len() {
344 return None;
345 }
346 let rem = &bytes[*pos..];
347 if rem.len() < 2 {
348 *fused = true;
349 return Some(Err(crate::Error::BufferTooShort {
350 need: 2,
351 have: rem.len(),
352 what: "descriptor header in loop",
353 }));
354 }
355 let tag = rem[0];
356 let len = rem[1] as usize;
357 let total = 2 + len;
358 if rem.len() < total {
359 *fused = true;
360 return Some(Err(crate::Error::BufferTooShort {
361 need: total,
362 have: rem.len(),
363 what: "descriptor body in loop",
364 }));
365 }
366 let full = &rem[..total];
367 *pos += total;
368 Some(Ok((tag, full)))
369}
370
371#[derive(Debug, Clone)]
373pub struct DescriptorIter<'a> {
374 bytes: &'a [u8],
375 pos: usize,
376 fused: bool,
377}
378
379impl<'a> Iterator for DescriptorIter<'a> {
380 type Item = crate::Result<AnyDescriptor<'a>>;
381
382 fn next(&mut self) -> Option<Self::Item> {
383 let (tag, full) = match next_loop_entry(self.bytes, &mut self.pos, &mut self.fused)? {
384 Ok(v) => v,
385 Err(e) => return Some(Err(e)),
386 };
387 Some(match AnyDescriptor::dispatch(tag, full) {
388 Some(res) => res,
389 None => Ok(AnyDescriptor::Unknown {
390 tag,
391 body: &full[2..],
392 }),
393 })
394 }
395}
396
397impl core::iter::FusedIterator for DescriptorIter<'_> {}
398
399#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
423#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
424pub struct DescriptorLoop<'a>(&'a [u8]);
425
426const DESC_HEADER_LEN: usize = 2;
428
429impl<'a> DescriptorLoop<'a> {
430 #[must_use]
433 pub const fn new(raw: &'a [u8]) -> Self {
434 Self(raw)
435 }
436
437 #[must_use]
440 pub const fn raw(&self) -> &'a [u8] {
441 self.0
442 }
443
444 #[must_use]
448 pub fn iter(&self) -> DescriptorIter<'a> {
449 parse_loop(self.0)
450 }
451
452 pub fn raw_tags(&self) -> impl Iterator<Item = (u8, &'a [u8])> {
462 let b = self.0;
463 let mut pos = 0usize;
464 core::iter::from_fn(move || {
465 if pos + DESC_HEADER_LEN > b.len() {
466 return None;
467 }
468 let tag = b[pos];
469 let len = b[pos + 1] as usize;
470 let end = pos + DESC_HEADER_LEN + len;
471 if end > b.len() {
472 return None; }
474 let body = &b[pos + DESC_HEADER_LEN..end];
475 pos = end;
476 Some((tag, body))
477 })
478 }
479
480 #[must_use]
485 pub fn contains_tag(&self, tag: u8) -> bool {
486 let b = self.0;
487 let mut pos = 0usize;
488 while pos < b.len() {
489 if b[pos] == tag {
490 return true;
491 }
492 if pos + 1 >= b.len() {
495 break;
496 }
497 pos += DESC_HEADER_LEN + b[pos + 1] as usize;
498 }
499 false
500 }
501
502 #[must_use]
510 pub fn iter_with<'r>(
511 &self,
512 registry: &'r crate::descriptors::registry::DescriptorRegistry,
513 ) -> crate::descriptors::registry::RegistryIter<'r, 'a> {
514 registry.parse_loop(self.0)
515 }
516
517 #[must_use]
530 pub fn iter_with_extensions<'r>(
531 &self,
532 desc_reg: &'r crate::descriptors::registry::DescriptorRegistry,
533 ext_reg: &'r crate::descriptors::extension::registry::ExtensionRegistry,
534 ) -> crate::descriptors::registry::ExtRegistryIter<'r, 'a> {
535 crate::descriptors::registry::ExtRegistryIter::new(desc_reg, ext_reg, self.0)
536 }
537}
538
539impl<'a> core::ops::Deref for DescriptorLoop<'a> {
540 type Target = [u8];
544 fn deref(&self) -> &[u8] {
545 self.0
546 }
547}
548
549impl<'a> From<&'a [u8]> for DescriptorLoop<'a> {
550 fn from(raw: &'a [u8]) -> Self {
551 Self(raw)
552 }
553}
554
555impl core::fmt::Debug for DescriptorLoop<'_> {
556 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
558 write!(f, "DescriptorLoop(<{} bytes>)", self.0.len())
559 }
560}
561
562impl<'a> IntoIterator for &DescriptorLoop<'a> {
563 type Item = crate::Result<AnyDescriptor<'a>>;
564 type IntoIter = DescriptorIter<'a>;
565 fn into_iter(self) -> Self::IntoIter {
566 self.iter()
567 }
568}
569
570#[cfg(feature = "serde")]
571impl serde::Serialize for DescriptorLoop<'_> {
572 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
577 struct Entry<'a>(crate::Result<AnyDescriptor<'a>>);
578 impl serde::Serialize for Entry<'_> {
579 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
580 match &self.0 {
581 Ok(d) => d.serialize(s),
582 Err(e) => {
583 use serde::ser::SerializeMap;
584 let mut m = s.serialize_map(Some(1))?;
585 m.serialize_entry("parseError", &e.to_string())?;
586 m.end()
587 }
588 }
589 }
590 }
591 s.collect_seq(self.iter().map(Entry))
592 }
593}
594#[cfg(test)]
600mod tests {
601 use super::*;
602
603 #[test]
604 fn contains_tag_detects_present_tag_regardless_of_body() {
605 assert!(DescriptorLoop::new(&[0x6A, 0x01, 0x80]).contains_tag(0x6A));
607 assert!(DescriptorLoop::new(&[0x6A, 0x00]).contains_tag(0x6A));
609 assert!(DescriptorLoop::new(&[0x6A, 0x01]).contains_tag(0x6A));
611 assert!(DescriptorLoop::new(&[0x09, 0x02, 0x00, 0x00, 0x7A, 0x00]).contains_tag(0x7A));
613 assert!(!DescriptorLoop::new(&[0x09, 0x02, 0x00, 0x00]).contains_tag(0x6A));
615 assert!(!DescriptorLoop::new(&[]).contains_tag(0x6A));
616 }
617
618 #[test]
619 fn raw_tags_walks_tlv_without_typed_parsing() {
620 let loop_ = DescriptorLoop::new(&[0x6A, 0x00, 0x40, 0x02, 0xAA, 0xBB]);
622 let pairs: Vec<_> = loop_.raw_tags().collect();
623 assert_eq!(pairs, vec![(0x6A, &[][..]), (0x40, &[0xAA, 0xBB][..])]);
624 let trunc = DescriptorLoop::new(&[0x40, 0x01, 0xAA, 0x6A, 0x05]);
627 let tags: Vec<u8> = trunc.raw_tags().map(|(t, _)| t).collect();
628 assert_eq!(tags, vec![0x40]); assert!(trunc.contains_tag(0x6A));
631 }
632
633 #[test]
634 fn unknown_tag_yields_unknown_with_body_sans_header() {
635 let bytes = [0xA7, 0x02, 0xDE, 0xAD];
637 let items: Vec<_> = parse_loop(&bytes).collect();
638 assert_eq!(items.len(), 1);
639 match items[0].as_ref().unwrap() {
640 AnyDescriptor::Unknown { tag, body } => {
641 assert_eq!(*tag, 0xA7);
642 assert_eq!(*body, &[0xDE, 0xAD]);
643 }
644 other => panic!("expected Unknown, got {other:?}"),
645 }
646 }
647
648 #[test]
649 fn empty_loop_yields_nothing() {
650 assert_eq!(parse_loop(&[]).count(), 0);
651 }
652
653 #[test]
654 fn logical_channel_0x83_is_not_dispatched() {
655 let bytes = [0x83, 0x04, 0x00, 0x01, 0xFC, 0x01];
657 let items: Vec<_> = parse_loop(&bytes).collect();
658 assert_eq!(items.len(), 1);
659 assert!(matches!(
660 items[0].as_ref().unwrap(),
661 AnyDescriptor::Unknown { tag: 0x83, .. }
662 ));
663 }
664
665 #[test]
666 fn descriptor_loop_iter_matches_parse_loop() {
667 let raw = [
668 0x4D, 0x07, b'e', b'n', b'g', 0x02, b'H', b'i', 0x00, 0xA7, 0x02, 0xCA, 0xFE, ];
671 let via_loop: Vec<_> = DescriptorLoop::new(&raw)
672 .iter()
673 .map(|r| format!("{r:?}"))
674 .collect();
675 let via_fn: Vec<_> = parse_loop(&raw).map(|r| format!("{r:?}")).collect();
676 assert_eq!(via_loop, via_fn);
677 assert_eq!(DescriptorLoop::new(&raw).raw(), &raw[..]);
679 assert_eq!(DescriptorLoop::new(&raw).len(), raw.len());
680 let count = (&DescriptorLoop::new(&raw)).into_iter().count();
682 assert_eq!(count, 2);
683 }
684
685 #[test]
686 fn descriptor_loop_debug_is_cheap() {
687 let raw = [0x4D, 0x02, 0x01, 0x02];
688 assert_eq!(
689 format!("{:?}", DescriptorLoop::new(&raw)),
690 "DescriptorLoop(<4 bytes>)"
691 );
692 }
693
694 #[test]
695 fn iter_with_custom_tag_yields_other() {
696 use crate::descriptors::registry::DescriptorRegistry;
697 use crate::traits::DescriptorDef;
698
699 #[derive(Debug, PartialEq, Eq)]
700 #[cfg_attr(feature = "serde", derive(serde::Serialize))]
701 struct MyTag0xA7 {
702 x: u8,
703 }
704
705 impl<'a> dvb_common::Parse<'a> for MyTag0xA7 {
706 type Error = crate::error::Error;
707 fn parse(bytes: &'a [u8]) -> crate::Result<Self> {
708 if bytes.len() < 3 {
709 return Err(crate::error::Error::BufferTooShort {
710 need: 3,
711 have: bytes.len(),
712 what: "MyTag0xA7",
713 });
714 }
715 Ok(Self { x: bytes[2] })
716 }
717 }
718
719 impl<'a> DescriptorDef<'a> for MyTag0xA7 {
720 const TAG: u8 = 0xA7;
721 const NAME: &'static str = "MY_TAG_0xA7";
722 }
723
724 let mut reg = DescriptorRegistry::new();
725 reg.register::<MyTag0xA7>();
726
727 let raw = [
728 0x4D, 0x07, b'e', b'n', b'g', 0x02, b'H', b'i', 0x00, 0xA7, 0x02, 0xCA, 0xFE,
729 ];
730 let loop_ = DescriptorLoop::new(&raw);
731 let items: Vec<_> = loop_.iter_with(®).collect::<Result<_, _>>().unwrap();
732 assert_eq!(items.len(), 2);
733 assert!(matches!(items[0], AnyDescriptor::ShortEvent(_)));
734 match &items[1] {
735 AnyDescriptor::Other { tag, value } => {
736 assert_eq!(*tag, 0xA7);
737 assert_eq!(value.downcast_ref::<MyTag0xA7>().unwrap().x, 0xCA);
738 }
739 other => panic!("expected Other, got {other:?}"),
740 }
741 }
742
743 #[test]
744 fn iter_with_empty_registry_matches_iter_for_builtin() {
745 let raw = [
746 0x4D, 0x07, b'e', b'n', b'g', 0x02, b'H', b'i', 0x00, 0xA7, 0x02, 0xCA, 0xFE,
747 ];
748 let loop_ = DescriptorLoop::new(&raw);
749 let reg = crate::descriptors::registry::DescriptorRegistry::new();
750 let via_iter: Vec<_> = loop_.iter().collect();
751 let via_iter_with: Vec<_> = loop_.iter_with(®).collect();
752
753 assert_eq!(via_iter.len(), via_iter_with.len());
754 for (a, b) in via_iter.iter().zip(via_iter_with.iter()) {
755 match (a, b) {
756 (Ok(AnyDescriptor::ShortEvent(_)), Ok(AnyDescriptor::ShortEvent(_))) => {}
757 (
758 Ok(AnyDescriptor::Unknown { tag: t1, body: b1 }),
759 Ok(AnyDescriptor::Unknown { tag: t2, body: b2 }),
760 ) => {
761 assert_eq!(t1, t2);
762 assert_eq!(b1, b2);
763 }
764 (Err(_), Err(_)) => {}
765 (l, r) => panic!("mismatch: {l:?} vs {r:?}"),
766 }
767 }
768 }
769
770 #[cfg(feature = "serde")]
771 #[test]
772 fn descriptor_loop_serializes_typed_unknown_and_parse_error() {
773 let raw = [
777 0x4D, 0x07, b'e', b'n', b'g', 0x02, b'H', b'i', 0x00, 0xA7, 0x02, 0xCA, 0xFE, 0x55, 0x05, 0x00, ];
781 let v = serde_json::to_value(DescriptorLoop::new(&raw)).unwrap();
782 let arr = v.as_array().expect("sequence");
783 assert_eq!(arr.len(), 3);
784 assert!(arr[0].get("shortEvent").is_some(), "got {}", arr[0]);
786 assert_eq!(arr[0]["shortEvent"]["event_name"], "Hi");
787 let unknown = arr[1].get("unknown").expect("unknown variant");
789 assert_eq!(unknown["tag"], 0xA7);
790 assert_eq!(unknown["body"], serde_json::json!([0xCA, 0xFE]));
791 assert!(
793 arr[2].get("parseError").is_some(),
794 "expected parseError, got {}",
795 arr[2]
796 );
797 }
798}