facet_core/types/ty/
enum_.rs1use super::{Repr, StructType};
2
3#[derive(Clone, Copy, Debug)]
5#[repr(C)]
6pub struct EnumType {
7 pub repr: Repr,
9
10 pub enum_repr: EnumRepr,
12
13 pub variants: &'static [Variant],
15}
16
17impl EnumType {
18 pub const fn builder() -> EnumDefBuilder {
20 EnumDefBuilder::new()
21 }
22}
23
24pub struct EnumDefBuilder {
26 repr: Option<Repr>,
27 enum_repr: Option<EnumRepr>,
28 variants: Option<&'static [Variant]>,
29}
30
31impl EnumDefBuilder {
32 #[allow(clippy::new_without_default)]
34 pub const fn new() -> Self {
35 Self {
36 repr: None,
37 enum_repr: None,
38 variants: None,
39 }
40 }
41
42 pub const fn repr(mut self, repr: Repr) -> Self {
44 self.repr = Some(repr);
45 self
46 }
47
48 pub const fn enum_repr(mut self, enum_repr: EnumRepr) -> Self {
50 self.enum_repr = Some(enum_repr);
51 self
52 }
53
54 pub const fn variants(mut self, variants: &'static [Variant]) -> Self {
56 self.variants = Some(variants);
57 self
58 }
59
60 pub const fn build(self) -> EnumType {
62 EnumType {
63 repr: self.repr.unwrap(),
64 enum_repr: self.enum_repr.unwrap(),
65 variants: self.variants.unwrap(),
66 }
67 }
68}
69
70#[derive(Clone, Copy, Debug)]
72#[repr(C)]
73pub struct Variant {
74 pub name: &'static str,
76
77 pub discriminant: Option<i64>,
79
80 pub attributes: &'static [VariantAttribute],
82
83 pub data: StructType,
87
88 pub doc: &'static [&'static str],
90}
91
92impl Variant {
93 pub const fn builder() -> VariantBuilder {
95 VariantBuilder::new()
96 }
97
98 #[inline]
101 pub fn has_arbitrary_attr(&self, content: &'static str) -> bool {
102 self.attributes
103 .contains(&VariantAttribute::Arbitrary(content))
104 }
105}
106
107pub struct VariantBuilder {
109 name: Option<&'static str>,
110 discriminant: Option<i64>,
111 attributes: &'static [VariantAttribute],
112 data: Option<StructType>,
113 doc: &'static [&'static str],
114}
115
116impl VariantBuilder {
117 #[allow(clippy::new_without_default)]
119 pub const fn new() -> Self {
120 Self {
121 name: None,
122 discriminant: None,
123 attributes: &[],
124 data: None,
125 doc: &[],
126 }
127 }
128
129 pub const fn name(mut self, name: &'static str) -> Self {
131 self.name = Some(name);
132 self
133 }
134
135 pub const fn discriminant(mut self, discriminant: i64) -> Self {
137 self.discriminant = Some(discriminant);
138 self
139 }
140
141 pub const fn attributes(mut self, attributes: &'static [VariantAttribute]) -> Self {
143 self.attributes = attributes;
144 self
145 }
146
147 pub const fn data(mut self, data: StructType) -> Self {
149 self.data = Some(data);
150 self
151 }
152
153 pub const fn doc(mut self, doc: &'static [&'static str]) -> Self {
155 self.doc = doc;
156 self
157 }
158
159 pub const fn build(self) -> Variant {
161 Variant {
162 name: self.name.unwrap(),
163 discriminant: self.discriminant,
164 attributes: self.attributes,
165 data: self.data.unwrap(),
166 doc: self.doc,
167 }
168 }
169}
170
171#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
173#[repr(C)]
174pub enum VariantAttribute {
175 Arbitrary(&'static str),
177}
178
179#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
181#[repr(C)]
182pub enum EnumRepr {
183 RustNPO,
187 U8,
189 U16,
191 U32,
193 U64,
195 USize,
197 I8,
199 I16,
201 I32,
203 I64,
205 ISize,
207}
208
209impl EnumRepr {
210 pub const fn from_discriminant_size<T>() -> Self {
218 match core::mem::size_of::<T>() {
219 1 => EnumRepr::U8,
220 2 => EnumRepr::U16,
221 4 => EnumRepr::U32,
222 8 => EnumRepr::U64,
223 _ => panic!("Invalid enum size"),
224 }
225 }
226}