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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use crate::{
forms::{html_controls::HtmlControls, ToForm, Widget},
store::{FormCache, FORM_STORE},
};
pub trait CachingForm: ToForm + HtmlControls {
fn widgets_to_cache() -> Result<(), Box<dyn std::error::Error>> {
let key: String = Self::key();
let mut form_store = FORM_STORE.write()?;
let map_widgets: std::collections::HashMap<String, Widget> = Self::widgets()?;
let new_form_cache = FormCache {
map_widgets,
..Default::default()
};
form_store.insert(key, new_form_cache);
Ok(())
}
fn form_wig() -> Result<std::collections::HashMap<String, Widget>, Box<dyn std::error::Error>> {
let key: String = Self::key();
let mut form_store = FORM_STORE.read()?;
if !form_store.contains_key(key.as_str()) {
drop(form_store);
Self::widgets_to_cache()?;
form_store = FORM_STORE.read()?;
}
if let Some(form_cache) = form_store.get(key.as_str()) {
Ok(form_cache.map_widgets.clone())
} else {
Err(format!(
"Form: `{}` -> Method: `form_wig()` : Failed to get data from cache.",
Self::form_name()
))?
}
}
fn form_json() -> Result<String, Box<dyn std::error::Error>> {
let key: String = Self::key();
let mut form_store = FORM_STORE.read()?;
if !form_store.contains_key(key.as_str()) {
drop(form_store);
Self::widgets_to_cache()?;
form_store = FORM_STORE.read()?;
}
if let Some(form_cache) = form_store.get(key.as_str()) {
if form_cache.form_json.is_empty() {
drop(form_store);
let mut form_store = FORM_STORE.write()?;
let form_cache = form_store.get(key.as_str()).unwrap();
let json = serde_json::to_string(&form_cache.map_widgets.clone())?;
let mut new_form_cache = form_cache.clone();
new_form_cache.form_json = json.clone();
form_store.insert(key, new_form_cache);
return Ok(json);
}
Ok(form_cache.form_json.clone())
} else {
Err(format!(
"Form: `{}` -> Method: `form_json()` : Failed to get data from cache.",
Self::form_name()
))?
}
}
fn form_html() -> Result<String, Box<dyn std::error::Error>> {
let key: String = Self::key();
let mut form_store = FORM_STORE.read()?;
if !form_store.contains_key(key.as_str()) {
drop(form_store);
Self::widgets_to_cache()?;
form_store = FORM_STORE.read()?;
}
if let Some(form_cache) = form_store.get(key.as_str()) {
if form_cache.form_html.is_empty() {
drop(form_store);
let mut form_store = FORM_STORE.write()?;
let form_cache = form_store.get(key.as_str()).unwrap();
let html =
Self::to_html(&form_cache.meta.fields_name, form_cache.map_widgets.clone());
let mut new_form_cache = form_cache.clone();
new_form_cache.form_html = html.clone();
form_store.insert(key, new_form_cache);
return Ok(html);
}
Ok(form_cache.form_html.clone())
} else {
Err(format!(
"Form: `{}` -> Method: `form_html()` : Failed to get data from cache.",
Self::form_name()
))?
}
}
fn get_cache_data() -> Result<FormCache, Box<dyn std::error::Error>> {
let key: String = Self::key();
let mut form_store = FORM_STORE.read()?;
if !form_store.contains_key(key.as_str()) {
drop(form_store);
Self::widgets_to_cache()?;
form_store = FORM_STORE.read()?;
}
if let Some(form_cache) = form_store.get(key.as_str()) {
Ok(form_cache.clone())
} else {
Err(format!(
"Form: `{}` -> Method: `get_cache_data()` : Failed to get data from cache.",
Self::form_name()
))?
}
}
}