carbide_derive/
lib.rs

1extern crate proc_macro;
2
3extern crate proc_macro2;
4#[macro_use] extern crate quote;
5extern crate syn;
6
7mod common;
8mod style;
9mod utils;
10mod widget;
11
12use proc_macro::TokenStream;
13
14// The implementation for the `WidgetCommon` trait derivation (aka `carbide_core::widget::Common`).
15#[proc_macro_derive(WidgetCommon, attributes(carbide, common_builder))]
16pub fn widget_common(input: TokenStream) -> TokenStream {
17    impl_derive(input, common::impl_widget_common)
18}
19
20// The implementation for the `WidgetCommon_` trait derivation (aka `carbide_core::widget::Common`).
21//
22// Note that this is identical to the `WidgetCommon` trait, but only for use within the carbide
23// crate itself.
24#[proc_macro_derive(WidgetCommon_, attributes(carbide, common_builder))]
25pub fn widget_common_(input: TokenStream) -> TokenStream {
26    impl_derive(input, common::impl_widget_common_)
27}
28
29// The implementation for the `WidgetStyle` trait derivation (aka `carbide_core::widget::Style`).
30#[proc_macro_derive(WidgetStyle, attributes(carbide, default))]
31pub fn widget_style(input: TokenStream) -> TokenStream {
32    impl_derive(input, style::impl_widget_style)
33}
34
35// The implementation for the `WidgetStyle_` trait derivation (aka `carbide_core::widget::Style`).
36//
37// Note that this is identical to the `WidgetStyle_` trait, but only for use within the carbide
38// crate itself.
39#[proc_macro_derive(WidgetStyle_, attributes(carbide, default))]
40pub fn widget_style_(input: TokenStream) -> TokenStream {
41    impl_derive(input, style::impl_widget_style_)
42}
43
44
45
46#[proc_macro_derive(Widget, attributes(global_state, state, state_sync, event))]
47pub fn widget(input: TokenStream) -> TokenStream {
48    impl_derive(input, widget::impl_widget)
49}
50
51
52
53// Use the given function to generate a TokenStream for the derive implementation.
54fn impl_derive(
55    input: TokenStream,
56    generate_derive: fn(&syn::DeriveInput) -> proc_macro2::TokenStream,
57) -> TokenStream
58{
59    // Parse the input TokenStream representation.
60    let ast = syn::parse(input).unwrap();
61    
62    // Build the implementation.
63    let gen = generate_derive(&ast);
64    // Return the generated impl.
65    gen.into()
66}