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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use syntax::ast;
use syntax::codemap::{DUMMY_SP, Span};
use syntax::ptr::P;
use ident::ToIdent;
use invoke::{Invoke, Identity};
use pat::PatBuilder;
use ty::TyBuilder;
pub struct FnDeclBuilder<F=Identity> {
callback: F,
span: Span,
args: Vec<ast::Arg>,
variadic: bool,
}
impl FnDeclBuilder {
pub fn new() -> FnDeclBuilder {
FnDeclBuilder::with_callback(Identity)
}
}
impl<F> FnDeclBuilder<F>
where F: Invoke<P<ast::FnDecl>>,
{
pub fn with_callback(callback: F) -> Self {
FnDeclBuilder {
callback: callback,
span: DUMMY_SP,
args: Vec::new(),
variadic: false,
}
}
pub fn span(mut self, span: Span) -> Self {
self.span = span;
self
}
pub fn variadic(mut self) -> Self {
self.variadic = true;
self
}
pub fn with_arg(mut self, arg: ast::Arg) -> Self {
self.args.push(arg);
self
}
pub fn with_args<I>(mut self, iter: I) -> Self
where I: IntoIterator<Item=ast::Arg>
{
self.args.extend(iter);
self
}
pub fn arg(self) -> ArgBuilder<Self> {
ArgBuilder::with_callback(self)
}
pub fn arg_id<T>(self, id: T) -> ArgPatBuilder<Self>
where T: ToIdent,
{
self.arg().pat().id(id)
}
pub fn arg_ref_id<T>(self, id: T) -> ArgPatBuilder<Self>
where T: ToIdent,
{
self.arg().ref_id(id)
}
pub fn arg_mut_id<T>(self, id: T) -> ArgPatBuilder<Self>
where T: ToIdent,
{
self.arg().mut_id(id)
}
pub fn arg_ref_mut_id<T>(self, id: T) -> ArgPatBuilder<Self>
where T: ToIdent,
{
self.arg().ref_mut_id(id)
}
pub fn no_return(self) -> F::Result {
let ret_ty = ast::FunctionRetTy::None(self.span);
self.build(ret_ty)
}
pub fn default_return(self) -> F::Result {
let ret_ty = ast::FunctionRetTy::Default(self.span);
self.build(ret_ty)
}
pub fn build_return(self, ty: P<ast::Ty>) -> F::Result {
self.build(ast::FunctionRetTy::Ty(ty))
}
pub fn return_(self) -> TyBuilder<Self> {
TyBuilder::with_callback(self)
}
pub fn build(self, output: ast::FunctionRetTy) -> F::Result {
self.callback.invoke(P(ast::FnDecl {
inputs: self.args,
output: output,
variadic: self.variadic,
}))
}
}
impl<F> Invoke<ast::Arg> for FnDeclBuilder<F>
where F: Invoke<P<ast::FnDecl>>
{
type Result = Self;
fn invoke(self, arg: ast::Arg) -> Self {
self.with_arg(arg)
}
}
impl<F> Invoke<P<ast::Ty>> for FnDeclBuilder<F>
where F: Invoke<P<ast::FnDecl>>,
{
type Result = F::Result;
fn invoke(self, ty: P<ast::Ty>) -> F::Result {
self.build_return(ty)
}
}
pub struct ArgBuilder<F=Identity> {
callback: F,
span: Span,
}
impl ArgBuilder {
pub fn new() -> Self {
ArgBuilder::with_callback( Identity)
}
}
impl<F> ArgBuilder<F>
where F: Invoke<ast::Arg>,
{
pub fn with_callback(callback: F) -> ArgBuilder<F> {
ArgBuilder {
callback: callback,
span: DUMMY_SP,
}
}
pub fn span(mut self, span: Span) -> Self {
self.span = span;
self
}
pub fn with_pat(self, pat: P<ast::Pat>) -> ArgPatBuilder<F> {
ArgPatBuilder {
callback: self.callback,
span: self.span,
pat: pat,
}
}
pub fn pat(self) -> PatBuilder<Self> {
PatBuilder::with_callback(self)
}
pub fn id<T>(self, id: T) -> ArgPatBuilder<F>
where T: ToIdent,
{
self.pat().id(id)
}
pub fn ref_id<T>(self, id: T) -> ArgPatBuilder<F>
where T: ToIdent,
{
self.pat().ref_id(id)
}
pub fn mut_id<T>(self, id: T) -> ArgPatBuilder<F>
where T: ToIdent,
{
self.pat().mut_id(id)
}
pub fn ref_mut_id<T>(self, id: T) -> ArgPatBuilder<F>
where T: ToIdent,
{
self.pat().ref_mut_id(id)
}
}
impl<F> Invoke<P<ast::Pat>> for ArgBuilder<F>
where F: Invoke<ast::Arg>
{
type Result = ArgPatBuilder<F>;
fn invoke(self, pat: P<ast::Pat>) -> Self::Result {
self.with_pat(pat)
}
}
pub struct ArgPatBuilder<F> {
callback: F,
span: Span,
pat: P<ast::Pat>,
}
impl<F> ArgPatBuilder<F>
where F: Invoke<ast::Arg>
{
pub fn with_ty(self, ty: P<ast::Ty>) -> F::Result {
self.callback.invoke(ast::Arg {
id: ast::DUMMY_NODE_ID,
ty: ty,
pat: self.pat,
})
}
pub fn ty(self) -> TyBuilder<Self> {
let span = self.span;
TyBuilder::with_callback(self).span(span)
}
}
impl<F> Invoke<P<ast::Ty>> for ArgPatBuilder<F>
where F: Invoke<ast::Arg>
{
type Result = F::Result;
fn invoke(self, ty: P<ast::Ty>) -> F::Result {
self.with_ty(ty)
}
}