1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
macro_rules! handler {
    (
        #[doc = $doc:literal]
        $handlers:ident,
        $name:ident,
        $context:path,
        $run_handlers:ident,
        $($will_handle:ident,)?
    ) => {
        #[doc = $doc]
        pub fn $name(
            &mut self,
            handler: impl FnMut(& $context) + Send + Sync + 'static,
        ) {
            self.$handlers.push(Mutex::new(Box::new(handler)))
        }

        $(fn $will_handle(&self) -> bool {
            !self.$handlers.is_empty()
        })?

        fn $run_handlers(&self, context: & $context) {
            for handler in &self.$handlers {
                if let Ok(mut handler) = handler.lock() {
                    (&mut *handler)(context)
                } else {
                    eprintln!(
                        "[tbot] Cannot run a handler since it previously \
                         panicked. You should analyze the cause and prevent it."
                    );
                }
            }
        }
    };
}