mpeg_syntax_dump/types.rs
1use std::fmt;
2
3/// How a field value should be displayed.
4#[derive(Debug, Clone, PartialEq)]
5pub enum Value {
6 /// Decimal unsigned: `= 256`
7 Unsigned(u64),
8 /// Decimal signed: `= -2`
9 Signed(i64),
10 /// Hexadecimal: `= 0x47`
11 Hex(u64),
12 /// Bit string: `= '111111'`
13 BitString(String),
14 /// Boolean displayed as 0/1: `= 1`
15 Bool(bool),
16}
17
18impl fmt::Display for Value {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 match self {
21 Value::Unsigned(v) => write!(f, "{v}"),
22 Value::Signed(v) => write!(f, "{v}"),
23 Value::Hex(v) => write!(f, "0x{v:02X}"),
24 Value::BitString(s) => write!(f, "'{s}'"),
25 Value::Bool(b) => write!(f, "{}", if *b { 1 } else { 0 }),
26 }
27 }
28}
29
30/// Term annotation: `/* name: value */` on condition/loop lines.
31pub struct TermAnnotation<'a> {
32 pub name: &'a str,
33 pub value: Value,
34}
35
36/// A fixed-width field read from the bitstream.
37pub struct FixedWidthField<'a> {
38 /// Field name, e.g. `"sync_byte"`, `"offset_for_ref_frame[ 0 ]"`
39 pub name: &'a str,
40 /// Number of bits
41 pub bits: u32,
42 /// Type descriptor, e.g. `"bslbf"`, `"u(8)"`, `"f(1)"`
43 pub descriptor: &'a str,
44 /// Decoded value, or `None` for raw-byte field templates
45 pub value: Option<Value>,
46 /// Inline comment, e.g. `"equal to 0x03"`
47 pub comment: Option<&'a str>,
48}
49
50/// A variable-length coded field.
51pub struct VariableLengthField<'a> {
52 /// Field name
53 pub name: &'a str,
54 /// Descriptor, e.g. `"ue(v)"`, `"se(v)"`, `"me(v)"`
55 pub descriptor: &'a str,
56 /// Decoded value
57 pub value: Option<Value>,
58 /// Inline comment
59 pub comment: Option<&'a str>,
60}
61
62/// A fixed bit pattern or marker bit field.
63pub struct BitPatternField<'a> {
64 /// Pattern name, e.g. `"'0010'"` or `"marker_bit"`
65 pub name: &'a str,
66 /// Number of bits
67 pub bits: u32,
68 /// Descriptor, e.g. `"bslbf"`
69 pub descriptor: &'a str,
70 /// The actual value
71 pub value: Value,
72}
73
74/// Column definition for a [`FieldTable`].
75pub struct ColumnDef<'a> {
76 /// Column name (used as field name in expanded form)
77 pub name: &'a str,
78 /// Type descriptor, e.g. `"uimsbf"`, `"ue(v)"`
79 pub descriptor: &'a str,
80 /// Bit width per value, or `None` for variable-length coded columns
81 pub bits: Option<u32>,
82}
83
84/// A table of homogeneous field values from a loop.
85///
86/// Producers emit this instead of a `begin_for`/`end_for` loop when every
87/// iteration has the same fixed set of fields. The default `SyntaxWrite`
88/// implementation expands it into a loop; compact renderers can collapse it
89/// into an inline list or aligned table.
90pub struct FieldTable<'a> {
91 pub columns: &'a [ColumnDef<'a>],
92 pub rows: &'a [&'a [Value]],
93}