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:
parent
789a41b51e
commit
995c0c35c1
4 changed files with 46 additions and 22 deletions
|
@ -20,6 +20,6 @@ opensmtpd_derive = { path = "../opensmtpd-derive", version = "0.2" }
|
||||||
name = "echo"
|
name = "echo"
|
||||||
path = "examples/echo.rs"
|
path = "examples/echo.rs"
|
||||||
|
|
||||||
#[[example]]
|
[[example]]
|
||||||
#name = "counter"
|
name = "counter"
|
||||||
#path = "examples/session_event_counter.rs"
|
path = "examples/session_event_counter.rs"
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
use opensmtpd::entry::Entry;
|
use opensmtpd::entry::Entry;
|
||||||
use opensmtpd::{report, simple_filter};
|
use opensmtpd::{report, simple_filter};
|
||||||
|
|
||||||
|
#[derive(Clone, Default)]
|
||||||
|
struct MyContext {
|
||||||
|
nb: usize,
|
||||||
|
}
|
||||||
|
|
||||||
#[report(v1, smtp_in, match(all))]
|
#[report(v1, smtp_in, match(all))]
|
||||||
fn echo_handler(entry: &Entry) -> Result<(), String> {
|
fn echo_handler(entry: &Entry) -> Result<(), String> {
|
||||||
log::info!("TEST ENTRY: {:?}", entry);
|
log::info!("TEST ENTRY: {:?}", entry);
|
||||||
|
@ -14,5 +19,5 @@ fn test(entry: &Entry) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
simple_filter!(echo_handler, test);
|
simple_filter!(MyContext, [echo_handler, test]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use log;
|
||||||
|
use opensmtpd::entry::Entry;
|
||||||
use opensmtpd::{report, simple_filter};
|
use opensmtpd::{report, simple_filter};
|
||||||
|
|
||||||
#[derive(Clone, Default)]
|
#[derive(Clone, Default)]
|
||||||
|
@ -5,12 +7,13 @@ struct MyContext {
|
||||||
nb: usize,
|
nb: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[report(Any)]
|
#[report(v1, smtp_in, match(all))]
|
||||||
fn on_report(ctx: &mut MyContext, entry: &Entry) {
|
fn on_report(ctx: &mut MyContext, entry: &Entry) {
|
||||||
ctx.nb += 1;
|
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() {
|
fn main() {
|
||||||
simple_filter!(vec![on_report]);
|
simple_filter!(MyContext, [on_report]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,22 +26,35 @@ pub use opensmtpd_derive::report;
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! simple_filter {
|
macro_rules! simple_filter {
|
||||||
($( $x:expr ),*) => {
|
($handlers: expr) => {
|
||||||
opensmtpd::simple_filter_log_level!(log::Level::Info $(,$x)*);
|
opensmtpd::simple_filter!(
|
||||||
|
log::Level::Info,
|
||||||
|
opensmtpd::NoContext,
|
||||||
|
opensmtpd::NoContext,
|
||||||
|
$handlers
|
||||||
|
);
|
||||||
};
|
};
|
||||||
}
|
($filter_ctx: ty, $handlers: expr) => {
|
||||||
|
opensmtpd::simple_filter!(
|
||||||
#[macro_export]
|
log::Level::Info,
|
||||||
macro_rules! simple_filter_log_level {
|
opensmtpd::NoContext,
|
||||||
($log_level: expr, $( $x:expr ),*) => {
|
$filter_ctx,
|
||||||
let mut handlers = Vec::new();
|
$handlers
|
||||||
$(
|
);
|
||||||
handlers.push(($x)());
|
};
|
||||||
)*;
|
($sesion_ctx: ty, $filter_ctx: ty, $handlers: expr) => {
|
||||||
let _ = opensmtpd::SmtpdLogger::new()
|
opensmtpd::simple_filter!(log::Level::Info, $sesion_ctx, $filter_ctx, $handlers);
|
||||||
.set_level($log_level)
|
};
|
||||||
.init();
|
($log_level: path, $sesion_ctx: ty, $filter_ctx: ty, $handlers: expr) => {
|
||||||
opensmtpd::Filter::<opensmtpd::input::StdIn, opensmtpd::output::StdOut>::default().set_handlers(&handlers).register_events().run();
|
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>
|
pub struct Filter<I, O>
|
||||||
where
|
where
|
||||||
I: crate::input::FilterInput + Default,
|
I: crate::input::FilterInput + Default,
|
||||||
|
|
Reference in a new issue