This repository has been archived on 2023-09-20. You can view files and clone it, but cannot push or open issues or pull requests.
rust-opensmtpd/opensmtpd/src/event_handlers.rs

53 lines
1.2 KiB
Rust
Raw Normal View History

use crate::entry::{Entry, Event};
use crate::Response;
use std::str::FromStr;
#[derive(Clone, Debug, PartialEq)]
pub enum MatchEvent {
Evt(Vec<Event>),
All,
}
#[derive(Clone)]
pub struct EventHandler {
event: MatchEvent,
callback: (fn(&Entry) -> Response),
}
impl EventHandler {
2019-01-06 17:07:00 +01:00
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;
}
2019-01-06 17:07:00 +01:00
_ => if let Ok(e) = Event::from_str(name) {
events.push(e);
},
}
}
MatchEvent::Evt(events)
}
2019-01-06 17:07:00 +01:00
pub fn new(event_str: &str, callback: (fn(&Entry) -> Response)) -> Self {
EventHandler {
2019-01-06 17:07:00 +01:00
event: EventHandler::get_events_from_string(event_str),
callback,
}
}
2019-01-06 17:07:00 +01:00
pub fn is_callable(&self, event: &Event) -> bool {
match &self.event {
MatchEvent::All => true,
MatchEvent::Evt(v) => v.contains(&event),
}
}
pub fn call(&self, entry: &Entry) {
match (self.callback)(entry) {
Response::None => {}
};
}
}