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
use proc_macro2::TokenStream as TokenStream2;
use syn::parse::ParseStream;
use syn::spanned::Spanned;
use syn::token::{And, Star};
use syn::{Error, Expr, Ident, Path, Token};
use crate::args::Args;
use crate::widgets::parse_util::{self, attr_twice_error};
use crate::widgets::{
Attr, Attrs, ParseError, Properties, PropertyType, Widget, WidgetAttr, WidgetFunc,
WidgetTemplateAttr,
};
type WidgetFuncInfo = (Option<And>, Option<Star>, WidgetFunc, Properties);
type AttributeInfo = (
WidgetAttr,
Option<TokenStream2>,
Option<Ident>,
Option<Path>,
WidgetTemplateAttr,
);
impl Widget {
pub(super) fn parse(
input: ParseStream<'_>,
attributes: Option<Attrs>,
args: Option<Args<Expr>>,
) -> Result<Self, ParseError> {
let (attr, doc_attr, new_name, assign_wrapper, template_attr) =
Self::process_attributes(attributes)?;
let mutable = input.parse().ok();
let name_opt: Option<Ident> = if input.peek2(Token![=]) {
if attr.is_local_attr() || template_attr == WidgetTemplateAttr::TemplateChild {
return Err(input.error("When using the `local`, `local_ref` or `template_child` attributes you cannot rename the existing local variable.").into());
}
let name = input.parse()?;
let _token: Token![=] = input.parse()?;
Some(name)
} else {
None
};
let (ref_token, deref_token, func, properties) = Self::parse_widget_func(input)?;
let mut name_set = name_opt.is_some();
if new_name.is_some() {
if name_set {
return Err(Error::new(name_opt.unwrap().span(), "Widget name is specified more than once (attribute, assignment or local attribute).").into());
}
name_set = true;
}
if name_set && (attr.is_local_attr() || template_attr == WidgetTemplateAttr::TemplateChild)
{
return Err(Error::new(input.span(), "Widget name is specified more than once (attribute, assignment or local attribute).").into());
}
let (name, name_assigned_by_user) = if let Some(name) = name_opt.or(new_name) {
(name, true)
} else if attr.is_local_attr() || template_attr == WidgetTemplateAttr::TemplateChild {
(Self::local_attr_name(&func)?, true)
} else {
(func.snake_case_name(), false)
};
let returned_widget = if input.peek(Token![->]) {
let _arrow: Token![->] = input.parse()?;
Some(input.parse()?)
} else {
None
};
Self::check_props(&properties, &template_attr)?;
Ok(Widget {
doc_attr,
attr,
template_attr,
mutable,
name,
name_assigned_by_user,
func,
args,
properties,
assign_wrapper,
ref_token,
deref_token,
returned_widget,
})
}
pub(super) fn parse_for_container_ext(
input: ParseStream<'_>,
func: WidgetFunc,
attributes: Option<Attrs>,
) -> Result<Self, ParseError> {
let (attr, doc_attr, new_name, assign_wrapper, template_attr) =
Self::process_attributes(attributes)?;
if let Some(wrapper) = assign_wrapper {
return Err(Error::new(
wrapper.span(),
"Can't use wrapper types in container assignment.",
)
.into());
}
let properties = if input.peek(Token![,]) {
Properties::default()
} else {
let inner = parse_util::braces(input)?;
Properties::parse(&inner)
};
if attr.is_local_attr() || template_attr == WidgetTemplateAttr::TemplateChild {
if let Some(name) = &new_name {
return Err(Error::new(name.span(), "Widget name is specified more than once (attribute, assignment or local attribute).").into());
}
}
let (name, name_assigned_by_user) = if let Some(name) = new_name {
(name, true)
} else if attr.is_local_attr() || template_attr == WidgetTemplateAttr::TemplateChild {
(Self::local_attr_name(&func)?, true)
} else {
(func.snake_case_name(), false)
};
let ref_token = Some(And::default());
Self::check_props(&properties, &template_attr)?;
Ok(Widget {
doc_attr,
attr,
template_attr,
mutable: None,
name,
name_assigned_by_user,
func,
args: None,
properties,
assign_wrapper,
ref_token,
deref_token: None,
returned_widget: None,
})
}
fn check_props(
props: &Properties,
template_attr: &WidgetTemplateAttr,
) -> Result<(), ParseError> {
if template_attr != &WidgetTemplateAttr::Template {
for prop in &props.properties {
if let PropertyType::Widget(widget) = &prop.ty {
if widget.template_attr == WidgetTemplateAttr::TemplateChild {
return Err(ParseError::Generic(
syn::Error::new(
widget.name.span(),
"You can't use a template child if the parent is not a template.",
)
.to_compile_error(),
));
}
}
}
}
Ok(())
}
fn process_attributes(attrs: Option<Attrs>) -> Result<AttributeInfo, ParseError> {
if let Some(attrs) = attrs {
let mut widget_attr = WidgetAttr::None;
let mut doc_attr: Option<TokenStream2> = None;
let mut name = None;
let mut assign_wrapper = None;
let mut template_attr = WidgetTemplateAttr::None;
for attr in attrs.inner {
let span = attr.span();
match attr {
Attr::Local(_) => {
if widget_attr == WidgetAttr::None {
widget_attr = WidgetAttr::Local;
} else {
return Err(attr_twice_error(span).into());
}
}
Attr::LocalRef(_) => {
if widget_attr == WidgetAttr::None {
widget_attr = WidgetAttr::LocalRef;
} else {
return Err(attr_twice_error(span).into());
}
}
Attr::Doc(tokens) => {
if let Some(doc_tokens) = &mut doc_attr {
doc_tokens.extend(tokens);
} else {
doc_attr = Some(tokens);
}
}
Attr::Name(_, name_value) => {
if name.is_some() {
return Err(attr_twice_error(span).into());
}
name = Some(name_value);
}
Attr::Wrap(_, path) => {
if assign_wrapper.is_some() {
return Err(attr_twice_error(span).into());
}
assign_wrapper = Some(path.clone());
}
Attr::Template(_) => {
if template_attr != WidgetTemplateAttr::None {
return Err(attr_twice_error(span).into());
}
template_attr = WidgetTemplateAttr::Template;
}
Attr::TemplateChild(_) => {
if template_attr != WidgetTemplateAttr::None {
return Err(attr_twice_error(span).into());
}
template_attr = WidgetTemplateAttr::TemplateChild;
}
_ => {
return Err(Error::new(
attr.span(),
"Widgets can only have docs and `local`, `local_ref`, `wrap`, `name`, `template`, `template_child` or `root` as attribute.",
).into());
}
}
}
Ok((widget_attr, doc_attr, name, assign_wrapper, template_attr))
} else {
Ok((WidgetAttr::None, None, None, None, WidgetTemplateAttr::None))
}
}
fn local_attr_name(func: &WidgetFunc) -> Result<Ident, ParseError> {
if let Some(name) = func.path.get_ident() {
Ok(name.clone())
} else {
Err(Error::new(
func.path.span(),
"Expected identifier due to the `local`, `local_ref` or `template_child` attribute.",
)
.into())
}
}
fn parse_widget_func(input: ParseStream<'_>) -> Result<WidgetFuncInfo, ParseError> {
let ref_token = input.parse().ok();
let deref_token = input.parse().ok();
let func = WidgetFunc::parse(input)?;
let properties = if input.peek(Token![,]) {
Properties::default()
} else {
let inner = parse_util::braces(input)?;
Properties::parse(&inner)
};
Ok((ref_token, deref_token, func, properties))
}
}