irc_bot/core/
handler.rs

1use super::BotCmdResult;
2use super::Error;
3use super::ErrorReaction;
4use super::MsgMetadata;
5use super::State;
6use regex::Captures;
7use std::panic::RefUnwindSafe;
8use std::panic::UnwindSafe;
9use yaml_rust::Yaml;
10
11pub trait ErrorHandler: Send + Sync + UnwindSafe + RefUnwindSafe + 'static {
12    /// Handles an error.
13    ///
14    /// The handler is given ownership of the error so that the handler can easily store the error
15    /// somewhere if desired.
16    fn run(&self, Error) -> ErrorReaction;
17}
18
19impl<T> ErrorHandler for T
20where
21    T: Fn(Error) -> ErrorReaction + Send + Sync + UnwindSafe + RefUnwindSafe + 'static,
22{
23    fn run(&self, err: Error) -> ErrorReaction {
24        self(err)
25    }
26}
27
28pub trait BotCmdHandler: Send + Sync + UnwindSafe + RefUnwindSafe {
29    fn run(&self, &State, &MsgMetadata, &Yaml) -> BotCmdResult;
30}
31
32impl<F, R> BotCmdHandler for F
33where
34    F: Fn(&State, &MsgMetadata, &Yaml) -> R + Send + Sync + UnwindSafe + RefUnwindSafe,
35    R: Into<BotCmdResult>,
36{
37    fn run(&self, state: &State, msg_md: &MsgMetadata, arg: &Yaml) -> BotCmdResult {
38        self(state, msg_md, arg).into()
39    }
40}
41
42pub trait TriggerHandler: Send + Sync + UnwindSafe + RefUnwindSafe {
43    fn run(&self, &State, &MsgMetadata, Captures) -> BotCmdResult;
44}
45
46impl<F, R> TriggerHandler for F
47where
48    F: Fn(&State, &MsgMetadata, Captures) -> R + Send + Sync + UnwindSafe + RefUnwindSafe,
49    R: Into<BotCmdResult>,
50{
51    fn run(&self, state: &State, msg_md: &MsgMetadata, args: Captures) -> BotCmdResult {
52        self(state, msg_md, args).into()
53    }
54}