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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use std::fmt::{self, Write};
use crate::field::Field;
use crate::formatter::Formatter;
use crate::r#type::Type;
/// Defines a set of fields.
#[derive(Debug, Clone)]
pub enum Fields {
Empty,
/// Tuple types.
Tuple(Vec<Type>),
/// Named fields.
Named(Vec<Field>),
}
impl Fields {
/// Pushed a named field passed in as a `Field` type.
///
/// # Arguments
///
/// * `field` - The field to push.
pub fn push_named(&mut self, field: Field) -> &mut Self {
match *self {
Fields::Empty => {
*self = Fields::Named(vec![field]);
}
Fields::Named(ref mut fields) => {
fields.push(field);
}
_ => panic!("field list is named"),
}
self
}
/// Pushes a named field by its name and type.
///
/// # Arguments
///
/// * `name` - The name of the field.
/// * `ty` - The type of the field.
pub fn named<T>(&mut self, name: &str, ty: T) -> &mut Self
where
T: Into<Type>,
{
self.push_named(Field {
name: name.to_string(),
ty: ty.into(),
documentation: Vec::new(),
annotation: Vec::new(),
})
}
/// Pushes a type.
///
/// # Arguments
///
/// * `ty` - The type to push.
pub fn tuple<T>(&mut self, ty: T) -> &mut Self
where
T: Into<Type>,
{
match *self {
Fields::Empty => {
*self = Fields::Tuple(vec![ty.into()]);
}
Fields::Tuple(ref mut fields) => {
fields.push(ty.into());
}
_ => panic!("field list is tuple"),
}
self
}
/// Formats the fields using the provided formatter.
///
/// * `fmt` - The formatter to use.
pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
match *self {
Fields::Named(ref fields) => {
assert!(!fields.is_empty());
fmt.block(|fmt| {
for f in fields {
if !f.documentation.is_empty() {
for doc in &f.documentation {
write!(fmt, "/// {}\n", doc)?;
}
}
if !f.annotation.is_empty() {
for ann in &f.annotation {
write!(fmt, "{}\n", ann)?;
}
}
write!(fmt, "{}: ", f.name)?;
f.ty.fmt(fmt)?;
write!(fmt, ",\n")?;
}
Ok(())
})?;
}
Fields::Tuple(ref tys) => {
assert!(!tys.is_empty());
write!(fmt, "(")?;
for (i, ty) in tys.iter().enumerate() {
if i != 0 {
write!(fmt, ", ")?;
}
ty.fmt(fmt)?;
}
write!(fmt, ")")?;
}
Fields::Empty => {}
}
Ok(())
}
}