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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//! Output data for QCommons.

use crate::widgets::Widget;
use mongodb::bson::document::Document;
use mongodb::{
    bson::{de::from_document, spec::ElementType, Bson},
    options::FindOptions,
    sync::Collection,
};
use std::{collections::HashMap, error::Error};

/// Helper methods for converting output data (use in the commons.rs module).
pub trait Converters {
    /// Get widgets map from document ( presence of widgets ).
    fn one_to_wig(
        doc: Option<Document>,
        ignore_fields: &Vec<String>,
        map_widget_type: &HashMap<String, String>,
        model_name: &str,
        fields_name: &Vec<String>,
        mut map_widgets: HashMap<String, Widget>,
    ) -> Result<Option<HashMap<String, Widget>>, Box<dyn Error>> {
        //
        if doc.is_some() {
            let prepared_doc =
                Self::to_prepared_doc(doc.unwrap(), ignore_fields, map_widget_type, model_name)?;
            for field in fields_name {
                if !ignore_fields.contains(field) {
                    let mut widget = map_widgets.get_mut(field).unwrap();
                    let doc = prepared_doc.get(field).unwrap();
                    if doc.element_type() != ElementType::Null {
                        match doc.element_type() {
                            ElementType::String => {
                                widget.value = doc.as_str().unwrap().to_string();
                            }
                            ElementType::Int32 => {
                                widget.value = doc.as_i32().unwrap().to_string();
                            }
                            ElementType::Int64 => {
                                widget.value = doc.as_i64().unwrap().to_string();
                            }
                            ElementType::Double => {
                                widget.value = doc.as_f64().unwrap().to_string();
                            }
                            ElementType::Boolean => {
                                widget.checked = doc.as_bool().unwrap();
                            }
                            ElementType::Array => {
                                widget.value =
                                    serde_json::to_string(&doc.clone().into_relaxed_extjson())?;
                            }
                            _ => match widget.widget.as_str() {
                                "inputFile" | "inputImage" => {
                                    widget.value =
                                        serde_json::to_string(&doc.clone().into_relaxed_extjson())?;
                                }
                                _ => Err(format!(
                                    "Model: `{}` > Method: `one_doc_to_wig()` \
                                    -> Invalid Widget type.",
                                    model_name
                                ))?,
                            },
                        }
                    }
                }
            }
            Ok(Some(map_widgets))
        } else {
            Ok(None)
        }
    }

    /// Get model instance from document.
    /// Hint: For the `save`, `update`, `delete` operations.
    fn to_model_instance<T>(
        doc: Option<Document>,
        ignore_fields: &Vec<String>,
        map_widget_type: &HashMap<String, String>,
        model_name: &str,
    ) -> Result<Option<T>, Box<dyn Error>>
    where
        T: serde::de::DeserializeOwned,
    {
        if doc.is_some() {
            let doc =
                Self::to_prepared_doc(doc.unwrap(), ignore_fields, map_widget_type, model_name)
                    .unwrap();
            let mut prepared_doc = mongodb::bson::document::Document::new();
            for (field_name, widget_type) in map_widget_type {
                if ignore_fields.contains(&field_name) {
                    continue;
                }
                let bson_val = doc.get(field_name).unwrap();
                if widget_type == "inputFile" || widget_type == "inputImage" {
                    prepared_doc.insert(
                        field_name,
                        if bson_val.element_type() != ElementType::Null {
                            let result =
                                serde_json::to_string(&bson_val.clone().into_relaxed_extjson())
                                    .unwrap();
                            Bson::String(result)
                        } else {
                            Bson::Null
                        },
                    );
                } else {
                    prepared_doc.insert(field_name, bson_val);
                }
            }
            Ok(Some(from_document::<T>(prepared_doc)?))
        } else {
            Ok(None)
        }
    }

    /// Get prepared document.
    /// Hint: Converting data types to model-friendly formats.
    // ---------------------------------------------------------------------------------------------
    fn to_prepared_doc(
        doc: Document,
        ignore_fields: &Vec<String>,
        map_widget_type: &HashMap<String, String>,
        model_name: &str,
    ) -> Result<Document, Box<dyn Error>> {
        //
        let mut prepared_doc = Document::new();
        for (field_name, widget_type) in map_widget_type {
            if ignore_fields.contains(&field_name) {
                continue;
            }
            if field_name == "hash" {
                let bson_val = doc.get("_id").unwrap();
                prepared_doc.insert(
                    field_name,
                    if bson_val.element_type() != ElementType::Null {
                        Bson::String(bson_val.as_object_id().unwrap().to_hex())
                    } else {
                        Err(format!(
                            "Model: `{}` > Field: `hash` > Method: `find_one()` -> \
                                Missing document identifier `_id`.",
                            model_name
                        ))?
                    },
                );
            } else if widget_type == "inputPassword" {
                let bson_val = doc.get(field_name).unwrap();
                prepared_doc.insert(
                    field_name,
                    if bson_val.element_type() != ElementType::Null {
                        Bson::String(String::new())
                    } else {
                        Bson::Null
                    },
                );
            } else if widget_type == "inputDate" {
                let bson_val = doc.get(field_name).unwrap();
                prepared_doc.insert(
                    field_name,
                    if bson_val.element_type() != ElementType::Null {
                        Bson::String(bson_val.as_datetime().unwrap().to_rfc3339()[..10].into())
                    } else {
                        Bson::Null
                    },
                );
            } else if widget_type == "inputDateTime" {
                let bson_val = doc.get(field_name).unwrap();
                prepared_doc.insert(
                    field_name,
                    if bson_val.element_type() != ElementType::Null {
                        Bson::String(bson_val.as_datetime().unwrap().to_rfc3339()[..19].into())
                    } else {
                        Bson::Null
                    },
                );
            } else {
                let bson_val = doc.get(field_name).unwrap();
                prepared_doc.insert(field_name, bson_val);
            }
        }

        Ok(prepared_doc)
    }

    /// Get prepared documents ( missing widgets ).
    fn many_to_docs(
        filter: Option<Document>,
        find_options: Option<FindOptions>,
        collection: Collection,
        ignore_fields: &Vec<String>,
        map_widget_type: &HashMap<String, String>,
        model_name: &str,
    ) -> Result<Vec<Document>, Box<dyn Error>> {
        //
        let mut cursor = collection.find(filter, find_options)?;
        let mut docs: Vec<Document> = Vec::new();
        while let Some(doc) = cursor.next() {
            let prepared_doc =
                Self::to_prepared_doc(doc?, ignore_fields, map_widget_type, model_name);
            docs.push(prepared_doc?);
        }

        Ok(docs)
    }

    /// Get json-line from document list ( missing widgets ).
    fn many_to_json(
        filter: Option<Document>,
        find_options: Option<FindOptions>,
        collection: Collection,
        ignore_fields: &Vec<String>,
        map_widget_type: &HashMap<String, String>,
        model_name: &str,
    ) -> Result<String, Box<dyn Error>> {
        //
        let mut cursor = collection.find(filter, find_options)?;
        let mut json_line = String::new();
        while let Some(doc) = cursor.next() {
            let prepared_doc =
                Self::to_prepared_doc(doc?, ignore_fields, map_widget_type, model_name);
            //
            json_line = format!(
                "{},{}",
                json_line,
                Bson::Document(prepared_doc?)
                    .into_relaxed_extjson()
                    .to_string(),
            );
        }

        if !json_line.is_empty() {
            Ok(format!("[{}]", &json_line[1..]))
        } else {
            Ok(String::new())
        }
    }
}