layline_codegen/model/
enumeration.rs1use alloc::string::String;
4use alloc::vec::Vec;
5
6use super::Derive;
7
8use super::Scalar;
9
10#[derive(Debug, Clone, PartialEq, Eq)]
12#[non_exhaustive]
13pub struct EnumDef {
14 pub name: String,
16 pub repr: Scalar,
18 pub variants: Vec<Variant>,
20 pub other: Option<String>,
22 pub doc: Option<String>,
24 pub default: Option<String>,
26 pub derives: Vec<Derive>,
28}
29
30impl EnumDef {
31 #[must_use]
33 pub fn new(name: &str, repr: Scalar, variants: Vec<Variant>) -> Self {
34 Self {
35 name: String::from(name),
36 repr,
37 variants,
38 other: None,
39 doc: None,
40 default: None,
41 derives: Vec::new(),
42 }
43 }
44
45 #[must_use]
49 pub fn with_default(self, variant: &str) -> Self {
50 Self { default: Some(String::from(variant)), ..self }
51 }
52
53 #[must_use]
55 pub fn with_derives(self, derives: Vec<Derive>) -> Self {
56 Self { derives, ..self }
57 }
58
59 #[must_use]
61 pub fn with_other(self, other: &str) -> Self {
62 Self { other: Some(String::from(other)), ..self }
63 }
64
65 #[must_use]
67 pub fn with_doc(self, doc: &str) -> Self {
68 Self { doc: Some(String::from(doc)), ..self }
69 }
70}
71
72#[derive(Debug, Clone, PartialEq, Eq)]
74#[non_exhaustive]
75pub struct Variant {
76 pub value: i64,
78 pub name: String,
80 pub doc: Option<String>,
82}
83
84impl Variant {
85 #[must_use]
87 pub fn new(value: i64, name: &str) -> Self {
88 Self { value, name: String::from(name), doc: None }
89 }
90
91 #[must_use]
93 pub fn with_doc(self, doc: &str) -> Self {
94 Self { doc: Some(String::from(doc)), ..self }
95 }
96}