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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use crate::{
forms::{html_controls::HtmlControls, ToForm, Widget},
store::{FormCache, FORM_CACHE},
};
pub trait CachingForm: ToForm + HtmlControls {
fn form_wig() -> Result<std::collections::HashMap<String, Widget>, Box<dyn std::error::Error>> {
let key: String = Self::key();
let mut form_store: std::sync::MutexGuard<
'_,
std::collections::HashMap<String, FormCache>,
> = FORM_CACHE.lock().unwrap();
let mut form_cache: Option<&FormCache> = form_store.get(&key[..]);
if form_cache.is_none() {
let map_widgets: std::collections::HashMap<String, Widget> = Self::widgets()?;
let new_form_cache = FormCache {
map_widgets,
..Default::default()
};
form_store.insert(key.clone(), new_form_cache);
form_cache = form_store.get(&key[..]);
}
Ok(form_cache.unwrap().map_widgets.clone())
}
fn form_json() -> Result<String, Box<dyn std::error::Error>> {
let key: String = Self::key();
let mut form_store: std::sync::MutexGuard<
'_,
std::collections::HashMap<String, FormCache>,
> = FORM_CACHE.lock().unwrap();
let mut form_cache: Option<&FormCache> = form_store.get(&key[..]);
if form_cache.is_none() {
let map_widgets: std::collections::HashMap<String, Widget> = Self::widgets()?;
let new_form_cache = FormCache {
map_widgets,
..Default::default()
};
form_store.insert(key.clone(), new_form_cache);
form_cache = form_store.get(&key[..]);
}
let form_cache: &FormCache = form_cache.unwrap();
if form_cache.attrs_json.is_empty() {
let map_widgets: std::collections::HashMap<String, Widget> =
form_cache.map_widgets.clone();
let json_line = serde_json::to_string(&map_widgets)?;
let mut form_cache: FormCache = form_cache.clone();
form_cache.attrs_json = json_line.clone();
form_store.insert(key, form_cache.clone());
return Ok(json_line);
}
Ok(form_cache.attrs_json.clone())
}
fn form_html() -> Result<String, Box<dyn std::error::Error>> {
let key: String = Self::key();
let mut form_store: std::sync::MutexGuard<
'_,
std::collections::HashMap<String, FormCache>,
> = FORM_CACHE.lock().unwrap();
let mut form_cache: Option<&FormCache> = form_store.get(&key[..]);
if form_cache.is_none() {
let map_widgets: std::collections::HashMap<String, Widget> = Self::widgets()?;
let new_form_cache = FormCache {
map_widgets,
..Default::default()
};
form_store.insert(key.clone(), new_form_cache);
form_cache = form_store.get(&key[..]);
}
let form_cache: &FormCache = form_cache.unwrap();
if form_cache.controls_html.is_empty() {
let map_widgets: std::collections::HashMap<String, Widget> =
form_cache.map_widgets.clone();
let controls: String = Self::to_html(&Self::fields_name()?, map_widgets);
let mut form_cache: FormCache = form_cache.clone();
form_cache.controls_html = controls.clone();
form_store.insert(key, form_cache.clone());
return Ok(controls);
}
Ok(form_cache.controls_html.clone())
}
fn get_cache_data() -> Result<FormCache, Box<dyn std::error::Error>> {
let key: String = Self::key();
let mut form_store: std::sync::MutexGuard<
'_,
std::collections::HashMap<String, FormCache>,
> = FORM_CACHE.lock()?;
let mut form_cache: Option<&FormCache> = form_store.get(&key[..]);
if form_cache.is_none() {
let map_widgets: std::collections::HashMap<String, Widget> = Self::widgets()?;
let new_form_cache = FormCache {
map_widgets,
..Default::default()
};
form_store.insert(key.clone(), new_form_cache);
form_cache = form_store.get(&key[..]);
}
let form_cache: &FormCache = form_cache.unwrap();
Ok(form_cache.clone())
}
}