green_barrel/models/
helpers.rs

1//! Collection of auxiliary Structures, Enumerations.
2
3use serde::{Deserialize, Serialize};
4use serde_json::{json, Value};
5use std::collections::HashMap;
6
7/// Metadata ( model parameters )
8// -------------------------------------------------------------------------------------------------
9#[derive(Deserialize, Clone, Debug)]
10pub struct Meta {
11    pub model_name: String,
12    pub app_name: String,
13    pub unique_app_key: String,
14    pub service_name: String,
15    pub database_name: String,
16    pub db_query_docs_limit: u32,
17    pub collection_name: String, // Field type map
18    pub fields_count: usize,
19    pub fields_name: Vec<String>,
20    pub is_add_doc: bool,
21    pub is_up_doc: bool,
22    pub is_del_doc: bool,
23    pub is_use_addition: bool,
24    pub is_use_hooks: bool,
25    pub is_use_hash_slug: bool,
26    // <field_name, field_value_type>
27    pub field_value_type_map: HashMap<String, String>,
28    // <field_name, fields_type>
29    pub field_type_map: HashMap<String, String>,
30    // <field_name, default_value>
31    pub default_value_map: HashMap<String, Value>,
32    // List of field names that will not be saved to the database
33    pub ignore_fields: Vec<String>,
34    // Choice maps for fields type `choice`. Format: HashMap<field_name, choices>
35    pub choice_str_map: HashMap<String, Vec<String>>,
36    pub choice_i32_map: HashMap<String, Vec<i32>>,
37    pub choice_i64_map: HashMap<String, Vec<i64>>,
38    pub choice_f64_map: HashMap<String, Vec<f64>>,
39    pub model_json: Value,
40}
41
42impl Default for Meta {
43    fn default() -> Self {
44        Self {
45            model_name: String::new(),
46            app_name: String::new(),
47            unique_app_key: String::new(),
48            service_name: String::new(),
49            database_name: String::new(),
50            db_query_docs_limit: 0_u32,
51            collection_name: String::new(),
52            fields_count: 0_usize,
53            fields_name: Vec::new(),
54            is_add_doc: true,
55            is_up_doc: true,
56            is_del_doc: true,
57            is_use_addition: false,
58            is_use_hooks: false,
59            is_use_hash_slug: false,
60            field_value_type_map: HashMap::new(),
61            field_type_map: HashMap::new(),
62            default_value_map: HashMap::new(),
63            ignore_fields: Vec::new(),
64            choice_str_map: HashMap::new(),
65            choice_i32_map: HashMap::new(),
66            choice_i64_map: HashMap::new(),
67            choice_f64_map: HashMap::new(),
68            model_json: json!(null),
69        }
70    }
71}
72
73/// Helper structures for inputFile fields type.
74// -------------------------------------------------------------------------------------------------
75#[derive(Serialize, Deserialize, Default, Clone, Debug)]
76pub struct FileData {
77    pub path: String,
78    pub url: String,
79    pub name: String,
80    pub size: f64, // bytes
81    pub is_delete: bool,
82}
83
84/// Helper structures for inputImage fields type.
85// -------------------------------------------------------------------------------------------------
86#[derive(Serialize, Deserialize, Default, Clone, Debug)]
87pub struct ImageData {
88    pub path: String, // max size == original
89    pub path_xs: String,
90    pub path_sm: String,
91    pub path_md: String,
92    pub path_lg: String,
93    pub url: String, // max size == original
94    pub url_xs: String,
95    pub url_sm: String,
96    pub url_md: String,
97    pub url_lg: String,
98    pub name: String,
99    pub size: f64,   // bytes
100    pub width: f64,  // pixels
101    pub height: f64, // pixels
102    pub is_delete: bool,
103}
104
105/// To optimize the update_dyn_wig method.
106// -------------------------------------------------------------------------------------------------
107pub enum ControlArr<'a> {
108    Text(Vec<&'a str>),
109    I32(Vec<i32>),
110    I64(Vec<i64>),
111    F64(Vec<f64>),
112}
113impl<'a> ControlArr<'a> {
114    pub fn control_arr_str(&self) -> &Vec<&'a str> {
115        match self {
116            Self::Text(data) => data,
117            _ => panic!("Invalid data type."),
118        }
119    }
120    pub fn control_arr_i32(&self) -> &Vec<i32> {
121        match self {
122            Self::I32(data) => data,
123            _ => panic!("Invalid data type."),
124        }
125    }
126    pub fn control_arr_i64(&self) -> &Vec<i64> {
127        match self {
128            Self::I64(data) => data,
129            _ => panic!("Invalid data type."),
130        }
131    }
132    pub fn control_arr_f64(&self) -> &Vec<f64> {
133        match self {
134            Self::F64(data) => data,
135            _ => panic!("Invalid data type."),
136        }
137    }
138}