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
use std::iter::IntoIterator;
use syntax::ast;
use syntax::attr;
use syntax::codemap::{DUMMY_SP, Span, respan};
use syntax::parse::token;
use syntax::ptr::P;
use invoke::{Invoke, Identity};
use lit::LitBuilder;
use str::ToInternedString;
pub struct AttrBuilder<F=Identity> {
callback: F,
span: Span,
style: ast::AttrStyle,
is_sugared_doc: bool,
}
impl AttrBuilder {
pub fn new() -> Self {
AttrBuilder::new_with_callback(Identity)
}
}
impl<F> AttrBuilder<F>
where F: Invoke<ast::Attribute>,
{
pub fn new_with_callback(callback: F) -> Self {
AttrBuilder {
callback: callback,
span: DUMMY_SP,
style: ast::AttrOuter,
is_sugared_doc: false,
}
}
pub fn span(mut self, span: Span) -> Self {
self.span = span;
self
}
pub fn inner(mut self) -> Self {
self.style = ast::AttrInner;
self
}
pub fn build_meta_item(self, item: P<ast::MetaItem>) -> F::Result {
let attr = respan(self.span, ast::Attribute_ {
id: attr::mk_attr_id(),
style: self.style,
value: item,
is_sugared_doc: self.is_sugared_doc,
});
self.callback.invoke(attr)
}
pub fn build_meta_item_(self, item: ast::MetaItem_) -> F::Result {
let item = P(respan(self.span, item));
self.build_meta_item(item)
}
pub fn word<T>(self, word: T) -> F::Result
where T: ToInternedString
{
self.build_meta_item_(ast::MetaWord(word.to_interned_string()))
}
pub fn list<T>(self, word: T) -> AttrListBuilder<Self>
where T: ToInternedString
{
AttrListBuilder::new_with_callback(word, self)
}
pub fn name_value<T>(self, name: T) -> LitBuilder<AttrNameValueBuilder<Self>>
where T: ToInternedString,
{
LitBuilder::new_with_callback(AttrNameValueBuilder {
callback: self,
name: name.to_interned_string(),
})
}
pub fn automatically_derived(self) -> F::Result {
self.word("automatically_derived")
}
pub fn inline(self) -> F::Result {
self.word("inline")
}
pub fn test(self) -> F::Result {
self.word("test")
}
pub fn allow<I, T>(self, iter: I) -> F::Result
where I: IntoIterator<Item=T>,
T: ToInternedString,
{
self.list("allow").words(iter).build()
}
pub fn warn<I, T>(self, iter: I) -> F::Result
where I: IntoIterator<Item=T>,
T: ToInternedString,
{
self.list("warn").words(iter).build()
}
pub fn deny<I, T>(self, iter: I) -> F::Result
where I: IntoIterator<Item=T>,
T: ToInternedString,
{
self.list("deny").words(iter).build()
}
pub fn features<I, T>(self, iter: I) -> F::Result
where I: IntoIterator<Item=T>,
T: ToInternedString,
{
self.list("feature").words(iter).build()
}
pub fn plugins<I, T>(self, iter: I) -> F::Result
where I: IntoIterator<Item=T>,
T: ToInternedString,
{
self.list("plugin").words(iter).build()
}
pub fn doc<T>(mut self, doc: T) -> F::Result
where T: ToInternedString,
{
self.is_sugared_doc = true;
self.name_value("doc").str(doc)
}
}
impl<F> Invoke<P<ast::MetaItem>> for AttrBuilder<F>
where F: Invoke<ast::Attribute>,
{
type Result = F::Result;
fn invoke(self, item: P<ast::MetaItem>) -> F::Result {
self.build_meta_item(item)
}
}
impl<F> Invoke<ast::MetaItem_> for AttrBuilder<F>
where F: Invoke<ast::Attribute>,
{
type Result = F::Result;
fn invoke(self, item: ast::MetaItem_) -> F::Result {
self.build_meta_item_(item)
}
}
pub struct AttrListBuilder<F> {
callback: F,
span: Span,
name: token::InternedString,
items: Vec<P<ast::MetaItem>>,
}
impl<F> AttrListBuilder<F>
where F: Invoke<P<ast::MetaItem>>,
{
pub fn new_with_callback<T>(name: T, callback: F) -> Self
where T: ToInternedString,
{
AttrListBuilder {
callback: callback,
span: DUMMY_SP,
name: name.to_interned_string(),
items: vec![],
}
}
pub fn span(mut self, span: Span) -> Self {
self.span = span;
self
}
pub fn with_meta_items<I>(mut self, iter: I) -> Self
where I: IntoIterator<Item=P<ast::MetaItem>>,
{
self.items.extend(iter);
self
}
pub fn with_meta_items_<I>(self, iter: I) -> Self
where I: IntoIterator<Item=ast::MetaItem_>,
{
let iter = iter.into_iter();
let span = self.span;
self.with_meta_items(iter.map(|item| P(respan(span, item))))
}
pub fn with_meta_item(mut self, item: P<ast::MetaItem>) -> Self {
self.items.push(item);
self
}
pub fn with_meta_item_(self, item: ast::MetaItem_) -> Self {
let span = self.span;
self.with_meta_item(P(respan(span, item)))
}
pub fn words<I, T>(self, iter: I) -> Self
where I: IntoIterator<Item=T>,
T: ToInternedString,
{
let iter = iter.into_iter();
self.with_meta_items_(iter.map(|word| ast::MetaWord(word.to_interned_string())))
}
pub fn word<T>(self, word: T) -> Self
where T: ToInternedString,
{
self.with_meta_item_(ast::MetaWord(word.to_interned_string()))
}
pub fn list<T>(self, name: T) -> AttrListBuilder<Self>
where T: ToInternedString,
{
AttrListBuilder::new_with_callback(name, self)
}
pub fn name_value<T>(self, name: T) -> LitBuilder<AttrNameValueBuilder<Self>>
where T: ToInternedString,
{
LitBuilder::new_with_callback(AttrNameValueBuilder {
callback: self,
name: name.to_interned_string(),
})
}
pub fn build(self) -> F::Result {
let item = respan(self.span, ast::MetaList(self.name, self.items));
self.callback.invoke(P(item))
}
}
impl<F> Invoke<P<ast::MetaItem>> for AttrListBuilder<F>
where F: Invoke<P<ast::MetaItem>>,
{
type Result = Self;
fn invoke(self, item: P<ast::MetaItem>) -> Self {
self.with_meta_item(item)
}
}
impl<F> Invoke<ast::MetaItem_> for AttrListBuilder<F>
where F: Invoke<P<ast::MetaItem>>,
{
type Result = Self;
fn invoke(self, item: ast::MetaItem_) -> Self {
self.with_meta_item_(item)
}
}
pub struct AttrNameValueBuilder<F> {
callback: F,
name: token::InternedString,
}
impl<F: Invoke<ast::MetaItem_>> Invoke<P<ast::Lit>> for AttrNameValueBuilder<F> {
type Result = F::Result;
fn invoke(self, value: P<ast::Lit>) -> F::Result {
let item = ast::MetaNameValue(self.name, (*value).clone());
self.callback.invoke(item)
}
}