dvb_common/lib.rs
1//! Shared primitives for the dvb_si / dvb_t2mi / dvb_bbframe family.
2//!
3//! See individual modules for documentation: the [`Parse`] / [`Serialize`]
4//! traits every wire type implements, the MPEG-2 [`crc32_mpeg2`] CRC, and the
5//! [`bcd`] / [`time`] codecs.
6//!
7//! # Quick start
8//! ```
9//! use dvb_common::{bcd, crc32_mpeg2};
10//!
11//! // Binary-coded decimal (as used in MJD/BCD time fields):
12//! assert_eq!(bcd::from_bcd_byte(0x42), Some(42));
13//! assert_eq!(bcd::to_bcd_byte(42), Some(0x42));
14//!
15//! // MPEG-2 CRC-32 over a section body (deterministic):
16//! let crc = crc32_mpeg2::compute(&[0xDE, 0xAD, 0xBE, 0xEF]);
17//! assert_eq!(crc, crc32_mpeg2::compute(&[0xDE, 0xAD, 0xBE, 0xEF]));
18//! ```
19
20#![forbid(unsafe_code)]
21#![warn(missing_docs)]
22#![cfg_attr(docsrs, feature(doc_cfg))]
23
24pub mod bcd;
25pub mod bits;
26pub mod crc32_mpeg2;
27pub mod time;
28pub mod traits;
29
30pub use traits::{Parse, Serialize};
31
32/// Generate a [`core::fmt::Display`] impl for a spec/field enum that delegates
33/// to an inherent `fn name(&self) -> &'static str`.
34///
35/// This is the project-wide convention for every public spec/field enum across
36/// the `dvb-*` crates (see issue #204): `name()` is the hand-written,
37/// zero-alloc static spec token (lossy on the reserved/unknown arm, which
38/// returns `"reserved"`), and `Display` is the lossless, composable view that
39/// delegates to it. The labels themselves live in `name()` in source — never in
40/// this macro — so they sit next to the variant docs and stay greppable. This
41/// macro carries no labels; it only removes the otherwise-identical `Display`
42/// boilerplate and keeps the two in lockstep.
43///
44/// # Forms
45/// - `impl_spec_display!(Ty)` — every variant's `Display` is exactly `name()`.
46/// Use when there is no byte-bearing catch-all (or its byte need not be
47/// shown), e.g. a unit `Reserved` variant.
48/// - `impl_spec_display!(Ty, Var1, Var2, …)` — each named variant is a
49/// single-field tuple binding a byte; `Display` renders it as
50/// `"{name}(0x{:02X})"` so the value is preserved (e.g. `Reserved(0x1A)` →
51/// `reserved(0x1A)`, `UserDefined(0x1A)` → `user defined(0x1A)`). All other
52/// variants delegate to `name()`.
53///
54/// ```
55/// pub enum Mode { Normal, HighEfficiency, Reserved(u8) }
56/// impl Mode {
57/// pub fn name(&self) -> &'static str {
58/// match self {
59/// Self::Normal => "normal",
60/// Self::HighEfficiency => "high efficiency",
61/// Self::Reserved(_) => "reserved",
62/// }
63/// }
64/// }
65/// dvb_common::impl_spec_display!(Mode, Reserved);
66/// assert_eq!(Mode::Normal.to_string(), "normal");
67/// assert_eq!(Mode::Reserved(0x1A).to_string(), "reserved(0x1A)");
68/// ```
69#[macro_export]
70macro_rules! impl_spec_display {
71 ($ty:ty) => {
72 impl ::core::fmt::Display for $ty {
73 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
74 f.write_str(self.name())
75 }
76 }
77 };
78 ($ty:ty, $($resv:ident),+ $(,)?) => {
79 impl ::core::fmt::Display for $ty {
80 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
81 match self {
82 $( Self::$resv(v) => ::core::write!(f, "{}(0x{:02X})", self.name(), v), )+
83 other => f.write_str(other.name()),
84 }
85 }
86 }
87 };
88}