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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
//! Output data for QPaladins.
use mongodb::bson::{document::Document, oid::ObjectId};
use std::collections::HashMap;
use std::error::Error;
use crate::widgets::{generate_html::GenerateHtml, Enctype, HttpMethod, Widget};
/// Helper methods for converting output data (use in the paladins.rs module).
#[derive(Debug)]
pub enum OutputData {
Check(
(
bool,
Vec<String>,
HashMap<String, Widget>,
Document,
String,
String,
),
),
Save((bool, Vec<String>, HashMap<String, Widget>, String, String)),
Delete((bool, String)),
UpdatePassword((bool, String)),
Stub,
}
impl GenerateHtml for OutputData {}
impl OutputData {
//
/// Get Hash-line
// ---------------------------------------------------------------------------------------------
fn get_hash(map_widgets: &HashMap<String, Widget>) -> Result<String, Box<dyn Error>> {
Ok(map_widgets.get("hash").unwrap().value.clone())
}
///
/// # Example:
///
/// ```
/// let user_profile = UserProfile {...};
/// let output_data = user_profile.check()?;
/// let output_data = user_profile.save(None, None)?;
/// println!("{}", output_data.hash()?);
/// ```
///
pub fn hash(&self) -> Result<String, Box<dyn Error>> {
match self {
Self::Check(data) => Ok(Self::get_hash(&data.2)?),
Self::Save(data) => Ok(Self::get_hash(&data.2)?),
_ => Err("Invalid output type.")?,
}
}
/// Get MongoDB ID from hash-line
// ---------------------------------------------------------------------------------------------
///
/// # Example:
///
/// ```
/// let user_profile = UserProfile {...};
/// let output_data = user_profile.check()?;
/// let output_data = user_profile.save(None, None)?;
/// println!("{:?}", output_data.object_id()?);
/// ```
///
pub fn object_id(&self) -> Result<ObjectId, Box<dyn Error>> {
match self {
Self::Check(data) => Ok(ObjectId::with_string(Self::get_hash(&data.2)?.as_str())?),
Self::Save(data) => Ok(ObjectId::with_string(Self::get_hash(&data.2)?.as_str())?),
_ => Err("Invalid output type.")?,
}
}
/// Get Map of Widgets
// ---------------------------------------------------------------------------------------------
/// ( Wig - Widgets )
///
/// # Example:
///
/// ```
/// let user_profile = UserProfile {...};
/// let output_data = user_profile.check()?;
/// let output_data = user_profile.save(None, None)?;
/// println!("{:?}", output_data.to_wig()?);
/// ```
///
pub fn to_wig(&self) -> Result<HashMap<String, Widget>, Box<dyn Error>> {
match self {
Self::Check(data) => Ok(data.2.clone()),
Self::Save(data) => Ok(data.2.clone()),
_ => Err("Invalid output type.")?,
}
}
/// Get Json-line
// ---------------------------------------------------------------------------------------------
///
/// # Example:
///
/// ```
/// let user_profile = UserProfile {...};
/// let output_data = user_profile.check()?;
/// let output_data = user_profile.save(None, None)?;
/// println!("{}", output_data.to_json()?);
/// ```
///
pub fn to_json(&self) -> Result<String, Box<dyn Error>> {
match self {
Self::Check(data) => Ok(serde_json::to_string(&data.2)?),
Self::Save(data) => Ok(serde_json::to_string(&data.2)?),
_ => Err("Invalid output type.")?,
}
}
/// Json-line for admin panel.
// ---------------------------------------------------------------------------------------------
/// ( converts a widget map to a list, in the order of the Model fields )
///
/// # Example:
///
/// ```
/// let user_profile = UserProfile {...};
/// let output_data = user_profile.save(None, None)?;
/// println!("{}", output_data.to_json_for_admin()?);
/// ```
///
pub fn to_json_for_admin(&self) -> Result<String, Box<dyn Error>> {
let data = match self {
Self::Save(data) => data,
_ => Err("Invalid output type.")?,
};
let map_widgets = data.2.clone();
let mut widget_list: Vec<Widget> = Vec::new();
let hash = map_widgets.get("hash").unwrap().clone().value;
// Get a list of widgets in the order of the model fields.
for field_name in data.1.iter() {
let mut widget = map_widgets.get(field_name).unwrap().clone();
if field_name == "created_at" || field_name == "updated_at" {
widget.is_hide = false;
}
if field_name.contains("password") && !hash.is_empty() {
widget.widget = "hiddenText".to_string();
widget.input_type = "hidden".to_string();
}
widget_list.push(widget);
}
//
Ok(serde_json::to_string(&widget_list)?)
}
/// Get Html-code
// ---------------------------------------------------------------------------------------------
///
/// # Example:
///
/// ```
/// let user_profile = UserProfile {...};
/// let output_data = user_profile.check()?;
/// let output_data = user_profile.save(None, None)?;
/// //
/// println!("{}", output_data.to_html(None, None, None)?);
/// // OR
/// println!("{}", output_data.to_html(Some("/login"), Some(HttpMethod::POST), Some(Enctype::Multipart))?);
/// ```
///
pub fn to_html(
&self,
action: Option<&str>,
method: Option<HttpMethod>,
enctype: Option<Enctype>,
) -> Result<String, Box<dyn Error>> {
match self {
Self::Check(data) => Self::generate_html(
action,
method,
enctype,
data.4.as_str(),
data.5.as_str(),
&data.1,
&data.2,
),
Self::Save(data) => Self::generate_html(
action,
method,
enctype,
data.3.as_str(),
data.4.as_str(),
&data.1,
&data.2,
),
_ => Err("Invalid output type.")?,
}
}
/// Get validation status (boolean).
// ---------------------------------------------------------------------------------------------
///
/// # Example:
///
/// ```
/// let user_profile = UserProfile {...};
/// let output_data = user_profile.check()?;
/// let output_data = user_profile.save(None, None)?;
/// let output_data = user_profile.delete()?;
/// let output_data = user_profile.update_password()?;
/// assert!(result.is_valid()?);
/// ```
///
pub fn is_valid(&self) -> Result<bool, Box<dyn Error>> {
match self {
Self::Check(data) => Ok(data.0),
Self::Save(data) => Ok(data.0),
Self::Delete(data) => Ok(data.0),
Self::UpdatePassword(data) => Ok(data.0),
_ => Err("Invalid output type.")?,
}
}
/// Get Document
// ---------------------------------------------------------------------------------------------
///
/// # Example:
///
/// ```
/// let user_profile = UserProfile {...};
/// let output_data = user_profile.check()?;
/// println!("{:?}", user_profile.to_doc()?);
/// ```
///
pub fn to_doc(&self) -> Result<Document, Box<dyn Error>> {
match self {
Self::Check(data) => Ok(data.3.clone()),
_ => Err("Invalid output type.")?,
}
}
/// Printing errors to the console ( for development ).
// ---------------------------------------------------------------------------------------------
fn print_to_console(map_widgets: &HashMap<String, Widget>) {
let mut errors = String::new();
for (field_name, widget) in map_widgets {
let tmp = errors.clone();
if !widget.error.is_empty() {
errors = format!("{}\nField: `{}` -> {}", tmp, field_name, widget.error);
}
}
if !errors.is_empty() {
errors = errors.replace("<br>", " | ");
println!("\nErrors:{}\n", errors);
}
}
///
/// # Example:
///
/// ```
/// let user_profile = UserProfile {...};
/// let output_data = user_profile.check()?;
/// let output_data = user_profile.save(None, None)?;
/// println!("{}", output_data.print_err()?);
/// ```
///
pub fn print_err(&self) -> Result<(), Box<dyn Error>> {
match self {
Self::Check(data) => {
Self::print_to_console(&data.2);
Ok(())
}
Self::Save(data) => {
Self::print_to_console(&data.2);
Ok(())
}
_ => Err("Invalid output type.")?,
}
}
/// Description of the error if the document was not deleted or the password was not updated.
// ---------------------------------------------------------------------------------------------
/// (Main use for admin panel.)
///
/// # Example:
///
/// ```
/// let user_profile = UserProfile {...};
/// user_profile.save(None, None)?;
/// let output_data = user_profile.delete()?;
/// println!("{}", output_data.err_msg()?);
/// ```
///
pub fn err_msg(&self) -> Result<String, Box<dyn Error>> {
match self {
Self::Delete(data) => Ok(data.1.clone()),
Self::UpdatePassword(data) => Ok(data.1.clone()),
_ => Err("Invalid output type.")?,
}
}
}