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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use proc_macro::TokenStream;
use quote::*;
use syn::parse::*;
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::*;
use crate::template::SlotType;
use super::template::Template;
use super::i18n::LocaleGroup;
struct ComponentAttr {
items: Punctuated<ComponentAttrItem, token::Comma>,
}
impl Parse for ComponentAttr {
fn parse(input: ParseStream) -> Result<Self> {
let items = Punctuated::parse_terminated(input)?;
Ok(Self { items })
}
}
enum ComponentAttrItem {
Backend {
attr_name: Ident,
#[allow(dead_code)]
equal_token: token::Eq,
impl_token: Option<token::Impl>,
path: Path,
},
SlotData {
attr_name: Ident,
#[allow(dead_code)]
equal_token: token::Eq,
path: Path,
},
Translation {
attr_name: Ident,
#[allow(dead_code)]
equal_token: token::Eq,
name: Ident,
},
}
impl Parse for ComponentAttrItem {
fn parse(input: ParseStream) -> Result<Self> {
let attr_name: Ident = input.parse()?;
let ret = match attr_name.to_string().as_str() {
"Backend" => Self::Backend {
attr_name,
equal_token: input.parse()?,
impl_token: input.parse()?,
path: input.parse()?,
},
"SlotData" => Self::SlotData {
attr_name,
equal_token: input.parse()?,
path: input.parse()?,
},
"Translation" => Self::Translation {
attr_name,
equal_token: input.parse()?,
name: input.parse()?,
},
_ => {
return Err(Error::new(attr_name.span(), "Unknown attribute parameter"));
}
};
Ok(ret)
}
}
struct ComponentBody {
inner: ItemStruct,
component_name: proc_macro2::TokenStream,
backend_param: proc_macro2::TokenStream,
backend_param_in_impl: Option<GenericParam>,
slot_kind: proc_macro2::TokenStream,
slot_data_ty: proc_macro2::TokenStream,
template: Result<Template>,
template_field: Ident,
locale_group: LocaleGroup,
}
impl ComponentBody {
fn new(attr: ComponentAttr, mut inner: ItemStruct) -> Result<Self> {
let mut backend_attr = None;
let mut slot_data_attr = None;
let mut locale_group_name = None;
for item in attr.items {
match item {
ComponentAttrItem::Backend {
attr_name,
impl_token,
path,
..
} => {
if backend_attr.is_some() {
return Err(Error::new(
attr_name.span(),
"Duplicated attribute parameter",
));
}
backend_attr = Some((impl_token, path));
}
ComponentAttrItem::SlotData {
attr_name, path, ..
} => {
if slot_data_attr.is_some() {
return Err(Error::new(
attr_name.span(),
"Duplicated attribute parameter",
));
}
slot_data_attr = Some(path);
}
ComponentAttrItem::Translation { attr_name, name, .. } => {
if locale_group_name.is_some() {
return Err(Error::new(
attr_name.span(),
"Duplicated attribute parameter",
));
}
locale_group_name = Some(name);
}
}
}
let backend_param = match &backend_attr {
None => quote! { __MBackend },
Some((Some(_), path)) => {
let span = path.span();
quote_spanned! {span=> __MBackend }
}
Some((None, path)) => {
let span = path.span();
quote_spanned! {span=> #path }
}
};
let backend_param_in_impl = match backend_attr {
None => Some(parse_quote! { __MBackend: maomi::backend::Backend }),
Some((Some(_), path)) => {
let span = path.span();
Some(parse_quote_spanned! {span=> __MBackend: #path })
}
Some((None, _)) => None,
};
let slot_data_ty = match slot_data_attr {
None => quote! { () },
Some(path) => {
let span = path.span();
quote_spanned! {span=> #path }
}
};
let locale_group = match locale_group_name {
None => LocaleGroup::get_default(),
Some(x) => LocaleGroup::get(&x.to_string()),
};
let component_name = {
let component_name_ident = &inner.ident;
let component_type_params = inner.generics.params.iter().map(|x| {
let span = x.span();
match x {
GenericParam::Type(x) => {
let x = x.ident.clone();
quote_spanned! {span=> #x }
}
GenericParam::Lifetime(x) => {
let x = x.lifetime.clone();
quote_spanned! {span=> #x }
}
GenericParam::Const(x) => {
let x = x.ident.clone();
quote_spanned! {span=> #x }
}
}
});
quote! {
#component_name_ident<#(#component_type_params),*>
}
};
let mut slot_kind = quote! {
maomi::node::NoneSlot
};
let mut template = None;
let mut template_field = None;
if let Fields::Named(fields) = &mut inner.fields {
for field in &mut fields.named {
let mut has_template = false;
if let Type::Macro(m) = &mut field.ty {
if m.mac.path.is_ident("template") {
if template.is_some() {
Err(syn::Error::new(
m.span(),
"a component struct can only contain one `template!` field",
))?;
continue;
}
has_template = true;
}
}
if has_template {
thread_local! {
static EMPTY_TY: Type = parse_str("()").unwrap();
}
if let Type::Macro(m) = &mut field.ty {
let tokens = m.mac.tokens.clone();
let t = Template::parse.parse2(tokens);
if let Ok(x) = &t {
match x.slot_type() {
SlotType::None => {}
SlotType::StaticSingle => {
slot_kind = quote! {
maomi::node::StaticSingleSlot
};
}
SlotType::Dynamic => {
slot_kind = quote! {
maomi::node::DynamicSlot
};
}
}
}
field.ty = parse_quote! {
maomi::template::Template<
#component_name,
maomi::node::DynNodeList,
#slot_kind<maomi::backend::tree::ForestTokenAddr, (maomi::backend::tree::ForestToken, maomi::prop::Prop<#slot_data_ty>)>,
>
};
template = Some(t);
template_field = field.ident.clone();
} else {
unreachable!()
}
}
}
} else {
Err(syn::Error::new(
inner.span(),
"a component struct must be a named struct",
))?;
}
let template = if let Some(t) = template {
t
} else {
return Err(syn::Error::new(
inner.span(),
"a component struct must contain a `template!` field",
));
};
Ok(Self {
inner,
component_name,
backend_param,
backend_param_in_impl,
slot_kind,
slot_data_ty,
template,
template_field: template_field.unwrap(),
locale_group,
})
}
}
impl ToTokens for ComponentBody {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let Self {
inner,
component_name,
backend_param,
backend_param_in_impl,
slot_kind,
slot_data_ty,
template,
template_field,
locale_group,
} = self;
inner.to_tokens(tokens);
let impl_type_params = {
let items = inner
.generics
.params
.iter()
.chain(backend_param_in_impl.as_ref());
quote! {
<#(#items),*>
}
};
let impl_type_params_without_backend_param = {
let items = inner
.generics
.params
.iter();
quote! {
<#(#items),*>
}
};
match template.as_ref() {
Ok(template) => {
let template_children = template.to_children(backend_param, locale_group);
quote! {
impl #impl_type_params_without_backend_param maomi::template::ComponentSlotKind for #component_name {
type SlotChildren<C> = #slot_kind<maomi::backend::tree::ForestTokenAddr, C>;
type SlotData = #slot_data_ty;
}
}.to_tokens(tokens);
quote! {
impl #impl_type_params maomi::template::ComponentTemplate<#backend_param> for #component_name {
type TemplateField = maomi::template::Template<
Self,
Self::TemplateStructure,
Self::SlotChildren<(maomi::backend::tree::ForestToken, maomi::prop::Prop<Self::SlotData>)>,
>;
type TemplateStructure = maomi::node::DynNodeList;
#[inline]
fn template(&self) -> &Self::TemplateField {
&self.#template_field
}
#[inline]
fn template_init(&mut self, __m_init: maomi::template::TemplateInit<#component_name>) {
self.#template_field.init(__m_init);
}
#[inline]
fn template_create_or_update<'__m_b>(
&'__m_b mut self,
__m_backend_context: &'__m_b maomi::BackendContext<#backend_param>,
__m_backend_element: &'__m_b mut maomi::backend::tree::ForestNodeMut<
<#backend_param as maomi::backend::Backend>::GeneralElement,
>,
__m_slot_fn: &mut dyn FnMut(
maomi::node::SlotChange<
&mut maomi::backend::tree::ForestNodeMut<
<#backend_param as maomi::backend::Backend>::GeneralElement,
>,
&maomi::backend::tree::ForestToken,
&Self::SlotData,
>,
) -> Result<(), maomi::error::Error>,
) -> Result<(), maomi::error::Error>
where
Self: Sized,
{
let __m_event_self_weak = maomi::template::TemplateHelper::component_weak(
&self.#template_field,
).unwrap();
let mut __m_slot_scopes = self.#template_field.__m_slot_scopes.borrow_mut();
let mut __m_slot_scopes = maomi::node::SlotKindTrait::update(&mut *__m_slot_scopes);
{
let __m_slot_scopes = &mut __m_slot_scopes;
let __m_self_owner_weak = self.#template_field.__m_self_owner_weak.as_ref().unwrap();
let __m_parent_element = __m_backend_element;
let mut __m_children_results = #template_children;
if let Some(__m_children) = self.#template_field.__m_structure.as_ref() {
__m_children_results(__m_parent_element, Some(&mut *__m_children.borrow_mut()))?;
} else {
self.#template_field.__m_structure = Some(std::cell::RefCell::new(
unsafe { __m_children_results(__m_parent_element, None)?.unwrap_unchecked() }
));
}
}
maomi::node::SlotKindUpdateTrait::finish(__m_slot_scopes, |(n, _)| {
__m_slot_fn(maomi::node::SlotChange::Removed(&n))?;
Ok(())
})?;
Ok(())
}
}
}.to_tokens(tokens);
}
Err(err) => {
err.to_compile_error().to_tokens(tokens);
},
}
}
}
pub fn component(attr: TokenStream, item: TokenStream) -> TokenStream {
let component_attr = parse_macro_input!(attr as ComponentAttr);
match ComponentBody::new(component_attr, parse_macro_input!(item as ItemStruct)) {
Ok(component_body) => {
quote! {
#component_body
}.into()
}
Err(err) => err.to_compile_error().into(),
}
}