rcus-core 0.2.0

Core types for rcus: retained view tree, style, input, and the host bridge protocol
Documentation
use std::collections::HashMap;

use crate::action::{Action, ActionError};

#[derive(Default)]
pub struct App {
    actions: HashMap<String, Box<dyn Fn()>>,
}

impl App {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn register_action(&mut self, name: impl Into<String>, handler: impl Fn() + 'static) {
        self.actions.insert(name.into(), Box::new(handler));
    }

    pub fn dispatch(&self, action: &Action) -> Result<(), ActionError> {
        let handler = self
            .actions
            .get(action.name())
            .ok_or_else(|| ActionError::Unknown(action.name().to_string()))?;
        handler();
        Ok(())
    }

    pub fn has_action(&self, name: &str) -> bool {
        self.actions.contains_key(name)
    }
}