2019-01-18 19:08:40 +01:00
|
|
|
// Copyright (c) 2019 Rodolphe Bréard
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2019-01-06 15:41:30 +01:00
|
|
|
use crate::entry::{Entry, Event};
|
2019-01-06 16:37:07 +01:00
|
|
|
use std::str::FromStr;
|
2019-01-06 15:41:30 +01:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub enum MatchEvent {
|
|
|
|
Evt(Vec<Event>),
|
|
|
|
All,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2019-01-12 00:17:59 +01:00
|
|
|
pub enum Callback<T> {
|
2019-01-12 12:58:01 +01:00
|
|
|
NoCtx(fn(&Entry)),
|
|
|
|
Ctx(fn(&T, &Entry)),
|
|
|
|
CtxMut(fn(&mut T, &Entry)),
|
2019-01-12 00:17:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct EventHandler<T> {
|
2019-01-12 10:42:37 +01:00
|
|
|
pub(crate) event: MatchEvent,
|
2019-01-12 00:17:59 +01:00
|
|
|
callback: Callback<T>,
|
2019-01-06 15:41:30 +01:00
|
|
|
}
|
|
|
|
|
2019-01-12 00:17:59 +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 15:41:30 +01:00
|
|
|
}
|
2019-01-18 19:10:12 +01:00
|
|
|
_ => {
|
|
|
|
if let Ok(e) = Event::from_str(name) {
|
|
|
|
events.push(e);
|
|
|
|
}
|
|
|
|
}
|
2019-01-06 15:41:30 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 00:17:59 +01:00
|
|
|
MatchEvent::Evt(events)
|
|
|
|
}
|
2019-01-06 15:41:30 +01:00
|
|
|
|
2019-01-12 00:17:59 +01:00
|
|
|
impl<T: Clone + Default> EventHandler<T> {
|
|
|
|
pub fn new(event_str: &str, callback: Callback<T>) -> Self {
|
2019-01-06 15:41:30 +01:00
|
|
|
EventHandler {
|
2019-01-12 00:17:59 +01:00
|
|
|
event: get_events_from_string(event_str),
|
2019-01-06 15:41:30 +01:00
|
|
|
callback,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 17:07:00 +01:00
|
|
|
pub fn is_callable(&self, event: &Event) -> bool {
|
2019-01-06 15:41:30 +01:00
|
|
|
match &self.event {
|
|
|
|
MatchEvent::All => true,
|
|
|
|
MatchEvent::Evt(v) => v.contains(&event),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 00:17:59 +01:00
|
|
|
pub fn call(&self, entry: &Entry, context: &mut T) {
|
2019-01-12 12:58:01 +01:00
|
|
|
match self.callback {
|
2019-01-12 00:17:59 +01:00
|
|
|
Callback::NoCtx(f) => f(entry),
|
|
|
|
Callback::Ctx(f) => f(context, entry),
|
|
|
|
Callback::CtxMut(f) => f(context, entry),
|
2019-01-06 16:03:49 +01:00
|
|
|
};
|
2019-01-12 00:17:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::*;
|
|
|
|
|
|
|
|
#[test]
|
2019-01-12 12:58:01 +01:00
|
|
|
fn test_eventhandler_build_noctx() {
|
2019-01-12 23:43:02 +01:00
|
|
|
EventHandler::new("Any", Callback::NoCtx::<NoContext>(|_entry: &Entry| {}));
|
2019-01-12 12:58:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_eventhandler_build_ctx() {
|
|
|
|
EventHandler::new(
|
|
|
|
"Any",
|
|
|
|
Callback::Ctx(|_context: &NoContext, _entry: &Entry| {}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_eventhandler_build_ctxmut() {
|
2019-01-12 00:17:59 +01:00
|
|
|
EventHandler::new(
|
|
|
|
"Any",
|
2019-01-12 12:58:01 +01:00
|
|
|
Callback::CtxMut(|_context: &mut NoContext, _entry: &Entry| {}),
|
2019-01-12 00:17:59 +01:00
|
|
|
);
|
2019-01-06 15:41:30 +01:00
|
|
|
}
|
|
|
|
}
|