Implement the register_events method

This commit is contained in:
Rodolphe Breard 2019-09-17 19:38:41 +02:00
parent 6b532242ef
commit 24c0253f88
2 changed files with 52 additions and 6 deletions

View file

@ -29,9 +29,9 @@ type Callback = fn(&mut dyn FilterOutput, &Entry) -> Result<(), String>;
#[derive(Clone)]
pub struct Handler {
version: Version,
kind: Kind,
subsystem: Subsystem,
events: HashSet<Event>,
pub(crate) kind: Kind,
pub(crate) subsystem: Subsystem,
pub(crate) events: HashSet<Event>,
action: Callback,
}

View file

@ -14,7 +14,9 @@ pub mod entry;
pub mod input;
pub mod output;
use crate::entry::{Kind, Subsystem};
use log;
use std::collections::HashSet;
use std::default::Default;
pub use crate::errors::Error;
@ -43,6 +45,32 @@ macro_rules! simple_filter_log_level {
};
}
macro_rules! fatal_error {
($error: ident) => {
log::error!("Error: {}", $error);
std::process::exit(1);
};
}
macro_rules! insert_events {
($handler: ident, $set: ident) => {{
for e in $handler.events.iter() {
$set.insert(e);
}
}};
}
macro_rules! register_events {
($output: expr, $set: ident, $kind: expr, $subsystem: expr) => {
for e in $set.iter() {
let msg = format!("register|{}|{}|{}", $kind, $subsystem, e.to_string());
if let Err(e) = $output.send(&msg) {
fatal_error!(e);
};
}
};
}
pub struct Filter<I, O>
where
I: crate::input::FilterInput + Default,
@ -78,7 +106,26 @@ where
}
pub fn register_events(&mut self) -> &mut Self {
// TODO: use self.output to register events
let mut report_smtp_in = HashSet::new();
let mut report_smtp_out = HashSet::new();
let mut filter_smtp_in = HashSet::new();
let mut filter_smtp_out = HashSet::new();
for h in self.handlers.iter() {
match h.kind {
Kind::Report => match h.subsystem {
Subsystem::SmtpIn => insert_events!(h, report_smtp_in),
Subsystem::SmtpOut => insert_events!(h, report_smtp_out),
},
Kind::Filter => match h.subsystem {
Subsystem::SmtpIn => insert_events!(h, filter_smtp_in),
Subsystem::SmtpOut => insert_events!(h, filter_smtp_out),
},
};
}
register_events!(self.output, report_smtp_in, "report", "smtp-in");
register_events!(self.output, report_smtp_out, "report", "smtp-out");
register_events!(self.output, filter_smtp_in, "filter", "smtp-in");
register_events!(self.output, filter_smtp_out, "filter", "smtp-out");
self
}
@ -97,8 +144,7 @@ where
}
}
Err(e) => {
log::error!("Error: {}", e);
std::process::exit(1);
fatal_error!(e);
}
};
}