Improve the simple_filter macro

The simple_filter macro must accept the two different levels of
contexts. In the same way, it should also accept the log level, and
therefore replace the now removed simple_filter_log_level macro.
This commit is contained in:
Rodolphe Breard 2019-09-18 14:52:21 +02:00
parent 789a41b51e
commit 995c0c35c1
4 changed files with 46 additions and 22 deletions

View file

@ -20,6 +20,6 @@ opensmtpd_derive = { path = "../opensmtpd-derive", version = "0.2" }
name = "echo"
path = "examples/echo.rs"
#[[example]]
#name = "counter"
#path = "examples/session_event_counter.rs"
[[example]]
name = "counter"
path = "examples/session_event_counter.rs"

View file

@ -1,6 +1,11 @@
use opensmtpd::entry::Entry;
use opensmtpd::{report, simple_filter};
#[derive(Clone, Default)]
struct MyContext {
nb: usize,
}
#[report(v1, smtp_in, match(all))]
fn echo_handler(entry: &Entry) -> Result<(), String> {
log::info!("TEST ENTRY: {:?}", entry);
@ -14,5 +19,5 @@ fn test(entry: &Entry) {
}
fn main() {
simple_filter!(echo_handler, test);
simple_filter!(MyContext, [echo_handler, test]);
}

View file

@ -1,3 +1,5 @@
use log;
use opensmtpd::entry::Entry;
use opensmtpd::{report, simple_filter};
#[derive(Clone, Default)]
@ -5,12 +7,13 @@ struct MyContext {
nb: usize,
}
#[report(Any)]
#[report(v1, smtp_in, match(all))]
fn on_report(ctx: &mut MyContext, entry: &Entry) {
ctx.nb += 1;
info!("Event received: {}, {}", entry.get_session_id(), ctx.nb);
log::info!("Event received: {}, {}", entry.get_session_id(), ctx.nb);
Ok(())
}
fn main() {
simple_filter!(vec![on_report]);
simple_filter!(MyContext, [on_report]);
}

View file

@ -26,22 +26,35 @@ pub use opensmtpd_derive::report;
#[macro_export]
macro_rules! simple_filter {
($( $x:expr ),*) => {
opensmtpd::simple_filter_log_level!(log::Level::Info $(,$x)*);
($handlers: expr) => {
opensmtpd::simple_filter!(
log::Level::Info,
opensmtpd::NoContext,
opensmtpd::NoContext,
$handlers
);
};
}
#[macro_export]
macro_rules! simple_filter_log_level {
($log_level: expr, $( $x:expr ),*) => {
let mut handlers = Vec::new();
$(
handlers.push(($x)());
)*;
let _ = opensmtpd::SmtpdLogger::new()
.set_level($log_level)
.init();
opensmtpd::Filter::<opensmtpd::input::StdIn, opensmtpd::output::StdOut>::default().set_handlers(&handlers).register_events().run();
($filter_ctx: ty, $handlers: expr) => {
opensmtpd::simple_filter!(
log::Level::Info,
opensmtpd::NoContext,
$filter_ctx,
$handlers
);
};
($sesion_ctx: ty, $filter_ctx: ty, $handlers: expr) => {
opensmtpd::simple_filter!(log::Level::Info, $sesion_ctx, $filter_ctx, $handlers);
};
($log_level: path, $sesion_ctx: ty, $filter_ctx: ty, $handlers: expr) => {
let handlers = ($handlers)
.iter()
.map(|f| f())
.collect::<Vec<opensmtpd::Handler>>();
let _ = opensmtpd::SmtpdLogger::new().set_level($log_level).init();
opensmtpd::Filter::<opensmtpd::input::StdIn, opensmtpd::output::StdOut>::default()
.set_handlers(handlers.as_slice())
.register_events()
.run();
};
}
@ -71,6 +84,9 @@ macro_rules! register_events {
};
}
#[derive(Default)]
pub struct NoContext;
pub struct Filter<I, O>
where
I: crate::input::FilterInput + Default,