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
use std::fmt::{self, Write};
use crate::fields::Fields;
use crate::formatter::Formatter;
use crate::r#type::Type;
#[derive(Debug, Clone)]
pub struct Variant {
name: String,
fields: Fields,
annotations: Vec<String>,
}
impl Variant {
pub fn new(name: impl Into<String>) -> Self {
Variant {
name: name.into(),
fields: Fields::Empty,
annotations: Vec::new(),
}
}
pub fn named<T>(&mut self, name: &str, ty: T) -> &mut Self
where
T: Into<Type>,
{
self.fields.named(name, ty);
self
}
pub fn tuple(&mut self, ty: &str) -> &mut Self {
self.fields.tuple(ty);
self
}
pub fn annotation(&mut self, annotation: impl Into<String>) -> &mut Self {
self.annotations.push(annotation.into());
self
}
pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
for a in &self.annotations {
write!(fmt, "{}", a)?;
write!(fmt, "\n")?;
}
write!(fmt, "{}", self.name)?;
self.fields.fmt(fmt)?;
write!(fmt, ",\n")?;
Ok(())
}
}