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
use super::*;
impl HtmlElement {
pub(crate) fn generate_builder(&self) -> proc_macro2::TokenStream {
use syn::spanned::Spanned;
use HtmlElement::*;
match self {
Tagged(element) => {
let tag = &element.opening_tag.tag;
let tag_str = tag.to_string();
let generics = &element.opening_tag.generics;
let mut visited_attrs = Vec::new();
let attributes = element
.opening_tag
.attributes
.iter()
.map(|x| {
let many;
let mut values = match &x.value {
Some(HtmlAttributeValue::Block(value)) => {
many = false;
vec![quote_block(value)]
}
Some(HtmlAttributeValue::Lit(lit)) => {
many = false;
vec![quote::ToTokens::to_token_stream(lit)]
}
Some(HtmlAttributeValue::ExprArray(array)) => {
many = true;
array
.elems
.iter()
.map(quote::ToTokens::to_token_stream)
.collect::<Vec<_>>()
}
None => {
many = false;
vec![]
}
};
let mut attrs_count = vec![values.len()];
attrs_count.resize(values.len(), 0);
match &x.name {
HtmlAttributeName::Block(name) => {
let name = quote_block(name);
quote::quote_spanned! {name.span()=>
#(.add_attr(#name, #values, #attrs_count))*
}
}
HtmlAttributeName::Ident(name) => {
if visited_attrs.contains(&name) {
values.clear();
values.push(quote::quote_spanned! {name.span()=>
compile_error!("attribute already defined")
});
}
visited_attrs.push(name);
if many {
let method = quote::format_ident!("add_attr_{}", name);
quote::quote_spanned! {name.span()=>
#(.#method(#values, #attrs_count))*
}
} else {
let method = quote::format_ident!("set_attr_{}", name);
quote::quote_spanned! {name.span()=>
#(.#method(#values))*
}
}
}
HtmlAttributeName::Shorthand { ident, .. } => {
let mut name = quote::ToTokens::to_token_stream(&ident);
if visited_attrs.contains(&ident) {
name = quote::quote_spanned! {ident.span()=>
compile_error!("attribute already defined")
};
}
visited_attrs.push(ident);
let method = quote::format_ident!("set_attr_{}", ident);
quote::quote_spanned! {ident.span()=>
.#method(#name)
}
}
}
})
.collect::<Vec<_>>();
let children = element
.children
.iter()
.enumerate()
.map(|(i, x)| {
let children_count = if i == 0 { element.children.len() } else { 0 };
let child = x.generate_builder();
quote::quote_spanned! {child.span()=>
.add_child(#child, #children_count)
}
})
.collect::<Vec<_>>();
quote::quote! {
<#tag #generics>::builder(#tag_str)
#(#attributes)*
#(#children)*
.finish()
}
}
Fragmented(fragment) => {
let children = fragment
.children
.iter()
.enumerate()
.map(|(i, x)| {
let children_count = if i == 0 { fragment.children.len() } else { 0 };
let child = x.generate_builder();
quote::quote_spanned! {child.span()=>
.add_child(#child, #children_count)
}
})
.collect::<Vec<_>>();
quote::quote! {
Fragment::builder("")
#(#children)*
.finish()
}
}
Block(block) => quote_block(block),
Format(format) => {
let args = &format.args;
quote::quote_spanned! {args.span()=>
Text::from(format_args!(#args))
}
}
}
}
}
fn quote_block(block: &syn::Block) -> proc_macro2::TokenStream {
if block.stmts.len() == 1 {
quote::ToTokens::to_token_stream(&block.stmts[0])
} else {
quote::ToTokens::to_token_stream(block)
}
}