blob: 46ead36785863492597380c3539eb9793fadfde4 (
plain) (
blame)
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
53
54
55
56
57
58
59
60
|
use mingling_core::{
Program, ProgramCollect, config, hook::ProgramHook, setup::ProgramSetup, this,
};
use crate::res::ResConfirm;
/// Confirm setup for managing confirmation state
///
/// This Setup manages the confirmation flag within the program's resource
/// store. It registers a [`ResConfirm`] resource and sets up a hook that
/// checks the user's confirmation mode during program execution.
///
/// # Usage
///
/// This Setup can be registered using the
/// [`Program`](https://docs.rs/mingling/latest/mingling/struct.Program.html)
/// `with_setup` method, for example:
///
/// ```rust
/// # use mingling::MockProgramCollect as ThisProgram;
/// use mingling::Program;
/// use mingling::setup::ConfirmSetup;
///
/// let mut program = Program::<ThisProgram>::new();
/// program.with_setup(ConfirmSetup);
/// ```
///
/// # Behavior
///
/// - Registers a [`ResConfirm`] resource that tracks confirmation state.
/// - At the beginning of command execution, checks whether the user's
/// confirmation mode is set to `Skip`.
/// - If confirmation is skipped, the [`ResConfirm`] resource is updated
/// to record the confirmed state.
///
/// # Notes
///
/// - This Setup applies uniformly to all subcommands of the entire program.
/// - The confirmation state is determined by the global `config` setting;
/// it does not support per-command overrides.
pub struct ConfirmSetup;
impl<C> ProgramSetup<C> for ConfirmSetup
where
C: ProgramCollect<Enum = C> + 'static,
{
fn setup(self, program: &mut Program<C>) {
program.with_resource(ResConfirm::new());
program.with_hook(ProgramHook::empty().on_pre_dispatch::<_, ()>(|_| {
let p = this::<C>();
let confirmed = p.user_context.confirmation == config::ConfirmationMode::Skip;
if confirmed {
p.modify_res(|c: &mut ResConfirm| {
c.set_confirmed();
});
}
}));
}
}
|