Skip to main content

layline_codegen/model/
choice.rs

1//! Choices: the arms a switch selects between.
2
3use alloc::string::String;
4use alloc::vec::Vec;
5
6use super::Derive;
7
8use super::{Endian, Segment};
9
10/// What a switch selects on.
11#[derive(Debug, Clone, PartialEq, Eq)]
12#[non_exhaustive]
13pub enum Discriminant {
14    /// The value of an earlier field.
15    ///
16    /// `outer.inner` names a field of a nested layout.
17    Field(String),
18    /// The number of bytes left in the body.
19    BodyLength,
20}
21
22/// One arm of a switch.
23///
24/// An arm's fields can refer only to each other.
25#[derive(Debug, Clone, PartialEq, Eq)]
26#[non_exhaustive]
27pub struct Arm {
28    /// The discriminant value that selects this arm.
29    pub value: u64,
30    /// The arm's segments, in wire order.
31    pub body: Vec<Segment>,
32    /// The variant name.
33    pub name: String,
34    /// Documentation.
35    pub doc: Option<String>,
36}
37
38impl Arm {
39    /// An undocumented arm.
40    #[must_use]
41    pub fn new(value: u64, name: &str, body: Vec<Segment>) -> Self {
42        Self { value, body, name: String::from(name), doc: None }
43    }
44
45    /// Sets the documentation.
46    #[must_use]
47    pub fn with_doc(self, doc: &str) -> Self {
48        Self { doc: Some(String::from(doc)), ..self }
49    }
50
51    /// Whether the arm reads to the end of its input.
52    #[must_use]
53    pub fn open_ended(&self) -> bool {
54        self.body.iter().any(Segment::open_ended)
55    }
56}
57
58/// The arms of a [`Segment::Switch`].
59///
60/// With a fallback [`other`](Self::other) variant, an unmatched value keeps its bytes: the rest of
61/// the body, or [`other_bytes`](Self::other_bytes) of them.
62#[derive(Debug, Clone, PartialEq, Eq)]
63#[non_exhaustive]
64pub struct ChoiceDef {
65    /// The type name.
66    pub name: String,
67    /// Byte order of the arms' fields.
68    pub endian: Endian,
69    /// The arms, in declaration order.
70    pub arms: Vec<Arm>,
71    /// The fallback variant for an unmatched value. `None` makes it a decode error.
72    pub other: Option<String>,
73    /// The fallback's size in bytes, when the choice is a union.
74    ///
75    /// `None` takes the rest of the body, so no field can follow the switch.
76    pub other_bytes: Option<usize>,
77    /// Documentation.
78    pub doc: Option<String>,
79    /// Derives for this choice, written after the module's.
80    pub derives: Vec<Derive>,
81}
82
83impl ChoiceDef {
84    /// A little-endian choice with no fallback.
85    #[must_use]
86    pub fn new(name: &str, arms: Vec<Arm>) -> Self {
87        Self {
88            name: String::from(name),
89            endian: Endian::Le,
90            arms,
91            other: None,
92            other_bytes: None,
93            doc: None,
94            derives: Vec::new(),
95        }
96    }
97
98    /// Sets the derives written on this choice, after the module's.
99    #[must_use]
100    pub fn with_derives(self, derives: Vec<Derive>) -> Self {
101        Self { derives, ..self }
102    }
103
104    /// Sets the byte order.
105    #[must_use]
106    pub fn with_endian(self, endian: Endian) -> Self {
107        Self { endian, ..self }
108    }
109
110    /// Adds a fallback variant that keeps an unmatched value's bytes.
111    #[must_use]
112    pub fn with_other(self, other: &str) -> Self {
113        Self { other: Some(String::from(other)), ..self }
114    }
115
116    /// Makes the choice a union of `bytes` bytes.
117    ///
118    /// Every arm must fill exactly that size, and a field can follow the switch.
119    #[must_use]
120    pub fn with_other_bytes(self, bytes: usize) -> Self {
121        Self { other_bytes: Some(bytes), ..self }
122    }
123
124    /// Sets the documentation.
125    #[must_use]
126    pub fn with_doc(self, doc: &str) -> Self {
127        Self { doc: Some(String::from(doc)), ..self }
128    }
129
130    /// Whether the choice reads to the end of the body.
131    ///
132    /// True when any arm does, or when the fallback has no fixed size.
133    #[must_use]
134    pub fn open_ended(&self) -> bool {
135        (self.other.is_some() && self.other_bytes.is_none())
136            || self.arms.iter().any(Arm::open_ended)
137    }
138}