-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
builder.rs
206 lines (178 loc) · 6.49 KB
/
builder.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
206
//! The `PropsBuilder` constructs props in alphabetical order and enforces that required props have
//! been set before allowing the build to complete. Each property has a corresponding method in the
//! builder. Required property builder methods advance the builder to the next step, optional
//! properties can be added or skipped with no effect on the build step. Once all of required
//! properties have been set, the builder moves to the final build step which implements the
//! `build()` method.
use super::generics::{to_arguments, with_param_bounds, GenericArguments};
use super::{DerivePropsInput, PropField};
use proc_macro2::{Ident, Span};
use quote::{format_ident, quote, ToTokens};
use syn::Attribute;
pub struct PropsBuilder<'a> {
builder_name: &'a Ident,
step_trait: &'a Ident,
step_names: Vec<Ident>,
props: &'a DerivePropsInput,
wrapper_name: &'a Ident,
extra_attrs: &'a [Attribute],
}
impl ToTokens for PropsBuilder<'_> {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let Self {
builder_name,
step_trait,
step_names,
props,
wrapper_name,
..
} = self;
let DerivePropsInput {
vis,
generics,
props_name,
..
} = props;
let build_step = self.build_step();
let impl_steps = self.impl_steps();
let set_fields = self.set_fields();
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let turbofish_generics = ty_generics.as_turbofish();
let generic_args = to_arguments(generics, build_step.clone());
// Each builder step implements the `BuilderStep` trait and `step_generics` is used to
// enforce that.
let step_generic_param = Ident::new("YEW_PROPS_BUILDER_STEP", Span::mixed_site());
let step_generics =
with_param_bounds(generics, step_generic_param.clone(), (*step_trait).clone());
let builder = quote! {
#(
#[doc(hidden)]
#[allow(non_camel_case_types)]
#vis struct #step_names;
)*
#[doc(hidden)]
#vis trait #step_trait {}
#(
#[automatically_derived]
impl #step_trait for #step_names {}
)*
#[doc(hidden)]
#vis struct #builder_name #step_generics
#where_clause
{
wrapped: ::std::boxed::Box<#wrapper_name #ty_generics>,
_marker: ::std::marker::PhantomData<#step_generic_param>,
}
#impl_steps
#[automatically_derived]
impl #impl_generics #builder_name<#generic_args> #where_clause {
#[doc(hidden)]
#vis fn build(self) -> #props_name #ty_generics {
#props_name #turbofish_generics {
#(#set_fields)*
}
}
}
};
tokens.extend(builder);
}
}
impl<'a> PropsBuilder<'_> {
pub fn new(
name: &'a Ident,
step_trait: &'a Ident,
props: &'a DerivePropsInput,
wrapper_name: &'a Ident,
extra_attrs: &'a [Attribute],
) -> PropsBuilder<'a> {
PropsBuilder {
builder_name: name,
step_trait,
step_names: Self::build_step_names(step_trait, &props.prop_fields),
props,
wrapper_name,
extra_attrs,
}
}
}
impl PropsBuilder<'_> {
pub fn first_step_generic_args(&self) -> GenericArguments {
to_arguments(&self.props.generics, self.first_step().clone())
}
fn first_step(&self) -> &Ident {
&self.step_names[0]
}
fn build_step(&self) -> &Ident {
&self.step_names[self.step_names.len() - 1]
}
fn build_step_names(prefix: &Ident, prop_fields: &[PropField]) -> Vec<Ident> {
let mut step_names: Vec<Ident> = prop_fields
.iter()
.filter(|pf| pf.is_required())
.map(|pf| pf.to_step_name(prefix))
.collect();
step_names.push(format_ident!(
"{}PropsBuilder",
prefix,
span = prefix.span(),
));
step_names
}
fn set_fields(&self) -> impl Iterator<Item = impl ToTokens + '_> {
self.props.prop_fields.iter().map(|pf| pf.to_field_setter())
}
fn impl_steps(&self) -> proc_macro2::TokenStream {
let Self {
builder_name,
props,
step_names,
extra_attrs,
..
} = self;
let DerivePropsInput {
vis,
generics,
prop_fields,
..
} = props;
let (impl_generics, _, where_clause) = generics.split_for_impl();
let mut fields_index = 0;
let mut token_stream = proc_macro2::TokenStream::new();
for (step, step_name) in step_names.iter().enumerate() {
let mut optional_fields = Vec::new();
let mut required_field = None;
if fields_index >= prop_fields.len() {
break;
}
while let Some(pf) = prop_fields.get(fields_index) {
fields_index += 1;
if pf.is_required() {
required_field = Some(pf);
break;
} else {
optional_fields.push(pf);
}
}
// Optional properties keep the builder on the current step
let current_step_arguments = to_arguments(generics, step_name.clone());
let optional_prop_fn = optional_fields
.iter()
.map(|pf| pf.to_build_step_fn(builder_name, ¤t_step_arguments, vis));
// Required properties will advance the builder to the next step
let required_prop_fn = required_field.iter().map(|pf| {
let next_step_name = &step_names[step + 1];
let next_step_arguments = to_arguments(generics, next_step_name.clone());
pf.to_build_step_fn(builder_name, &next_step_arguments, vis)
});
token_stream.extend(quote! {
#[automatically_derived]
#( #extra_attrs )*
impl #impl_generics #builder_name<#current_step_arguments> #where_clause {
#(#optional_prop_fn)*
#(#required_prop_fn)*
}
});
}
token_stream
}
}