ambient_element_component 0.3.1

Helper procedural macro for the Ambient runtime that converts a function to an `ElementComponent`, allowing for more concise definitions of components.
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
//! Provides a macro for converting a function to an `ElementComponent`, allowing for more concise definitions of components.

use itertools::Itertools;
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};

/// Helper macro to implement a `ElementComponent` with a pure free function.
///
/// Rewrites a `fn Component(&mut Hooks, ...Args) -> Element` to a `struct Component` and an implementation
/// of `ElementComponent` for that `Component`, where the trait's `render` function corresponds to this function.
///
/// Example:
///
/// ```ignore
/// pub fn FancyText(
///     hooks: &mut ambient_element::Hooks,
///     /// The message to display
///     msg: String,
///     alpha: f32,
/// ) -> ambient_element::Element {
///     Text::el(msg)
/// }
/// ```
///
/// becomes:
///
/// ```ignore
/// #[derive(std::clone::Clone, std::fmt::Debug)]
/// pub struct FancyText {
///     /// The message to display
///     pub msg: String,
///     pub alpha: f32,
/// }
/// impl ambient_element::ElementComponent for FancyText {
///     fn render(self: Box<Self>, hooks: &mut ambient_element::Hooks) -> ambient_element::Element {
///         let Self { msg, alpha } = *self;
///         {
///             Text::el(msg)
///         }
///     }
/// }
/// ```
#[proc_macro_attribute]
pub fn element_component(
    input: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    do_derive_element_component(input.into(), item.into()).into()
}

fn do_derive_element_component(input: TokenStream, item: TokenStream) -> TokenStream {
    let with_el = if input.is_empty() {
        true
    } else {
        let input = match syn::parse2::<syn::Ident>(input) {
            Ok(ident) => ident,
            Err(err) => return err.to_compile_error(),
        };

        if input == "without_el" {
            false
        } else {
            panic!("unexpected argument {input} to macro");
        }
    };

    let item = match syn::parse2::<syn::ItemFn>(item) {
        Ok(syntax_tree) => syntax_tree,
        Err(err) => return err.to_compile_error(),
    };

    fn get_pat_type(arg: &syn::FnArg) -> Option<&syn::PatType> {
        match arg {
            syn::FnArg::Receiver(_) => None,
            syn::FnArg::Typed(t) => Some(t),
        }
    }

    if item.sig.receiver().is_some() {
        panic!("a `self` was specified in `{}`; your function must be a free function for this macro to work", item.sig.to_token_stream());
    }

    let attrs = item.attrs;
    let name = item.sig.ident;
    let ret = item.sig.output;

    let (generic_params, generic_idents) = {
        let generic_params = item.sig.generics.params.into_iter().collect_vec();
        let generic_idents = generic_params
            .iter()
            .map(|p| match p {
                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(),
            })
            .collect_vec();

        let generic_params = if generic_params.is_empty() {
            quote! {}
        } else {
            quote! {<#(#generic_params),*>}
        };

        let generic_idents = if generic_idents.is_empty() {
            quote! {}
        } else {
            quote! {<#(#generic_idents),*>}
        };

        (generic_params, generic_idents)
    };
    let where_clause = item.sig.generics.where_clause;

    let visibility = item.vis;
    let body = item.block;

    let inputs = item.sig.inputs.into_iter().collect_vec();
    // NOTE(philpax): It is technically possible to support destructuring, but it's a niche use-case
    // for this particular application. Feel free to change it if you feel otherwise!
    assert!(
        inputs
            .iter()
            .flat_map(get_pat_type)
            .map(|t| match *t.pat {
                syn::Pat::Ident(_) => 0,
                syn::Pat::Wild(_) => 0,
                _ => 1,
            })
            .sum::<usize>()
            == 0,
        "your function signature uses destructuring; this macro only supports identifiers at present"
    );
    let (hooks, props) = inputs.split_at(1);

    // dirty check to ensure the first arguments is &mut hooks
    fn check_type_is_mut_ref(ty: &syn::Type, ident: &str) {
        if let syn::Type::Reference(tr) = ty {
            assert!(
                tr.mutability.is_some(),
                "expected {ty:?} to be a mutable reference"
            );
            if let syn::Type::Path(path) = tr.elem.as_ref() {
                assert_eq!(
                    path.path.segments.last().unwrap().ident,
                    ident,
                    "expected the last segment of the path to equal {ident}"
                );
            } else {
                panic!("expected {tr:?} to be a mutable reference to a path");
            }
        } else {
            panic!("expected {ty:?} to be a reference to {ident}");
        }
    }
    check_type_is_mut_ref(get_pat_type(&hooks[0]).unwrap().ty.as_ref(), "Hooks");

    let pub_props = props
        .iter()
        .flat_map(get_pat_type)
        .map(|pt| {
            let attrs = &pt.attrs;
            let pat = pt.pat.as_ref();
            let ty = pt.ty.as_ref();
            quote! {
                #(#attrs)*
                pub #pat: #ty
            }
        })
        .collect_vec();

    let struct_body = if pub_props.is_empty() {
        quote! {
            ;
        }
    } else {
        quote! {
            {
                #(#pub_props,)*
            }
        }
    };
    let (props_names_braced, struct_unpack) = if !props.is_empty() {
        let props_names = props
            .iter()
            .flat_map(get_pat_type)
            .map(|p| p.pat.as_ref())
            .collect_vec();
        let braced = quote! { { #(#props_names),* }  };
        (
            Some(braced.clone()),
            Some(quote! { let Self #braced = *self; }),
        )
    } else {
        (None, None)
    };

    let el_block = with_el.then(|| {
        let args = props
            .iter()
            .flat_map(get_pat_type)
            .map(|pt| {
                let pat = pt.pat.as_ref();
                let ty = pt.ty.as_ref();
                quote! { #pat: #ty }
            })
            .collect_vec();

        quote! {
            impl #generic_params #name #generic_idents #where_clause {
                #[allow(clippy::too_many_arguments)]
                /// Create an `Element` from this component.
                pub fn el(#(#args),*) -> ambient_element::Element {
                    use ambient_element::ElementComponentExt;
                    Self #props_names_braced .el()
                }
            }
        }
    });

    quote! {
        #[derive(std::clone::Clone, std::fmt::Debug)]
        #(#attrs)*
        #visibility struct #name #generic_params #where_clause #struct_body
        impl #generic_params ambient_element::ElementComponent for #name #generic_idents #where_clause {
            fn render(self: Box<Self>, #(#hooks),*) #ret {
                #struct_unpack
                #body
            }
        }
        #el_block
    }
}

#[cfg(test)]
mod test {
    use proc_macro2::TokenStream;
    use quote::quote;

    #[test]
    #[should_panic(
        expected = "assertion `left == right` failed: expected the last segment of the path to equal Hooks\n  left: Ident(NotAValidHooks)\n right: \"Hooks\""
    )]
    fn test_invalid_base_args_1() {
        let input = quote! {
            pub fn ZeroArg(_: &mut NotAValidHooks) -> ambient_element::Element {
                Element::new()
            }
        };

        let _ = super::do_derive_element_component(quote! {without_el}, input);
    }

    #[test]
    #[should_panic(
        expected = "a `self` was specified in `fn ZeroArg (& self , _ : & mut ambient_element :: Hooks) -> ambient_element :: Element`; your function must be a free function for this macro to work"
    )]
    fn test_zero_arg_with_self_component() {
        let input = quote! {
            pub fn ZeroArg(&self, _: &mut ambient_element::Hooks) -> ambient_element::Element {
                Element::new()
            }
        };

        let _ = super::do_derive_element_component(quote! {without_el}, input);
    }

    #[test]
    fn test_zero_arg_component() {
        let input = quote! {
            #[doc = "My cool comment"]
            pub fn ZeroArg(
                _: &mut ambient_element::Hooks
            ) -> ambient_element::Element {
                Element::new()
            }
        };

        let output = quote! {
            #[derive(std::clone::Clone, std::fmt::Debug)]
            #[doc = "My cool comment"]
            pub struct ZeroArg;
            impl ambient_element::ElementComponent for ZeroArg {
                fn render(self: Box<Self>, _: &mut ambient_element::Hooks) -> ambient_element::Element {
                    {
                        Element::new()
                    }
                }
            }
        };

        assert_eq!(
            super::do_derive_element_component(quote! {without_el}, input).to_string(),
            output.to_string()
        );
    }

    #[test]
    fn test_zero_arg_with_el_component() {
        let input = quote! {
            #[doc = "My cool comment"]
            pub fn ZeroArg(
                _: &mut ambient_element::Hooks
            ) -> ambient_element::Element {
                Element::new()
            }
        };

        let output = quote! {
            #[derive(std::clone::Clone, std::fmt::Debug)]
            #[doc = "My cool comment"]
            pub struct ZeroArg;
            impl ambient_element::ElementComponent for ZeroArg {
                fn render(self: Box<Self>, _: &mut ambient_element::Hooks) -> ambient_element::Element {
                    {
                        Element::new()
                    }
                }
            }
            impl ZeroArg {
                #[allow(clippy::too_many_arguments)]
                /// Create an `Element` from this component.
                pub fn el() -> ambient_element::Element {
                    use ambient_element::ElementComponentExt;
                    Self.el()
                }
            }
        };

        assert_eq!(
            super::do_derive_element_component(TokenStream::new(), input).to_string(),
            output.to_string()
        );
    }

    #[test]
    fn test_single_arg_component() {
        let input = quote! {
            pub fn FancyText(
                _: &mut ambient_element::Hooks,
                /// The message to display
                msg: String,
            ) -> ambient_element::Element {
                Text::el(msg)
            }
        };

        let output = quote! {
            #[derive(std::clone::Clone, std::fmt::Debug)]
            pub struct FancyText {
                /// The message to display
                pub msg: String,
            }
            impl ambient_element::ElementComponent for FancyText {
                fn render(self: Box<Self>,  _: &mut ambient_element::Hooks) -> ambient_element::Element {
                    let Self { msg } = *self;
                    {
                        Text::el(msg)
                    }
                }
            }
            impl FancyText {
                #[allow(clippy::too_many_arguments)]
                /// Create an `Element` from this component.
                pub fn el(msg: String) -> ambient_element::Element {
                    use ambient_element::ElementComponentExt;
                    Self { msg } .el()
                }
            }
        };

        assert_eq!(
            super::do_derive_element_component(quote! {}, input).to_string(),
            output.to_string()
        );
    }

    #[test]
    #[should_panic(
        expected = "your function signature uses destructuring; this macro only supports identifiers at present"
    )]
    fn test_single_arg_component_with_destructuring() {
        let input = quote! {
            pub fn FancyText(
                _: &mut ambient_element::Hooks,
                /// The message to display
                Wrap(msg): Wrap,
            ) -> ambient_element::Element {
                Text::el(msg)
            }
        };

        let _ = super::do_derive_element_component(quote! {without_el}, input);
    }

    #[test]
    fn test_choice_component_with_el() {
        let input = quote! {
            pub(crate) fn Choice(
                _: &mut ambient_element::Hooks,
                msg: CowStr,
                choices: Vec<(CowStr, WorldCallback)>,
                post: WorldCallback,
            ) -> ambient_element::Element {
                let buttons = choices
                    .into_iter()
                    .map(|(label, cb)| Button::new(label.to_string(), closure::closure!(std::clone::clone post, |w| {(cb)(w); (post)(w)})).el())
                    .collect_vec();

                FlowColumn::el([Text::el(msg), FlowRow(buttons).el()]).set(space_between_items(), 10.0)
            }
        };

        let output = quote! {
            #[derive(std::clone::Clone, std::fmt::Debug)]
            pub(crate) struct Choice {
                pub msg: CowStr,
                pub choices: Vec<(CowStr, WorldCallback)>,
                pub post: WorldCallback,
            }
            impl ambient_element::ElementComponent for Choice {
                fn render(self: Box<Self>, _: &mut ambient_element::Hooks) -> ambient_element::Element {
                    let Self { msg, choices, post } = *self;
                    {
                        let buttons = choices
                            .into_iter()
                            .map(|(label, cb)| Button::new(label.to_string(), closure::closure!(std::clone::clone post, |w| {(cb)(w); (post)(w)})).el())
                            .collect_vec();

                        FlowColumn::el([Text::el(msg), FlowRow(buttons).el()]).set(space_between_items(), 10.0)
                    }
                }
            }
            impl Choice {
                #[allow(clippy::too_many_arguments)]
                /// Create an `Element` from this component.
                pub fn el(msg: CowStr, choices: Vec<(CowStr, WorldCallback)>, post: WorldCallback) -> ambient_element::Element {
                    use ambient_element::ElementComponentExt;
                    Self { msg, choices, post }.el()
                }
            }
        };

        assert_eq!(
            super::do_derive_element_component(TokenStream::new(), input).to_string(),
            output.to_string()
        );
    }

    #[test]
    fn test_component_with_generics() {
        let input = quote! {
            pub(crate) fn GenericComponent<T1: Debug + 'static, T2>(
                _: &mut ambient_element::Hooks,
                _a: T1,
                _b: T2,
            ) -> ambient_element::Element
            where T2: Debug + 'static {
                Element::new()
            }
        };

        let output = quote! {
            #[derive(std::clone::Clone, std::fmt::Debug)]
            pub(crate) struct GenericComponent<T1: Debug + 'static, T2> where T2: Debug + 'static {
                pub _a: T1,
                pub _b: T2,
            }
            impl<T1: Debug + 'static, T2> ambient_element::ElementComponent for GenericComponent<T1, T2> where T2: Debug + 'static {
                fn render(self: Box<Self>, _: &mut ambient_element::Hooks) -> ambient_element::Element {
                    let Self { _a, _b } = *self;
                    {
                        Element::new()
                    }
                }
            }
        };

        assert_eq!(
            super::do_derive_element_component(quote! {without_el}, input).to_string(),
            output.to_string()
        );
    }

    #[test]
    fn test_component_with_generics_and_el() {
        let input = quote! {
            pub(crate) fn GenericComponent<T1: Debug + 'static, T2>(
                _: &mut ambient_element::Hooks,
                a: T1,
                b: T2,
            ) -> ambient_element::Element
            where T2: Debug + 'static {
                Element::new()
            }
        };

        let output = quote! {
            #[derive(std::clone::Clone, std::fmt::Debug)]
            pub(crate) struct GenericComponent<T1: Debug + 'static, T2> where T2: Debug + 'static {
                pub a: T1,
                pub b: T2,
            }
            impl<T1: Debug + 'static, T2> ambient_element::ElementComponent for GenericComponent<T1, T2> where T2: Debug + 'static {
                fn render(self: Box<Self>, _: &mut ambient_element::Hooks) -> ambient_element::Element {
                    let Self { a, b } = *self;
                    {
                        Element::new()
                    }
                }
            }
            impl<T1: Debug + 'static, T2> GenericComponent<T1, T2> where T2: Debug + 'static {
                #[allow(clippy::too_many_arguments)]
                /// Create an `Element` from this component.
                pub fn el(a: T1, b: T2) -> ambient_element::Element {
                    use ambient_element::ElementComponentExt;
                    Self { a, b }.el()
                }
            }
        };

        assert_eq!(
            super::do_derive_element_component(TokenStream::new(), input).to_string(),
            output.to_string()
        );
    }
}