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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use syntax::ast;
use syntax::codemap::{DUMMY_SP, Span, respan};
use syntax::ptr::P;
use attr::AttrBuilder;
use ident::ToIdent;
use invoke::{Invoke, Identity};
use ty::TyBuilder;
pub struct StructDefBuilder<F=Identity> {
callback: F,
span: Span,
fields: Vec<ast::StructField>,
}
impl StructDefBuilder {
pub fn new() -> Self {
StructDefBuilder::new_with_callback(Identity)
}
}
impl<F> StructDefBuilder<F>
where F: Invoke<P<ast::StructDef>>
{
pub fn new_with_callback(callback: F) -> Self {
StructDefBuilder {
callback: callback,
span: DUMMY_SP,
fields: vec![],
}
}
pub fn span(mut self, span: Span) -> Self {
self.span = span;
self
}
pub fn with_fields<I>(mut self, iter: I) -> Self
where I: IntoIterator<Item=ast::StructField>,
{
self.fields.extend(iter);
self
}
pub fn with_field(mut self, field: ast::StructField) -> Self {
self.fields.push(field);
self
}
pub fn field<T>(self, id: T) -> StructFieldBuilder<Self>
where T: ToIdent,
{
let span = self.span;
StructFieldBuilder::named_with_callback(id, self).span(span)
}
pub fn build(self) -> F::Result {
self.callback.invoke(P(ast::StructDef {
fields: self.fields,
ctor_id: None,
}))
}
}
impl<F> Invoke<ast::StructField> for StructDefBuilder<F>
where F: Invoke<P<ast::StructDef>>,
{
type Result = Self;
fn invoke(self, field: ast::StructField) -> Self {
self.with_field(field)
}
}
pub struct StructFieldBuilder<F=Identity> {
callback: F,
span: Span,
kind: ast::StructFieldKind,
attrs: Vec<ast::Attribute>,
}
impl StructFieldBuilder {
pub fn named<T>(name: T) -> Self
where T: ToIdent,
{
StructFieldBuilder::named_with_callback(name, Identity)
}
pub fn unnamed() -> Self {
StructFieldBuilder::unnamed_with_callback(Identity)
}
}
impl<F> StructFieldBuilder<F>
where F: Invoke<ast::StructField>,
{
pub fn named_with_callback<T>(id: T, callback: F) -> Self
where T: ToIdent,
{
let id = id.to_ident();
StructFieldBuilder {
callback: callback,
span: DUMMY_SP,
kind: ast::StructFieldKind::NamedField(id, ast::Inherited),
attrs: vec![],
}
}
pub fn unnamed_with_callback(callback: F) -> Self {
StructFieldBuilder {
callback: callback,
span: DUMMY_SP,
kind: ast::StructFieldKind::UnnamedField(ast::Inherited),
attrs: vec![],
}
}
pub fn span(mut self, span: Span) -> Self {
self.span = span;
self
}
pub fn pub_(mut self) -> Self {
match self.kind {
ast::StructFieldKind::NamedField(_, ref mut vis) => { *vis = ast::Public; }
ast::StructFieldKind::UnnamedField(ref mut vis) => { *vis = ast::Public; }
}
self
}
pub fn attr(self) -> AttrBuilder<Self> {
let span = self.span;
AttrBuilder::new_with_callback(self).span(span)
}
pub fn build_ty(self, ty: P<ast::Ty>) -> F::Result {
let field = ast::StructField_ {
kind: self.kind,
id: ast::DUMMY_NODE_ID,
ty: ty,
attrs: self.attrs,
};
self.callback.invoke(respan(self.span, field))
}
pub fn ty(self) -> TyBuilder<Self> {
let span = self.span;
TyBuilder::new_with_callback(self).span(span)
}
}
impl<F> Invoke<ast::Attribute> for StructFieldBuilder<F> {
type Result = Self;
fn invoke(mut self, attr: ast::Attribute) -> Self {
self.attrs.push(attr);
self
}
}
impl<F> Invoke<P<ast::Ty>> for StructFieldBuilder<F>
where F: Invoke<ast::StructField>,
{
type Result = F::Result;
fn invoke(self, ty: P<ast::Ty>) -> F::Result {
self.build_ty(ty)
}
}