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
use crate::forms::{html_controls::HtmlControls, Widget};
#[derive(Debug)]
pub enum OutputDataForm {
Check(
(
bool,
Vec<String>,
std::collections::HashMap<String, Widget>,
mongodb::bson::document::Document,
),
),
Save(
(
bool,
String,
Vec<String>,
std::collections::HashMap<String, Widget>,
),
),
}
impl HtmlControls for OutputDataForm {
fn html(&self) -> String {
match self {
Self::Check(data) => Self::to_html(&data.1, data.2.clone()),
Self::Save(data) => Self::to_html(&data.2, data.3.clone()),
}
}
}
impl OutputDataForm {
fn to_hash(
map_widgets: &std::collections::HashMap<String, Widget>,
) -> Result<String, Box<dyn std::error::Error>> {
let mut errors = String::new();
for (field_name, widget) in map_widgets {
let tmp = if !errors.is_empty() {
format!("{} ; ", errors)
} else {
String::new()
};
if !widget.error.is_empty() {
errors = format!("{}Field: `{}` - {}", tmp, field_name, widget.error);
}
}
if !errors.is_empty() {
Err(errors.replace("<br>", " | "))?
}
Ok(map_widgets.get(&"hash".to_owned()).unwrap().value.clone())
}
pub fn hash(&self) -> Result<String, Box<dyn std::error::Error>> {
match self {
Self::Check(data) => Ok(Self::to_hash(&data.2)?),
Self::Save(data) => Ok(Self::to_hash(&data.3)?),
}
}
pub fn id(&self) -> Result<mongodb::bson::oid::ObjectId, Box<dyn std::error::Error>> {
match self {
Self::Check(data) => Ok(mongodb::bson::oid::ObjectId::with_string(
Self::to_hash(&data.2)?.as_str(),
)?),
Self::Save(data) => Ok(mongodb::bson::oid::ObjectId::with_string(
Self::to_hash(&data.3)?.as_str(),
)?),
}
}
pub fn wig(&self) -> std::collections::HashMap<String, Widget> {
match self {
Self::Check(data) => data.2.clone(),
Self::Save(data) => data.3.clone(),
}
}
pub fn json(&self) -> Result<String, Box<dyn std::error::Error>> {
match self {
Self::Check(data) => Ok(serde_json::to_string(&data.2)?),
Self::Save(data) => Ok(serde_json::to_string(&data.3)?),
}
}
pub fn bool(&self) -> bool {
match self {
Self::Check(data) => data.0,
Self::Save(data) => data.0,
}
}
pub fn doc(&self) -> mongodb::bson::document::Document {
match self {
Self::Check(data) => data.3.clone(),
_ => panic!("Invalid output type."),
}
}
}