-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
component.rs
205 lines (184 loc) · 6.39 KB
/
component.rs
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
use std::convert::TryFrom;
use proc_macro2::{Ident, Span, TokenStream};
use quote::{quote, quote_spanned, ToTokens};
use syn::parse::{Parse, ParseStream};
use syn::spanned::Spanned;
use syn::token::Dot2;
use syn::Expr;
use super::{Prop, Props, SpecialProps, CHILDREN_LABEL};
struct BaseExpr {
pub dot2: Dot2,
pub expr: Expr,
}
impl Parse for BaseExpr {
fn parse(input: ParseStream) -> syn::Result<Self> {
let dot2 = input.parse()?;
let expr = input.parse().map_err(|expr_error| {
let mut error =
syn::Error::new_spanned(dot2, "expected base props expression after `..`");
error.combine(expr_error);
error
})?;
Ok(Self { dot2, expr })
}
}
impl ToTokens for BaseExpr {
fn to_tokens(&self, tokens: &mut TokenStream) {
let BaseExpr { dot2, expr } = self;
tokens.extend(quote! { #dot2 #expr });
}
}
pub struct ComponentProps {
props: Props,
base_expr: Option<Expr>,
}
impl ComponentProps {
/// Get the special props supported by both variants
pub fn special(&self) -> &SpecialProps {
&self.props.special
}
// check if the `children` prop is given explicitly
pub fn children(&self) -> Option<&Prop> {
self.props.get_by_label(CHILDREN_LABEL)
}
fn prop_validation_tokens(&self, props_ty: impl ToTokens, has_children: bool) -> TokenStream {
let props_ident = Ident::new("__yew_props", props_ty.span());
let check_children = if has_children {
Some(quote_spanned! {props_ty.span()=>
let _ = #props_ident.children;
})
} else {
None
};
let check_props: TokenStream = self
.props
.iter()
.map(|Prop { label, .. }| {
quote_spanned! {Span::call_site().located_at(label.span())=>
let __yew_check_props = #props_ident.#label;
}
})
.collect();
quote_spanned! {props_ty.span()=>
#[allow(clippy::no_effect)]
if false {
let _ = |#props_ident: #props_ty| {
#check_children
#check_props
};
};
}
}
pub fn build_properties_tokens<CR: ToTokens>(
&self,
props_ty: impl ToTokens,
children_renderer: Option<CR>,
) -> TokenStream {
let has_children = children_renderer.is_some();
let validate_props = self.prop_validation_tokens(&props_ty, has_children);
let build_props = match &self.base_expr {
None => {
let builder_ident = Ident::new("__yew_props", props_ty.span());
let token_ident = Ident::new(
"__yew_required_props_token",
props_ty.span().resolved_at(Span::mixed_site()),
);
let init_builder = quote_spanned! {props_ty.span()=>
let mut #builder_ident = <#props_ty as ::yew::html::Properties>::builder();
let #token_ident = ::yew::html::AssertAllProps;
};
let set_props = self.props.iter().map(|Prop { label, value, .. }| {
quote_spanned! {value.span()=>
let #token_ident = #builder_ident.#label(#token_ident, #value);
}
});
let set_children = children_renderer.map(|children| {
quote_spanned! {props_ty.span()=>
let #token_ident = #builder_ident.children(#token_ident, #children);
}
});
let build_builder = quote_spanned! {props_ty.span()=>
::yew::html::Buildable::prepare_build(#builder_ident, &#token_ident).build()
};
quote! {
#init_builder
#( #set_props )*
#set_children
#build_builder
}
}
// Builder pattern is unnecessary in this case, since the base expression guarantees
// all values are initialized
Some(expr) => {
let ident = Ident::new("__yew_props", props_ty.span());
let set_props = self.props.iter().map(|Prop { label, value, .. }| {
quote_spanned! {value.span().resolved_at(Span::call_site())=>
#ident.#label = ::yew::html::IntoPropValue::into_prop_value(#value);
}
});
let set_children = children_renderer.map(|children| {
quote_spanned! {props_ty.span()=>
#ident.children = #children;
}
});
let init_base = quote_spanned! {expr.span().resolved_at(Span::call_site())=>
let mut #ident: #props_ty = #expr;
};
quote! {
#init_base
#(#set_props)*
#set_children
#ident
}
}
};
quote! {
{
#validate_props
#build_props
}
}
}
}
impl Parse for ComponentProps {
fn parse(input: ParseStream) -> syn::Result<Self> {
let props = validate(input.parse()?)?;
let base_expr = if input.is_empty() {
None
} else {
Some(input.parse::<BaseExpr>()?)
};
if input.is_empty() {
let base_expr = base_expr.map(|base| base.expr);
Ok(Self { props, base_expr })
} else {
Err(syn::Error::new_spanned(
base_expr,
"base props expression must appear last in list of props",
))
}
}
}
impl TryFrom<Props> for ComponentProps {
type Error = syn::Error;
fn try_from(props: Props) -> Result<Self, Self::Error> {
Ok(Self {
props: validate(props)?,
base_expr: None,
})
}
}
fn validate(props: Props) -> Result<Props, syn::Error> {
props.check_no_duplicates()?;
props.check_all(|prop| {
if !prop.label.extended.is_empty() {
Err(syn::Error::new_spanned(
&prop.label,
"expected a valid Rust identifier",
))
} else {
Ok(())
}
})?;
Ok(props)
}