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
use crate::Error;
use std::collections::HashMap;
pub trait FnBox<P> {
fn call(&mut self, props: &P) -> Result<(), Error>;
}
type Hook<P> = Box<dyn FnBox<P>>;
pub struct State<W, P> {
pub widget: W,
pub trigger: Hook<P>,
state: HashMap<String, String>,
}
impl<W, P> State<W, P> {
pub fn new(widget: W, trigger: Hook<P>) -> State<W, P> {
State {
state: HashMap::new(),
widget,
trigger,
}
}
pub fn process(&mut self, p: &P) -> Result<(), Error> {
self.trigger.call(p)
}
pub fn get(&self, k: String) -> String {
self.state.get(&k).unwrap_or(&"".to_string()).to_string()
}
pub fn set(&mut self, k: &str, v: &str) {
self.state.insert(k.to_string(), v.to_string());
}
}