Skip to main content

layline_codegen/model/
enumeration.rs

1//! Enumerations.
2
3use alloc::string::String;
4use alloc::vec::Vec;
5
6use super::Derive;
7
8use super::Scalar;
9
10/// An enumeration of named values, with an optional fallback variant for unlisted values.
11#[derive(Debug, Clone, PartialEq, Eq)]
12#[non_exhaustive]
13pub struct EnumDef {
14    /// The type name.
15    pub name: String,
16    /// The value's type.
17    pub repr: Scalar,
18    /// The variants, in declaration order.
19    pub variants: Vec<Variant>,
20    /// The fallback variant, which holds an unlisted value. `None` means every value is listed.
21    pub other: Option<String>,
22    /// Documentation.
23    pub doc: Option<String>,
24    /// The variant marked `#[default]`, which needs an ungated `Default` derive.
25    pub default: Option<String>,
26    /// Derives for this enumeration, written after the module's.
27    pub derives: Vec<Derive>,
28    /// Documentation for the fallback variant. `None` writes a general sentence.
29    pub other_doc: Option<String>,
30}
31
32impl EnumDef {
33    /// An enumeration with no fallback, so it must list every value.
34    #[must_use]
35    pub fn new(name: &str, repr: Scalar, variants: Vec<Variant>) -> Self {
36        Self {
37            name: String::from(name),
38            repr,
39            variants,
40            other: None,
41            doc: None,
42            default: None,
43            derives: Vec::new(),
44            other_doc: None,
45        }
46    }
47
48    /// Sets the fallback variant's documentation.
49    #[must_use]
50    pub fn with_other_doc(self, doc: &str) -> Self {
51        Self { other_doc: Some(String::from(doc)), ..self }
52    }
53
54    /// Marks `variant` `#[default]`, which the derives must match with an ungated `Default`.
55    ///
56    /// The variant must be a listed one, not the fallback, which carries a value.
57    #[must_use]
58    pub fn with_default(self, variant: &str) -> Self {
59        Self { default: Some(String::from(variant)), ..self }
60    }
61
62    /// Sets the derives written on this enumeration, after the module's.
63    #[must_use]
64    pub fn with_derives(self, derives: Vec<Derive>) -> Self {
65        Self { derives, ..self }
66    }
67
68    /// Adds a fallback variant `other(repr)` that holds any unlisted value.
69    #[must_use]
70    pub fn with_other(self, other: &str) -> Self {
71        Self { other: Some(String::from(other)), ..self }
72    }
73
74    /// Sets the documentation.
75    #[must_use]
76    pub fn with_doc(self, doc: &str) -> Self {
77        Self { doc: Some(String::from(doc)), ..self }
78    }
79}
80
81/// One variant of an [`EnumDef`].
82#[derive(Debug, Clone, PartialEq, Eq)]
83#[non_exhaustive]
84pub struct Variant {
85    /// The value on the wire.
86    pub value: i64,
87    /// The variant name.
88    pub name: String,
89    /// Documentation.
90    pub doc: Option<String>,
91}
92
93impl Variant {
94    /// `name = value`, undocumented.
95    #[must_use]
96    pub fn new(value: i64, name: &str) -> Self {
97        Self { value, name: String::from(name), doc: None }
98    }
99
100    /// Sets the documentation.
101    #[must_use]
102    pub fn with_doc(self, doc: &str) -> Self {
103        Self { doc: Some(String::from(doc)), ..self }
104    }
105}