Add macros for logging.
This commit is contained in:
12
libclide-macros/Cargo.toml
Normal file
12
libclide-macros/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "libclide-macros"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
syn = { version = "2", features = ["full"] }
|
||||
quote = "1"
|
||||
proc-macro2 = "1"
|
||||
27
libclide-macros/src/lib.rs
Normal file
27
libclide-macros/src/lib.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{ItemStruct, parse_macro_input};
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn log_id(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(item as ItemStruct);
|
||||
|
||||
let struct_name = &input.ident;
|
||||
let generics = &input.generics;
|
||||
|
||||
// This is the important part
|
||||
let (impl_generics, type_generics, where_clause) = generics.split_for_impl();
|
||||
|
||||
let struct_name_str = struct_name.to_string();
|
||||
|
||||
let expanded = quote! {
|
||||
#input
|
||||
|
||||
impl #impl_generics #struct_name #type_generics #where_clause {
|
||||
#[allow(unused)]
|
||||
pub const ID: &'static str = #struct_name_str;
|
||||
}
|
||||
};
|
||||
|
||||
TokenStream::from(expanded)
|
||||
}
|
||||
Reference in New Issue
Block a user