signal_handler/handler/
builder.rs

1use core::{future::Future, pin::Pin};
2
3use crate::{
4    callback::{Callback, CallbackInfo, CallbackType, Callbacks},
5    handler::Handler,
6    register::Registers,
7};
8
9//
10#[derive(Debug, Clone, Default)]
11#[non_exhaustive]
12pub struct Builder {
13    pub callbacks: Callbacks,
14    pub registers: Registers,
15}
16
17impl Builder {
18    pub fn new() -> Self {
19        Self::default()
20    }
21
22    pub fn build(self) -> Handler {
23        Handler::from_builder(self)
24    }
25
26    //
27    pub fn initialized<F>(mut self, cb: F) -> Self
28    where
29        F: Fn(CallbackInfo) + Send + Sync + 'static,
30    {
31        let cb = Callback::with_sync(cb);
32        self.callbacks.insert(CallbackType::Initialized, cb);
33
34        self
35    }
36
37    pub fn initialized_async<F>(mut self, cb: F) -> Self
38    where
39        F: Fn(CallbackInfo) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>
40            + Send
41            + Sync
42            + 'static,
43    {
44        let cb = Callback::with_async(cb);
45        self.callbacks.insert(CallbackType::Initialized, cb);
46
47        self
48    }
49
50    //
51    #[cfg(not(windows))]
52    pub fn reload_config<F>(mut self, cb: F) -> Self
53    where
54        F: Fn(CallbackInfo) + Send + Sync + 'static,
55    {
56        let cb = Callback::with_sync(cb);
57        self.callbacks.insert(CallbackType::ReloadConfig, cb);
58
59        self.registers.insert_reload_config();
60
61        self
62    }
63
64    #[cfg(not(windows))]
65    pub fn reload_config_async<F>(mut self, cb: F) -> Self
66    where
67        F: Fn(CallbackInfo) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>
68            + Send
69            + Sync
70            + 'static,
71    {
72        let cb = Callback::with_async(cb);
73        self.callbacks.insert(CallbackType::ReloadConfig, cb);
74
75        self.registers.insert_reload_config();
76
77        self
78    }
79
80    //
81    pub fn wait_for_stop<F>(mut self, cb: F) -> Self
82    where
83        F: Fn(CallbackInfo) + Send + Sync + 'static,
84    {
85        let cb = Callback::with_sync(cb);
86        self.callbacks.insert(CallbackType::WaitForStop, cb);
87
88        self.registers.insert_wait_for_stop();
89
90        self
91    }
92
93    pub fn wait_for_stop_async<F>(mut self, cb: F) -> Self
94    where
95        F: Fn(CallbackInfo) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>
96            + Send
97            + Sync
98            + 'static,
99    {
100        let cb = Callback::with_async(cb);
101        self.callbacks.insert(CallbackType::WaitForStop, cb);
102
103        self.registers.insert_wait_for_stop();
104
105        self
106    }
107
108    //
109    #[cfg(not(windows))]
110    pub fn print_stats<F>(mut self, cb: F) -> Self
111    where
112        F: Fn(CallbackInfo) + Send + Sync + 'static,
113    {
114        let cb = Callback::with_sync(cb);
115        self.callbacks.insert(CallbackType::PrintStats, cb);
116
117        self.registers.insert_print_stats();
118
119        self
120    }
121
122    #[cfg(not(windows))]
123    pub fn print_stats_async<F>(mut self, cb: F) -> Self
124    where
125        F: Fn(CallbackInfo) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>
126            + Send
127            + Sync
128            + 'static,
129    {
130        let cb = Callback::with_async(cb);
131        self.callbacks.insert(CallbackType::PrintStats, cb);
132
133        self.registers.insert_print_stats();
134
135        self
136    }
137}