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 pub other_doc: Option<String>,
30}
31
32impl EnumDef {
33 #[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 #[must_use]
50 pub fn with_other_doc(self, doc: &str) -> Self {
51 Self { other_doc: Some(String::from(doc)), ..self }
52 }
53
54 #[must_use]
58 pub fn with_default(self, variant: &str) -> Self {
59 Self { default: Some(String::from(variant)), ..self }
60 }
61
62 #[must_use]
64 pub fn with_derives(self, derives: Vec<Derive>) -> Self {
65 Self { derives, ..self }
66 }
67
68 #[must_use]
70 pub fn with_other(self, other: &str) -> Self {
71 Self { other: Some(String::from(other)), ..self }
72 }
73
74 #[must_use]
76 pub fn with_doc(self, doc: &str) -> Self {
77 Self { doc: Some(String::from(doc)), ..self }
78 }
79}
80
81#[derive(Debug, Clone, PartialEq, Eq)]
83#[non_exhaustive]
84pub struct Variant {
85 pub value: i64,
87 pub name: String,
89 pub doc: Option<String>,
91}
92
93impl Variant {
94 #[must_use]
96 pub fn new(value: i64, name: &str) -> Self {
97 Self { value, name: String::from(name), doc: None }
98 }
99
100 #[must_use]
102 pub fn with_doc(self, doc: &str) -> Self {
103 Self { doc: Some(String::from(doc)), ..self }
104 }
105}