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
use syntax::ast;
use syntax::codemap::{DUMMY_SP, Span};
use syntax::ptr::P;
use ident::ToIdent;
use invoke::{Invoke, Identity};
use struct_field::StructFieldBuilder;
use ty::TyBuilder;
pub struct VariantDataBuilder<F=Identity> {
callback: F,
span: Span,
}
impl VariantDataBuilder {
pub fn new() -> Self {
VariantDataBuilder::with_callback(Identity)
}
}
impl<F> VariantDataBuilder<F>
where F: Invoke<ast::VariantData>
{
pub fn with_callback(callback: F) -> Self {
VariantDataBuilder {
callback: callback,
span: DUMMY_SP,
}
}
pub fn span(mut self, span: Span) -> Self {
self.span = span;
self
}
pub fn tuple(self) -> StructFieldBuilder<VariantDataTupleBuilder<F>> {
let builder = VariantDataTupleBuilder {
callback: self.callback,
span: self.span,
fields: Vec::new(),
};
builder.field()
}
pub fn struct_(self) -> VariantDataStructBuilder<F> {
VariantDataStructBuilder {
callback: self.callback,
span: self.span,
fields: Vec::new(),
}
}
pub fn unit(self) -> F::Result {
self.callback.invoke(ast::VariantData::Unit(ast::DUMMY_NODE_ID))
}
}
pub struct VariantDataTupleBuilder<F> {
callback: F,
span: Span,
fields: Vec<ast::StructField>,
}
impl<F> VariantDataTupleBuilder<F>
where F: Invoke<ast::VariantData>
{
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(self) -> StructFieldBuilder<Self> {
let span = self.span;
StructFieldBuilder::unnamed_with_callback(self).span(span)
}
pub fn with_ty(self, ty: P<ast::Ty>) -> Self {
self.field().build_ty(ty)
}
pub fn ty(self) -> TyBuilder<Self> {
let span = self.span;
TyBuilder::with_callback(self).span(span)
}
pub fn build(self) -> F::Result {
self.callback.invoke(ast::VariantData::Tuple(self.fields, ast::DUMMY_NODE_ID))
}
}
impl<F> Invoke<P<ast::Ty>> for VariantDataTupleBuilder<F>
where F: Invoke<ast::VariantData>,
{
type Result = Self;
fn invoke(self, ty: P<ast::Ty>) -> Self {
self.with_ty(ty)
}
}
impl<F> Invoke<ast::StructField> for VariantDataTupleBuilder<F>
where F: Invoke<ast::VariantData>,
{
type Result = Self;
fn invoke(self, field: ast::StructField) -> Self {
self.with_field(field)
}
}
pub struct VariantDataStructBuilder<F> {
callback: F,
span: Span,
fields: Vec<ast::StructField>,
}
impl<F> VariantDataStructBuilder<F>
where F: Invoke<ast::VariantData>,
{
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(ast::VariantData::Struct(self.fields, ast::DUMMY_NODE_ID))
}
}
impl<F> Invoke<ast::StructField> for VariantDataStructBuilder<F>
where F: Invoke<ast::VariantData>,
{
type Result = Self;
fn invoke(self, field: ast::StructField) -> Self {
self.with_field(field)
}
}