1use std::collections::HashMap;
2
3use crate::action::{Action, ActionError};
4
5#[derive(Default)]
6pub struct App {
7 actions: HashMap<String, Box<dyn Fn()>>,
8}
9
10impl App {
11 pub fn new() -> Self {
12 Self::default()
13 }
14
15 pub fn register_action(&mut self, name: impl Into<String>, handler: impl Fn() + 'static) {
16 self.actions.insert(name.into(), Box::new(handler));
17 }
18
19 pub fn dispatch(&self, action: &Action) -> Result<(), ActionError> {
20 let handler = self
21 .actions
22 .get(action.name())
23 .ok_or_else(|| ActionError::Unknown(action.name().to_string()))?;
24 handler();
25 Ok(())
26 }
27
28 pub fn has_action(&self, name: &str) -> bool {
29 self.actions.contains_key(name)
30 }
31}