tui_markup_renderer/
actions.rs1#![allow(unused_imports)]
2use std::collections::HashMap;
3use std::io::Stdout;
4use std::{
5 borrow::{Borrow, BorrowMut},
6 fmt,
7 ops::Deref,
8};
9use tui::layout::Rect;
10use tui::{
11 backend::{Backend, CrosstermBackend, TestBackend},
12 Frame,
13};
14
15use crate::event_response::EventResponse;
16use crate::markup_element::MarkupElement;
17
18type Callback = fn(HashMap<String, String>, Option<MarkupElement>) -> EventResponse;
19
20pub trait IActionsStorage {
21 fn has_action(&self, name: String) -> bool;
22 fn add_action(&mut self, name: String, render: Callback) -> &mut Self;
23 fn execute(&self, name: String, state: HashMap<String, String>, node: Option<MarkupElement>) -> Option<EventResponse>;
24}
25
26#[derive(Default)]
27pub struct ActionsStorage {
28 storage: HashMap<String, Callback>,
29}
30
31impl ActionsStorage {
32 pub fn new() -> Self {
33 ActionsStorage {
34 storage: HashMap::new(),
35 }
36 }
37}
38
39impl IActionsStorage for ActionsStorage {
40 fn add_action(&mut self, name: String, action: Callback) -> &mut Self {
41 self.storage.entry(name).or_insert(action);
42 self
43 }
44
45 fn has_action(&self, name: String) -> bool {
46 self.storage.contains_key(&name)
47 }
48
49 fn execute(&self, name: String, state: HashMap<String, String>, node: Option<MarkupElement>) -> Option<EventResponse> {
50 let opt = self.storage.get(&name);
51 opt.map(|f| f(state, node.clone()))
52 }
53}
54
55impl fmt::Debug for ActionsStorage {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 let mut r = f.debug_struct("RenderStorage");
58 r.field("Components", &self.storage.keys());
59 r.finish()
60 }
61}
62