Skip to main content

console_display_macros/
lib.rs

1use proc_macro::TokenStream;
2use quote::quote;
3use syn::{
4    DeriveInput,
5    GenericParam,
6    Generics,
7    parse_macro_input,
8    parse_quote,
9};
10
11/// Derives `StaticWidget` for a struct.
12/// Sets the width and height in characters to the dimensions of the child element.
13#[proc_macro_derive(StaticWidget)]
14pub fn derive_static_widget(input: TokenStream) -> TokenStream {
15    let input = parse_macro_input!(input as DeriveInput);
16
17    let name = input.ident;
18    let generics = add_static_widget_bound_to_t(input.generics);
19    let (impl_generics, ty_generics, where_clause) =
20        generics.split_for_impl();
21
22    TokenStream::from(quote!(
23    impl #impl_generics const StaticWidget for #name #ty_generics #where_clause {
24        const WIDTH_CHARACTERS: usize = <T as StaticWidget>::WIDTH_CHARACTERS;
25        const HEIGHT_CHARACTERS: usize = <T as StaticWidget>::HEIGHT_CHARACTERS;
26    }))
27}
28
29/// Derives `DynamicWidget` for a struct.
30/// Sets the width and height in characters to the dimensions of the child element.
31#[proc_macro_derive(DynamicWidget)]
32pub fn derive_dynamic_widget(input: TokenStream) -> TokenStream {
33    let input = parse_macro_input!(input as DeriveInput);
34
35    let name = input.ident;
36    let generics = add_static_widget_bound_to_t(input.generics);
37    let (impl_generics, ty_generics, where_clause) =
38        generics.split_for_impl();
39
40    // TODO: Check if this can be const
41    TokenStream::from(quote!(
42    impl #impl_generics DynamicWidget for #name #ty_generics #where_clause {
43        fn width_characters(&self) -> usize {
44            self.child.width_characters()
45        }
46        fn height_characters(&self) -> usize {
47            self.child.height_characters()
48        }
49        fn string_data(&self) -> StringData {
50            self.child.string_data()
51        }
52    }))
53}
54
55/// Derives `SingleWidget` for a struct.
56/// Implements getter (+ mut) for the child element assuming the child is of type `T`.
57#[proc_macro_derive(SingleWidget)]
58pub fn derive_single_widget(input: TokenStream) -> TokenStream {
59    let input = parse_macro_input!(input as DeriveInput);
60
61    let name = input.ident;
62    let generics = input.generics;
63    let (impl_generics, ty_generics, where_clause) =
64        generics.split_for_impl();
65
66    TokenStream::from(quote!(
67    impl #impl_generics const SingleWidget<T> for #name #ty_generics #where_clause {
68        type Borrowed<'a>
69            = &'a T
70        where
71            T: 'a,
72            Self: 'a;
73
74        type BorrowedMut<'a>
75            = &'a mut T
76        where
77            T: 'a,
78            Self: 'a;
79
80        fn child(&self) -> Self::Borrowed<'_> {
81            &self.child
82        }
83
84        fn child_mut(&mut self) -> Self::BorrowedMut<'_> {
85            &mut self.child
86        }
87    }))
88}
89
90/// Derives `TwoWidget` for a struct.
91/// Implements getter (+ mut) for the child elements assuming the children are of type `S` and `T` respectively.
92#[proc_macro_derive(TwoWidget)]
93pub fn derive_two_widget(input: TokenStream) -> TokenStream {
94    let input = parse_macro_input!(input as DeriveInput);
95
96    let name = input.ident;
97    let generics = input.generics;
98    let (impl_generics, ty_generics, where_clause) =
99        generics.split_for_impl();
100
101    TokenStream::from(quote!(
102    impl #impl_generics const TwoWidget<S, T> for #name #ty_generics #where_clause {
103        fn children(&self) -> (&S, &T) {
104            (&self.children.0, &self.children.1)
105        }
106        fn children_mut(&mut self) -> (&mut S, &mut T) {
107            (&mut self.children.0, &mut self.children.1)
108        }
109    }))
110}
111
112/// Adds a bound that restricts generics such that: `T: StaticWidget`.
113fn add_static_widget_bound_to_t(mut generics: Generics) -> Generics {
114    for param in &mut generics.params {
115        if let GenericParam::Type(type_param) = param &&
116            type_param.ident == "T"
117        {
118            type_param.bounds.push(parse_quote!(StaticWidget));
119        }
120    }
121    generics
122}