This repository has been archived on 2023-09-20. You can view files and clone it, but cannot push or open issues or pull requests.
rust-opensmtpd/opensmtpd-derive/src/lib.rs

22 lines
616 B
Rust
Raw Normal View History

extern crate proc_macro;
use proc_macro::TokenStream;
use syn::{parse_macro_input, ItemFn};
use quote::quote;
#[proc_macro_attribute]
pub fn event(attr: TokenStream, input: TokenStream) -> TokenStream {
let attr = attr.to_string();
let item = parse_macro_input!(input as ItemFn);
let fn_name = &item.ident;
let fn_params = &item.decl.inputs;
let fn_body = &item.block;
let fn_output = &item.decl.output;
let output = quote! {
fn #fn_name() -> EventHandler {
EventHandler::new(#attr.to_string(), |#fn_params| #fn_output #fn_body)
}
};
output.into()
}