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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
use std::iter::IntoIterator;
use syntax::ast;
use syntax::codemap::{DUMMY_SP, Span};
use syntax::ptr::P;
use ident::ToIdent;
use invoke::{Invoke, Identity};
use name::ToName;
use path::PathBuilder;
use qpath::QPathBuilder;
pub struct TyBuilder<F=Identity> {
callback: F,
span: Span,
}
impl TyBuilder {
pub fn new() -> Self {
TyBuilder::new_with_callback(Identity)
}
}
impl<F> TyBuilder<F>
where F: Invoke<P<ast::Ty>>,
{
pub fn new_with_callback(callback: F) -> Self {
TyBuilder {
callback: callback,
span: DUMMY_SP,
}
}
pub fn build(self, ty: P<ast::Ty>) -> F::Result {
self.callback.invoke(ty)
}
pub fn span(mut self, span: Span) -> Self {
self.span = span;
self
}
pub fn build_ty_(self, ty_: ast::Ty_) -> F::Result {
let span = self.span;
self.build(P(ast::Ty {
id: ast::DUMMY_NODE_ID,
node: ty_,
span: span,
}))
}
pub fn id<I>(self, id: I) -> F::Result
where I: ToIdent,
{
self.path().id(id).build()
}
pub fn build_path(self, path: ast::Path) -> F::Result {
self.build_ty_(ast::Ty_::TyPath(None, path))
}
pub fn build_qpath(self, qself: ast::QSelf, path: ast::Path) -> F::Result {
self.build_ty_(ast::Ty_::TyPath(Some(qself), path))
}
pub fn path(self) -> PathBuilder<TyPathBuilder<F>> {
PathBuilder::new_with_callback(TyPathBuilder(self))
}
pub fn qpath(self) -> QPathBuilder<TyQPathBuilder<F>> {
QPathBuilder::new_with_callback(TyQPathBuilder(self))
}
pub fn isize(self) -> F::Result {
self.id("isize")
}
pub fn i8(self) -> F::Result {
self.id("i8")
}
pub fn i16(self) -> F::Result {
self.id("i16")
}
pub fn i32(self) -> F::Result {
self.id("i32")
}
pub fn i64(self) -> F::Result {
self.id("i64")
}
pub fn usize(self) -> F::Result {
self.id("usize")
}
pub fn u8(self) -> F::Result {
self.id("u8")
}
pub fn u16(self) -> F::Result {
self.id("u16")
}
pub fn u32(self) -> F::Result {
self.id("u32")
}
pub fn u64(self) -> F::Result {
self.id("u64")
}
pub fn unit(self) -> F::Result {
self.tuple().build()
}
pub fn tuple(self) -> TyTupleBuilder<F> {
TyTupleBuilder {
builder: self,
tys: vec![],
}
}
pub fn build_slice(self, ty: P<ast::Ty>) -> F::Result {
self.build_ty_(ast::Ty_::TyVec(ty))
}
pub fn slice(self) -> TyBuilder<TySliceBuilder<F>> {
TyBuilder::new_with_callback(TySliceBuilder(self))
}
pub fn ref_(self) -> TyRefBuilder<F> {
TyRefBuilder {
builder: self,
lifetime: None,
mutability: ast::MutImmutable,
}
}
pub fn infer(self) -> F::Result {
self.build_ty_(ast::TyInfer)
}
pub fn option(self) -> TyBuilder<TyOptionBuilder<F>> {
TyBuilder::new_with_callback(TyOptionBuilder(self))
}
pub fn result(self) -> TyBuilder<TyResultOkBuilder<F>> {
TyBuilder::new_with_callback(TyResultOkBuilder(self))
}
pub fn phantom_data(self) -> TyBuilder<TyPhantomDataBuilder<F>> {
TyBuilder::new_with_callback(TyPhantomDataBuilder(self))
}
}
pub struct TyPathBuilder<F>(TyBuilder<F>);
impl<F> Invoke<ast::Path> for TyPathBuilder<F>
where F: Invoke<P<ast::Ty>>,
{
type Result = F::Result;
fn invoke(self, path: ast::Path) -> F::Result {
self.0.build_path(path)
}
}
pub struct TyQPathBuilder<F>(TyBuilder<F>);
impl<F> Invoke<(ast::QSelf, ast::Path)> for TyQPathBuilder<F>
where F: Invoke<P<ast::Ty>>,
{
type Result = F::Result;
fn invoke(self, (qself, path): (ast::QSelf, ast::Path)) -> F::Result {
self.0.build_qpath(qself, path)
}
}
pub struct TySliceBuilder<F>(TyBuilder<F>);
impl<F> Invoke<P<ast::Ty>> for TySliceBuilder<F>
where F: Invoke<P<ast::Ty>>,
{
type Result = F::Result;
fn invoke(self, ty: P<ast::Ty>) -> F::Result {
self.0.build_slice(ty)
}
}
pub struct TyRefBuilder<F> {
builder: TyBuilder<F>,
lifetime: Option<ast::Lifetime>,
mutability: ast::Mutability,
}
impl<F> TyRefBuilder<F>
where F: Invoke<P<ast::Ty>>,
{
pub fn mut_(mut self) -> Self {
self.mutability = ast::MutMutable;
self
}
pub fn lifetime<N>(mut self, name: N) -> Self
where N: ToName,
{
self.lifetime = Some(ast::Lifetime {
id: ast::DUMMY_NODE_ID,
span: self.builder.span,
name: name.to_name(),
});
self
}
pub fn build_ty(self, ty: P<ast::Ty>) -> F::Result {
let ty = ast::MutTy {
ty: ty,
mutbl: self.mutability,
};
self.builder.build_ty_(ast::TyRptr(self.lifetime, ty))
}
pub fn ty(self) -> TyBuilder<Self> {
TyBuilder::new_with_callback(self)
}
}
impl<F> Invoke<P<ast::Ty>> for TyRefBuilder<F>
where F: Invoke<P<ast::Ty>>,
{
type Result = F::Result;
fn invoke(self, ty: P<ast::Ty>) -> F::Result {
self.build_ty(ty)
}
}
pub struct TyOptionBuilder<F>(TyBuilder<F>);
impl<F> Invoke<P<ast::Ty>> for TyOptionBuilder<F>
where F: Invoke<P<ast::Ty>>,
{
type Result = F::Result;
fn invoke(self, ty: P<ast::Ty>) -> F::Result {
let path = PathBuilder::new()
.global()
.id("std")
.id("option")
.segment("Option")
.with_ty(ty)
.build()
.build();
self.0.build_path(path)
}
}
pub struct TyResultOkBuilder<F>(TyBuilder<F>);
impl<F> Invoke<P<ast::Ty>> for TyResultOkBuilder<F>
where F: Invoke<P<ast::Ty>>,
{
type Result = TyBuilder<TyResultErrBuilder<F>>;
fn invoke(self, ty: P<ast::Ty>) -> TyBuilder<TyResultErrBuilder<F>> {
TyBuilder::new_with_callback(TyResultErrBuilder(self.0, ty))
}
}
pub struct TyResultErrBuilder<F>(TyBuilder<F>, P<ast::Ty>);
impl<F> Invoke<P<ast::Ty>> for TyResultErrBuilder<F>
where F: Invoke<P<ast::Ty>>,
{
type Result = F::Result;
fn invoke(self, ty: P<ast::Ty>) -> F::Result {
let path = PathBuilder::new()
.global()
.id("std")
.id("result")
.segment("Result")
.with_ty(self.1)
.with_ty(ty)
.build()
.build();
self.0.build_path(path)
}
}
pub struct TyPhantomDataBuilder<F>(TyBuilder<F>);
impl<F> Invoke<P<ast::Ty>> for TyPhantomDataBuilder<F>
where F: Invoke<P<ast::Ty>>,
{
type Result = F::Result;
fn invoke(self, ty: P<ast::Ty>) -> F::Result {
let path = PathBuilder::new()
.global()
.id("std")
.id("marker")
.segment("PhantomData")
.with_ty(ty)
.build()
.build();
self.0.build_path(path)
}
}
pub struct TyTupleBuilder<F> {
builder: TyBuilder<F>,
tys: Vec<P<ast::Ty>>,
}
impl<F> TyTupleBuilder<F>
where F: Invoke<P<ast::Ty>>,
{
pub fn with_tys<I>(mut self, iter: I) -> Self
where I: IntoIterator<Item=P<ast::Ty>>,
{
self.tys.extend(iter);
self
}
pub fn with_ty(mut self, ty: P<ast::Ty>) -> Self {
self.tys.push(ty);
self
}
pub fn ty(self) -> TyBuilder<Self> {
TyBuilder::new_with_callback(self)
}
pub fn build(self) -> F::Result {
self.builder.build_ty_(ast::TyTup(self.tys))
}
}
impl<F> Invoke<P<ast::Ty>> for TyTupleBuilder<F>
where F: Invoke<P<ast::Ty>>,
{
type Result = Self;
fn invoke(self, ty: P<ast::Ty>) -> Self {
self.with_ty(ty)
}
}