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
61
62
63
64
65
#![allow(unused_imports)]
use std::collections::HashMap;
use std::io::Stdout;
use std::{
    borrow::{Borrow, BorrowMut},
    fmt,
    ops::Deref,
};
use tui::layout::Rect;
use tui::{
    backend::{Backend, CrosstermBackend, TestBackend},
    Frame,
};

use crate::event_response::EventResponse;

type Callback = fn(HashMap<String, String>) -> EventResponse;

pub trait IActionsStorage {
    fn has_action(self: &Self, name: String) -> bool;
    fn add_action<'b>(self: &'b mut Self, name: String, render: Callback) -> &'b mut Self;
    fn execute(self: &Self, name: String, state: HashMap<String, String>) -> Option<EventResponse>;
}

pub struct ActionsStorage {
    storage: HashMap<String, Callback>,
}

impl ActionsStorage {
    pub fn new() -> Self {
        ActionsStorage {
            storage: HashMap::new(),
        }
    }
}

impl IActionsStorage for ActionsStorage {
    fn add_action<'b>(self: &'b mut Self, name: String, action: Callback) -> &'b mut Self {
        self.storage.entry(name).or_insert(action);
        self
    }

    fn has_action(self: &Self, name: String) -> bool {
        self.storage.contains_key(&name)
    }

    fn execute(self: &Self, name: String, state: HashMap<String, String>) -> Option<EventResponse> {
        let opt = self.storage.get(&name).clone();
        if opt.is_some() {
            let f = opt.unwrap();
            Some(f(state))
        } else {
            None
        }
    }
}

impl fmt::Debug for ActionsStorage {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut r = f.debug_struct("RenderStorage");
        r.field("Components", &self.storage.keys());
        r.finish()
    }
}