Testing procedural macros
This commit is contained in:
2
aweblib_macros/.gitignore
vendored
Normal file
2
aweblib_macros/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
Cargo.lock
|
||||
/target
|
||||
11
aweblib_macros/Cargo.toml
Normal file
11
aweblib_macros/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "aweblib_macros"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
syn = "2.0.90"
|
||||
quote = "1.0.37"
|
||||
28
aweblib_macros/src/lib.rs
Normal file
28
aweblib_macros/src/lib.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{parse_macro_input, Data, DataStruct};
|
||||
|
||||
#[proc_macro_derive(Getter)]
|
||||
pub fn getter(item: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(item as syn::DeriveInput);
|
||||
let ident = input.ident;
|
||||
let mut quote = quote! {
|
||||
println!("Test");
|
||||
};
|
||||
if let Data::Struct(DataStruct { fields, .. }) = input.data {
|
||||
for field in fields {
|
||||
let name = field.ident.unwrap();
|
||||
quote.extend(quote! {
|
||||
println!("{}",stringify!(#name));
|
||||
})
|
||||
}
|
||||
}
|
||||
quote!(
|
||||
impl #ident {
|
||||
fn get() {
|
||||
#quote
|
||||
}
|
||||
}
|
||||
)
|
||||
.into()
|
||||
}
|
||||
Reference in New Issue
Block a user