frender-macros 1.0.0-alpha.8

macros for frender
Documentation
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
use std::borrow::Cow;

use quote::{quote, quote_spanned, ToTokens, TokenStreamExt};
use syn::{
    parse_quote_spanned,
    punctuated::{Pair, Punctuated},
    spanned::Spanned,
};

use crate::err::OutputError;

use super::props_data::*;

impl PropsDefinitionWithOptions {
    pub fn into_tokens(self, tokens: &mut proc_macro2::TokenStream) {
        let Self {
            errors,
            options: PropsOptions {},
            definition:
                PropsDefinition {
                    vis,
                    attrs,
                    struct_token,
                    ident,
                    generics,
                    fields,
                },
        } = self;

        let span = ident.span();

        let (impl_generics, type_generics, where_clause) = generics.split_for_impl();
        let tp = &generics.params;

        let fields = match fields.to_fields_impl() {
            Ok(v) => v,
            Err(err) => return tokens.append_all(err.into_compile_error()),
        };

        // struct MyProps { ... }
        let t_struct_props = {
            let props_struct_fields = fields
                .iter()
                .map(PropsFieldImpl::to_props_struct_field_tokens);

            quote! {
                #(#attrs)*
                #vis #struct_token #ident #type_generics #where_clause {
                    #(#props_struct_fields),*
                }
            }
        };

        let info = fields.iter().map(|f| f.to_info(&ident)).collect::<Vec<_>>();
        let (required, optional) = info
            .iter()
            .partition::<Vec<_>, _>(|v| v.builder_type_param.is_some());

        let fields_all_optional = required.len() == 0;

        let builder_ident = if fields_all_optional {
            Cow::Borrowed(&ident)
        } else {
            Cow::Owned(quote::format_ident!("{}Builder", ident))
        };

        let builder_init = info.iter().map(|v| &v.builder_init);

        let (initial_builder_tps, t_impl_builder) = {
            if fields_all_optional {
                // all fields are optional
                // the MyProps struct can be the builder for itself
                let builder_fns = info.iter().map(
                    |PropsFieldInfo {
                         ident,
                         ty: _,
                         builder,
                         ..
                     }| {
                        let PropsFieldBuilder {
                            arrow,
                            fn_arg,
                            generics,
                            fn_block,
                            ..
                        } = builder.as_ref();
                        let (gp, _, where_clause) = generics.split_for_impl();
                        quote! {
                            #vis fn #ident #gp (mut self, #fn_arg) #arrow Self
                                #where_clause {
                                #![allow(unused_braces)]
                                self.#ident = #fn_block;
                                self
                            }
                        }
                    },
                );

                let t_impl_builder = quote! {
                    impl #impl_generics ::frender::react::PropsBuilder::<Self> for #ident #type_generics
                        #where_clause {
                        #[inline]
                        fn build(self) -> Self {
                            self
                        }
                    }
                    impl #impl_generics #builder_ident #type_generics #where_clause {
                        #(#builder_fns)*
                    }
                };

                (type_generics.to_token_stream(), t_impl_builder)
            } else {
                let (builder_tp, builder_valid_type): (Vec<_>, Vec<_>) = required
                    .iter()
                    .map(|v| {
                        let v = v.builder_type_param.as_ref().unwrap();
                        (&v.0, &v.1)
                    })
                    .unzip();
                let builder_tp_ts = builder_tp
                    .iter()
                    .map(|v| v.to_token_stream())
                    .collect::<Vec<_>>();
                let builder_tp_ts = builder_tp_ts.iter().collect::<Vec<_>>();
                let (
                    builder_impl_generics,
                    builder_type_params,
                    builder_valid_type_params,
                    type_params_with_comma,
                ) = {
                    let tp_comma = if tp.empty_or_trailing() {
                        None
                    } else {
                        Some(syn::Token![,](span))
                    };

                    let type_params =
                        Punctuated::<_, syn::Token![,]>::from_iter(tp.pairs().map(|p| {
                            let (gp, c) = p.into_tuple();
                            let ident_or_lt = match gp {
                                syn::GenericParam::Type(t) => t.ident.to_token_stream(),
                                syn::GenericParam::Lifetime(lt) => lt.lifetime.to_token_stream(),
                                syn::GenericParam::Const(c) => c.ident.to_token_stream(),
                            };
                            Pair::new(ident_or_lt, c)
                        }));

                    let type_params_with_comma = quote!(#type_params #tp_comma);

                    (
                        quote!(<#tp #tp_comma #(#builder_tp),*>),
                        quote!(<#type_params_with_comma #(#builder_tp),*>),
                        quote!(<#type_params_with_comma #(#builder_valid_type),*>),
                        type_params_with_comma,
                    )
                };

                let not_set = quote!(::frender::react::__private::required_prop::PropertyNotSet);
                let not_set = (0..(required.len())).map(|_| &not_set);
                let initial_builder_tps = quote!(
                    <#type_params_with_comma #(#not_set),*>
                );
                let required_field_defs = required.iter().map(|v| {
                    let ident = v.ident;
                    let ty = &v.builder_type_param.as_ref().unwrap().0;
                    quote!(#ident : #ty)
                });
                let optional_field_defs = optional.iter().map(|v| {
                    let ident = v.ident;
                    let ty = v.ty;
                    quote!(#ident : #ty)
                });

                let required_field_set = required
                    .iter()
                    .map(|PropsFieldInfo { ident, .. }| quote!( #ident : self.#ident.0 ));
                let required_field_set = quote!(#(#required_field_set),*);

                let optional_field_set = optional
                    .iter()
                    .map(|PropsFieldInfo { ident, .. }| quote!( #ident : self.#ident ));
                let optional_field_set = quote!(#(#optional_field_set),*);

                let required_field_set_builder = required
                    .iter()
                    .map(|PropsFieldInfo { ident, .. }| quote!( #ident : self.#ident ))
                    .collect::<Vec<_>>();
                let required_field_set_wrapped = quote!(
                    ::frender::react::__private::required_prop::PropertyToBeSet(value)
                );

                let required_field_fns = required.iter().enumerate().map(|(idx, v)| {
                    let PropsFieldInfo {
                        ident, ty, builder, ..
                    } = v;

                    let required_field_set_wrapped = quote!(#ident : #required_field_set_wrapped);
                    let mut field_set = required_field_set_builder.iter().collect::<Vec<_>>();
                    field_set[idx] = &required_field_set_wrapped;

                    let PropsFieldBuilder {
                        arrow,
                        fn_arg,
                        generics,
                        fn_block,
                        ..
                    } = builder.as_ref();
                    let (gp, _, where_clause) = generics.split_for_impl();

                    let tp_new = {
                        let mut tp_new = builder_tp_ts.clone();
                        tp_new[idx] = &builder_valid_type[idx];
                        tp_new
                    };

                    quote! {
                        #vis fn #ident #gp (self, #fn_arg ) #arrow #builder_ident < #type_params_with_comma #(#tp_new),* >
                            #where_clause {
                                // #![allow(unused_braces)]
                                let value: #ty = #fn_block;
                                #builder_ident {
                                    #(#field_set),*
                                    ,
                                    #optional_field_set
                                }
                        }
                    }
                });

                let optional_field_fns = optional.iter().map(|v| {
                    let PropsFieldInfo { ident, builder, .. } = v;
                    let PropsFieldBuilder {
                        arrow,
                        fn_arg,
                        generics,
                        fn_block,
                        ..
                    } = builder.as_ref();
                    let (gp, _, where_clause) = generics.split_for_impl();

                    quote! {
                        #vis fn #ident #gp (mut self, #fn_arg ) #arrow Self
                            #where_clause {
                                #![allow(unused_braces)]
                                self.#ident = #fn_block;
                                self
                        }
                    }
                });

                let t_impl_builder = quote_spanned! {span=>
                    #[allow(non_camel_case_types)]
                    #vis #struct_token #builder_ident #builder_impl_generics #where_clause {
                        #(#required_field_defs),*
                        ,
                        #(#optional_field_defs),*
                    }

                    impl #impl_generics ::frender::react::PropsBuilder::<#ident #type_generics> for
                        #builder_ident #builder_valid_type_params
                        #where_clause {
                        fn build(self) -> #ident #type_generics {
                            #ident {
                                #required_field_set,
                                #optional_field_set
                            }
                        }
                    }

                    #[allow(non_camel_case_types)]
                    impl #builder_impl_generics #builder_ident #builder_type_params #where_clause {
                        #(#required_field_fns)*
                        #(#optional_field_fns)*
                    }
                };

                (initial_builder_tps, t_impl_builder)
            }
        };

        let t_impl_props = quote_spanned! {span=>
            impl ::frender::react::Props for #ident {
                type InitialBuilder = #builder_ident #initial_builder_tps;

                fn init_builder() -> Self::InitialBuilder {
                    Self::InitialBuilder {
                        #(#builder_init),*
                    }
                }
            }
        };

        tokens.append_all(t_struct_props);
        tokens.append_all(t_impl_props);
        tokens.append_all(t_impl_builder);

        if let Some(error) = errors.output_error() {
            tokens.append_all(error.write_errors());
        }
    }
}

impl PropsFields {
    fn to_fields_impl(&self) -> syn::Result<Vec<PropsFieldImpl>> {
        let fields = self.named.iter().map(|f| f.to_field_impl());

        let mut res = Ok(Vec::with_capacity(fields.len()));

        for f in fields {
            match &mut res {
                Ok(vec) => match f {
                    Ok(f) => {
                        vec.push(f);
                    }
                    Err(err) => res = Err(err),
                },
                Err(err) => {
                    if let Err(e) = f {
                        err.combine(e);
                    }
                }
            }
        }

        res
    }
}

struct PropsFieldImpl<'a> {
    /// Attributes tagged on the field.
    pub attrs: &'a Vec<syn::Attribute>,

    /// Visibility of the field.
    pub vis: &'a syn::Visibility,

    /// Name of the field
    pub ident: &'a syn::Ident,
    // whether the field is optional
    pub question: &'a Option<syn::Token![?]>,
    pub colon: Cow<'a, syn::Token![:]>,
    pub ty: Cow<'a, syn::Type>,
    pub builder: Cow<'a, PropsFieldBuilder>,
}

struct PropsFieldInfo<'a> {
    ident: &'a syn::Ident,
    ty: &'a Cow<'a, syn::Type>,
    /// If `None`, this field is optional
    ///
    /// ```ignore
    /// struct MyBuilder<MyBuilder__name> {
    ///     //           =============== ident
    ///     name: MyBuilder__name
    /// }
    /// ```
    ///
    /// ```ignore
    /// impl PropsBuilder<MyProps> for MyBuilder<PropertyToBeSet<String>> {
    ///     //                                   ======================= type
    /// }
    /// ```
    builder_type_param: Option<(syn::Ident, proc_macro2::TokenStream)>,
    /// ```ignore
    /// # MyBuilder {
    ///     optional_prop: Default::default(),
    ///     required_prop: PropertyNotSet
    /// # }
    /// ```
    builder_init: proc_macro2::TokenStream,
    builder: &'a Cow<'a, PropsFieldBuilder>,
}

impl<'a> PropsFieldImpl<'a> {
    #[inline]
    pub fn as_required_field(&self) -> Option<&Self> {
        if self.is_optional() {
            None
        } else {
            Some(self)
        }
    }

    #[inline]
    pub fn is_optional(&self) -> bool {
        self.question.is_some()
    }

    #[inline]
    pub fn is_required(&self) -> bool {
        self.question.is_none()
    }

    pub fn to_info(&self, props_ident: &syn::Ident) -> PropsFieldInfo {
        let PropsFieldImpl {
            // vis,
            ident,
            // question,
            colon,
            ty,
            builder,
            ..
        } = self;
        if self.is_optional() {
            PropsFieldInfo {
                ident,
                ty,
                builder_init: quote!( #ident #colon Default::default() ),
                builder_type_param: None,
                builder,
            }
        } else {
            let span = ident.span();
            let tp_ident = quote::format_ident!("{}Builder__{}", props_ident, ident, span = span);
            let builder_init = quote_spanned! {span=>
                    #ident #colon ::frender::react::__private::required_prop::PropertyNotSet
            };

            let valid_type = quote_spanned! {span=>
                ::frender::react::__private::required_prop::PropertyToBeSet::< #ty >
            };
            let builder_type_param = (tp_ident, valid_type);
            PropsFieldInfo {
                ident,
                ty,
                builder_type_param: Some(builder_type_param),
                builder_init,
                builder,
            }
        }
    }

    pub fn to_props_struct_field_tokens(&self) -> proc_macro2::TokenStream {
        let PropsFieldImpl {
            attrs,
            vis,
            ident,
            // question,
            colon,
            ty,
            // builder,
            ..
        } = self;
        quote! {
            #(#attrs)*
            #vis #ident #colon #ty
        }
    }
}

impl PropsField {
    fn to_field_impl(&self) -> syn::Result<PropsFieldImpl<'_>> {
        let span = self.ident.span();
        let (colon, ty, builder) = match &self.type_and_builder {
            PropsFieldTypeAndBuilder::ImplicitBuilder { colon, ty } => (
                Cow::Borrowed(colon),
                Cow::Borrowed(ty),
                Cow::Owned(PropsFieldBuilder::new(span, ty.clone())),
            ),
            PropsFieldTypeAndBuilder::Explicit(ret) => (
                Cow::Owned(syn::Token![:](span)),
                Cow::Borrowed(&ret.ty),
                Cow::Borrowed(&ret.builder),
            ),
            PropsFieldTypeAndBuilder::ImplicitTypeAndBuilder => {
                let ret = infer_field_type_and_builder(&self.ident)?;

                (
                    Cow::Owned(syn::Token![:](span)),
                    Cow::Owned(ret.ty),
                    Cow::Owned(ret.builder),
                )
            }
        };

        Ok(PropsFieldImpl {
            attrs: &self.attrs,
            vis: &self.vis,
            ident: &self.ident,
            question: &self.question,
            colon,
            ty,
            builder,
        })
    }
}

fn infer_field_type_and_builder(
    ident: &syn::Ident,
) -> syn::Result<PropsFieldTypeAndBuilderExplicit> {
    let span = ident.span();
    let name = ident.to_string();
    let name = name.as_str();
    let ret = match name {
        "children" => parse_quote_spanned! {span=>
            <TNode: ::frender::react::Node>(node: Option<TNode>) -> Option<::frender::react::Children> {
                node.and_then(::frender::react::Node::into_children)
            }
        },
        "style" => {
            let ty: syn::TypePath = parse_quote_spanned!(span=>
                Option<::frender::react::CssProperties>);
            let ty = syn::Type::Path(ty);
            PropsFieldTypeAndBuilderExplicit {
                builder: PropsFieldBuilder::new(span, ty.clone()),
                ty,
            }
        }
        "id" | "class_name" | "class" => {
            let ty: syn::TypePath = parse_quote_spanned!(span=>
                Option<String>);
            let ty = syn::Type::Path(ty);
            PropsFieldTypeAndBuilderExplicit {
                builder: PropsFieldBuilder::new(span, ty.clone()),
                ty,
            }
        }
        _ => {
            return Err(syn::Error::new(
                span,
                format!(
                    "Can't infer type for prop {}. Please specify the field type.",
                    name
                ),
            ))
        }
    };

    Ok(ret)
}