1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use std::fmt;
use crate::formatter::Formatter;
use crate::type_def::TypeDef;
use crate::variant::Variant;
use crate::r#type::Type;
#[derive(Debug, Clone)]
pub struct Enum {
type_def: TypeDef,
variants: Vec<Variant>,
}
impl Enum {
pub fn new(name: impl Into<String>) -> Self {
Enum {
type_def: TypeDef::new(name),
variants: vec![],
}
}
pub fn ty(&self) -> &Type {
&self.type_def.ty
}
pub fn vis(&mut self, vis: &str) -> &mut Self {
self.type_def.vis(vis);
self
}
pub fn generic(&mut self, name: &str) -> &mut Self {
self.type_def.ty.generic(name);
self
}
pub fn bound<T>(&mut self, name: &str, ty: T) -> &mut Self
where
T: Into<Type>,
{
self.type_def.bound(name, ty);
self
}
pub fn doc(&mut self, docs: &str) -> &mut Self {
self.type_def.doc(docs);
self
}
pub fn derive(&mut self, name: &str) -> &mut Self {
self.type_def.derive(name);
self
}
pub fn allow(&mut self, allow: &str) -> &mut Self {
self.type_def.allow(allow);
self
}
pub fn repr(&mut self, repr: &str) -> &mut Self {
self.type_def.repr(repr);
self
}
pub fn r#macro(&mut self, r#macro: &str) -> &mut Self {
self.type_def.r#macro(r#macro);
self
}
pub fn new_variant(&mut self, name: impl Into<String>) -> &mut Variant {
self.push_variant(Variant::new(name.into()));
self.variants.last_mut().unwrap()
}
pub fn push_variant(&mut self, item: Variant) -> &mut Self {
self.variants.push(item);
self
}
pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
self.type_def.fmt_head("enum", &[], fmt)?;
fmt.block(|fmt| {
for variant in &self.variants {
variant.fmt(fmt)?;
}
Ok(())
})
}
}