mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-22 13:38:43 +00:00

`leon_macros::template!` can parse template at compile-time. It accepts a utf-8 string literal and uses `leon` internally to parse it, then generate code that evaluates to `Template<'static>`. - Exclude fuzz from crate leon when publishing - Impl fn-like proc-macro `leon_macros::template!` - Add dep `leon-macros` to binstalk - Use `leon_macros::template!` in `binstalk::fetchers::gh_crate_meta::hosting` - Add doc for `leon-macros` in `leon` - Improve `std::fmt::Display` impl for `leon::ParseError` - Fixed broken infra link in leon Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
36 lines
920 B
Rust
36 lines
920 B
Rust
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()
|
|
}
|