leon_macros/
lib.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
use leon::{Item, Template};
use quote::quote;
use syn::{parse_macro_input, LitStr};

#[proc_macro]
pub fn template(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = parse_macro_input!(input as LitStr).value();

    #[allow(clippy::unnecessary_to_owned)]
    let items = Template::parse(&input)
        .unwrap()
        .items
        .into_owned()
        .into_iter()
        .map(|item| match item {
            Item::Text(text) => quote! {
                ::leon::Item::Text(#text)
            },
            Item::Key(key) => quote! {
                ::leon::Item::Key(#key)
            },
        });

    quote! {
        ::leon::Template::new(
            {
                const ITEMS: &'static [::leon::Item<'static>] = &[
                    #(#items),*
                ];
                ITEMS
            },
            ::core::option::Option::None,
        )
    }
    .into()
}