elvis_shared/tree/
state.rs1use crate::Error;
2use std::collections::HashMap;
3
4pub trait FnBox<P> {
6 fn call(&mut self, props: &P) -> Result<(), Error>;
8}
9
10type Hook<P> = Box<dyn FnBox<P>>;
12
13pub struct State<W, P> {
15 pub widget: W,
17 pub trigger: Hook<P>,
19 state: HashMap<String, String>,
20}
21
22impl<W, P> State<W, P> {
23 pub fn new(widget: W, trigger: Hook<P>) -> State<W, P> {
25 State {
26 state: HashMap::new(),
27 widget,
28 trigger,
29 }
30 }
31
32 pub fn process(&mut self, p: &P) -> Result<(), Error> {
34 self.trigger.call(p)
35 }
36
37 pub fn get(&self, k: String) -> String {
39 self.state.get(&k).unwrap_or(&"".to_string()).to_string()
40 }
41
42 pub fn set(&mut self, k: &str, v: &str) {
44 self.state.insert(k.to_string(), v.to_string());
45 }
46}