From ea710408d418c9a73d39f0b7ddb6eef7f55370b4 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sun, 6 Jan 2019 17:07:00 +0100 Subject: [PATCH] Cleanup the code --- opensmtpd-derive/src/lib.rs | 2 +- opensmtpd/src/event_handlers.rs | 15 ++++++--------- opensmtpd/src/lib.rs | 9 +++++---- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/opensmtpd-derive/src/lib.rs b/opensmtpd-derive/src/lib.rs index 112e7b2..09a81d1 100644 --- a/opensmtpd-derive/src/lib.rs +++ b/opensmtpd-derive/src/lib.rs @@ -14,7 +14,7 @@ pub fn event(attr: TokenStream, input: TokenStream) -> TokenStream { let fn_output = &item.decl.output; let output = quote! { fn #fn_name() -> opensmtpd::EventHandler { - opensmtpd::EventHandler::new(#attr.to_string(), |#fn_params| #fn_output #fn_body) + opensmtpd::EventHandler::new(#attr, |#fn_params| #fn_output #fn_body) } }; output.into() diff --git a/opensmtpd/src/event_handlers.rs b/opensmtpd/src/event_handlers.rs index d42a9f9..c573eb7 100644 --- a/opensmtpd/src/event_handlers.rs +++ b/opensmtpd/src/event_handlers.rs @@ -15,32 +15,29 @@ pub struct EventHandler { } impl EventHandler { - fn get_events_from_string(event_str: &String) -> MatchEvent { + fn get_events_from_string(event_str: &str) -> MatchEvent { let mut events = Vec::new(); for name in event_str.split(" , ") { match name { "Any" | "All" => { return MatchEvent::All; } - _ => match Event::from_str(name) { - Ok(e) => { - events.push(e); - } - Err(_) => {} + _ => if let Ok(e) = Event::from_str(name) { + events.push(e); }, } } MatchEvent::Evt(events) } - pub fn new(event_str: String, callback: (fn(&Entry) -> Response)) -> Self { + pub fn new(event_str: &str, callback: (fn(&Entry) -> Response)) -> Self { EventHandler { - event: EventHandler::get_events_from_string(&event_str), + event: EventHandler::get_events_from_string(event_str), callback, } } - pub fn is_callable(&self, event: Event) -> bool { + pub fn is_callable(&self, event: &Event) -> bool { match &self.event { MatchEvent::All => true, MatchEvent::Evt(v) => v.contains(&event), diff --git a/opensmtpd/src/lib.rs b/opensmtpd/src/lib.rs index 0babb7d..99600a8 100644 --- a/opensmtpd/src/lib.rs +++ b/opensmtpd/src/lib.rs @@ -37,7 +37,7 @@ struct SessionHandler { } impl SessionHandler { - fn new(entry_rx: mpsc::Receiver, handlers_rx: mpsc::Receiver) -> Self { + fn new(entry_rx: mpsc::Receiver, handlers_rx: &mpsc::Receiver) -> Self { debug!( "New thread for session {}", thread::current().name().unwrap() @@ -56,7 +56,7 @@ impl SessionHandler { fn read_entries(&self) { for e in self.entry_rx.iter() { for h in self.event_handlers.iter() { - if h.is_callable(e.event.clone()) { + if h.is_callable(&e.event) { h.call(&e); } } @@ -64,6 +64,7 @@ impl SessionHandler { } } +#[derive(Default)] pub struct SmtpIn { sessions: HashMap, thread::JoinHandle<()>)>, event_handlers: Vec, @@ -94,7 +95,7 @@ impl SmtpIn { let (entry_tx, entry_rx) = mpsc::channel(); let name = entry.session_id.to_string(); let handle = thread::Builder::new().name(name).spawn(move || { - SessionHandler::new(entry_rx, handlers_rx).read_entries(); + SessionHandler::new(entry_rx, &handlers_rx).read_entries(); })?; for h in self.event_handlers.iter() { handlers_tx.send(h.clone())?; @@ -133,7 +134,7 @@ impl SmtpIn { } pub fn event_handlers(&mut self, handlers: Vec) -> &mut Self { - self.event_handlers = handlers.clone(); + self.event_handlers = handlers.to_owned(); self }