flake_edit/tui/components/confirm/
model.rs

1use crossterm::event::{KeyCode, KeyEvent};
2
3/// Actions for confirmation dialog
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum ConfirmAction {
6    Apply,
7    Back,
8    Exit,
9    None,
10}
11
12impl ConfirmAction {
13    pub fn from_key(key: KeyEvent) -> Self {
14        match key.code {
15            KeyCode::Char('y') | KeyCode::Char('Y') | KeyCode::Enter => ConfirmAction::Apply,
16            KeyCode::Char('b') | KeyCode::Char('B') | KeyCode::Esc => ConfirmAction::Back,
17            KeyCode::Char('n') | KeyCode::Char('N') | KeyCode::Char('q') => ConfirmAction::Exit,
18            _ => ConfirmAction::None,
19        }
20    }
21}
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26    use crossterm::event::KeyModifiers;
27
28    #[test]
29    fn test_confirm_action_apply() {
30        let key = KeyEvent::new(KeyCode::Char('y'), KeyModifiers::NONE);
31        assert_eq!(ConfirmAction::from_key(key), ConfirmAction::Apply);
32
33        let key = KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE);
34        assert_eq!(ConfirmAction::from_key(key), ConfirmAction::Apply);
35    }
36
37    #[test]
38    fn test_confirm_action_back() {
39        let key = KeyEvent::new(KeyCode::Char('b'), KeyModifiers::NONE);
40        assert_eq!(ConfirmAction::from_key(key), ConfirmAction::Back);
41    }
42
43    #[test]
44    fn test_confirm_action_exit() {
45        let key = KeyEvent::new(KeyCode::Char('n'), KeyModifiers::NONE);
46        assert_eq!(ConfirmAction::from_key(key), ConfirmAction::Exit);
47    }
48
49    #[test]
50    fn test_confirm_action_back_esc() {
51        let key = KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE);
52        assert_eq!(ConfirmAction::from_key(key), ConfirmAction::Back);
53    }
54}