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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use crate::flag::Flag;
/// `FlagTrigger` is a functional wrapper that associates flag states with executable actions.
///
/// It uses Generics (`F: Flag`) to remain compatible with any type that implements the
/// `Flag` trait, making it a powerful tool for event-driven architectures.
pub struct FlagTrigger<F: Flag> {
/// The underlying flag-storage instance.
flags: F,
}
impl<F: Flag> FlagTrigger<F> {
/// Injects a flag-capable instance into a new Trigger coordinator.
pub fn new(flags: F) -> Self {
Self { flags }
}
/// Conditional Execution: Runs the provided `action` only if the specified `flag` is active.
///
/// # Example
/// `trigger.trigger_if(FLAGS_ADMIN, || println!("Access Granted"));`
pub fn trigger_if(&self, flag: F::Value, action: impl FnOnce()) {
if self.flags.check_flag(flag) {
action();
}
}
/// Negative Conditional Execution: Runs the provided `action` only if the `flag` is NOT active.
///
/// Useful for default behaviors or fallback logic.
pub fn trigger_unless(&self, flag: F::Value, action: impl FnOnce()) {
if !self.flags.check_flag(flag) {
action();
}
}
/// Atomic-style Operation: Flips the state of the flag and then immediately executes the action.
///
/// This is common in UI toggles or hardware state-switching.
pub fn toggle_and_trigger(&mut self, flag: F::Value, action: impl FnOnce()) {
self.flags.toggle_flag(flag);
action();
}
/// Cleanup Operation: Force-clears the specified flag and then executes the action.
///
/// Useful for "Shutdown" or "De-authorization" sequences.
pub fn clear_and_trigger(&mut self, flag: F::Value, action: impl FnOnce()) {
self.flags.clear_flag(flag);
action();
}
}