diff --git a/examples/dummy.rs b/examples/dummy.rs index 5cd9c1f..c361d8e 100644 --- a/examples/dummy.rs +++ b/examples/dummy.rs @@ -1,14 +1,21 @@ use log::debug; use env_logger::{Builder, Env}; -use opensmtpd::{Entry, EventHandler, MatchEvent, SmtpIn}; +use opensmtpd::{handlers, Entry, EventHandler, MatchEvent, SmtpIn}; fn on_event(entry: &Entry) -> bool { debug!("Event received: {:?}", entry); true } +// This function should be replaced by a procedural macro on +// the `on_event` function. +fn on_event_builder() -> EventHandler { + EventHandler::new(MatchEvent::All, on_event) +} + fn main() { Builder::from_env(Env::default().default_filter_or("debug")).init(); - let h = vec![EventHandler::new(MatchEvent::All, on_event)]; - SmtpIn::new().event_handlers(h).run(); + SmtpIn::new() + .event_handlers(handlers!(on_event_builder)) + .run(); } diff --git a/src/lib.rs b/src/lib.rs index 0915f3c..9e9c0ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,19 @@ pub use crate::entry::{Entry, Event}; pub use crate::errors::Error; pub use crate::event_handlers::{EventHandler, MatchEvent}; +#[macro_export] +macro_rules! handlers { + ( $( $x:expr ),* ) => { + { + let mut temp_vec = Vec::new(); + $( + temp_vec.push(($x)()); + )* + temp_vec + } + }; +} + struct SessionHandler { entry_rx: mpsc::Receiver, event_handlers: Vec, @@ -19,7 +32,10 @@ struct SessionHandler { impl SessionHandler { fn new(entry_rx: mpsc::Receiver, handlers_rx: mpsc::Receiver) -> Self { - debug!("New thread for session {}", thread::current().name().unwrap()); + debug!( + "New thread for session {}", + thread::current().name().unwrap() + ); let mut event_handlers = Vec::new(); for h in handlers_rx.iter() { debug!("Event handler registered");