Allow the use of custom context

The main goal of events/reports is to update a context object, which
will be used in filters to generate a response. It is now possible to
use any object implementing both Clone and Default as a context object.
It is also possible to define no context at all.
This commit is contained in:
Rodolphe Breard 2019-01-12 23:43:02 +01:00
parent dd7f4d1a86
commit 4b1f99db7e
6 changed files with 75 additions and 18 deletions

View file

@ -4,21 +4,61 @@ use proc_macro::TokenStream;
use syn::{parse_macro_input, ItemFn};
use quote::quote;
fn get_type(
params: &syn::punctuated::Punctuated<syn::FnArg, syn::token::Comma>,
) -> Result<(Box<syn::Type>, syn::Type), ()> {
match params.iter().count() {
1 => {
let ctx = Box::new(syn::Type::Verbatim(syn::TypeVerbatim {
tts: quote! {
opensmtpd::NoContext
},
}));
let cb = syn::Type::Verbatim(syn::TypeVerbatim {
tts: quote!{ opensmtpd::Callback::NoCtx },
});
Ok((ctx, cb))
}
2 => match params.iter().next().unwrap() {
syn::FnArg::Captured(ref a) => match &a.ty {
syn::Type::Reference(r) => {
let cb = match r.mutability {
Some(_) => syn::Type::Verbatim(syn::TypeVerbatim {
tts: quote!{ opensmtpd::Callback::CtxMut },
}),
None => syn::Type::Verbatim(syn::TypeVerbatim {
tts: quote!{ opensmtpd::Callback::Ctx },
}),
};
Ok((r.elem.clone(), cb))
}
_ => Err(()),
},
_ => Err(()),
},
_ => Err(()),
}
}
#[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 (ctx_type, callback_type) = match get_type(fn_params) {
Ok(t) => t,
Err(_) => {
panic!();
}
};
let fn_body = &item.block;
let fn_output = &item.decl.output;
let output = quote! {
// TODO: set the correct EventHandler type
fn #fn_name() -> opensmtpd::EventHandler<opensmtpd::NoContext> {
// TODO: set the correct Callback type
fn #fn_name() -> opensmtpd::EventHandler<#ctx_type> {
opensmtpd::EventHandler::new(
#attr,
opensmtpd::Callback::CtxMut(|#fn_params| #fn_output #fn_body)
#callback_type(|#fn_params| #fn_output #fn_body)
)
}
};