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
//! # Caching.
//! Caching information about Forms for speed up work.
//!
//! Trait:
//! `Caching` - Methods caching information about Forms for speed up work.
//!
//! Methods:
//! `widgets_to_cache` - Add map of widgets to cache.
//! `form_wig` - Get an widgets map for page template.
//! `form_json` - Get Form attributes in Json format for page templates.
//! `form_html` - Get Html Form of Model for page templates.
//! `get_cache_data` - Get cached Form data.
//!

use crate::{
    forms::{html_controls::HtmlControls, ToForm, Widget},
    store::{FormCache, FORM_STORE},
};

// Caching information about Forms for speed up work.
// *************************************************************************************************
pub trait CachingForm: ToForm + HtmlControls {
    /// Add map of widgets to cache.
    // ---------------------------------------------------------------------------------------------
    fn widgets_to_cache() -> Result<(), Box<dyn std::error::Error>> {
        // Get a key to access Model data in the cache.
        let key: String = Self::key();
        // Get write access in cache.
        let mut form_store = FORM_STORE.write()?;
        // Create `FormCache` default and add map of widgets.
        let map_widgets: std::collections::HashMap<String, Widget> = Self::widgets()?;
        let new_form_cache = FormCache {
            map_widgets,
            ..Default::default()
        };
        // Save structure `FormCache` to store.
        form_store.insert(key, new_form_cache);
        //
        Ok(())
    }

    /// Get an widgets map for page template.
    // ---------------------------------------------------------------------------------------------
    ///
    /// # Example:
    ///
    /// ```
    /// let widgets_map = RestorePasswordForm::form_wig()?;
    /// println!("{:?}", widgets_map);
    /// ```
    ///
    fn form_wig() -> Result<std::collections::HashMap<String, Widget>, Box<dyn std::error::Error>> {
        // Get a key to access Model data in the cache.
        let key: String = Self::key();
        // Get read access from cache.
        let mut form_store = FORM_STORE.read()?;
        // Check if there is widgets map for the Form in the cache.
        if !form_store.contains_key(key.as_str()) {
            // Unlock.
            drop(form_store);
            // Add map of widgets to cache.
            Self::widgets_to_cache()?;
            // Reaccess.
            form_store = FORM_STORE.read()?;
        }
        // Get data and return the result.
        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()
            ))?
        }
    }

    /// Get Form attributes in Json format for page templates.
    // ---------------------------------------------------------------------------------------------
    ///
    /// # Example:
    ///
    /// ```
    /// let json_line = RestorePasswordForm::form_json()?;
    /// println!("{}", json_line);
    /// ```
    ///
    fn form_json() -> Result<String, Box<dyn std::error::Error>> {
        // Get a key to access Model data in the cache.
        let key: String = Self::key();
        // Get read access from cache.
        let mut form_store = FORM_STORE.read()?;
        // Check if there is widgets map for the Form in the cache.
        if !form_store.contains_key(key.as_str()) {
            // Unlock.
            drop(form_store);
            // Add map of widgets to cache.
            Self::widgets_to_cache()?;
            // Reaccess.
            form_store = FORM_STORE.read()?;
        }
        // Get data and return the result.
        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()
            ))?
        }
    }

    /// Get Html Form of Model for page templates.
    // ---------------------------------------------------------------------------------------------
    ///
    /// # Example:
    ///
    /// ```
    /// let html = RestorePasswordForm::form_html()?;
    /// println!("{}", html);
    /// ```
    ///
    fn form_html() -> Result<String, Box<dyn std::error::Error>> {
        // Get a key to access Model data in the cache.
        let key: String = Self::key();
        // Get read access from cache.
        let mut form_store = FORM_STORE.read()?;
        // Check if there is widgets map for the Form in the cache.
        if !form_store.contains_key(key.as_str()) {
            // Unlock.
            drop(form_store);
            // Add map of widgets to cache.
            Self::widgets_to_cache()?;
            // Reaccess.
            form_store = FORM_STORE.read()?;
        }
        // Get data and return the result.
        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()
            ))?
        }
    }

    /// Get cached Form data.
    // ---------------------------------------------------------------------------------------------
    ///
    /// # Example:
    ///
    /// ```
    /// let form_cache = RestorePasswordForm::get_cache_data()?;
    /// println!("{:?}", form_cache);
    /// ```
    ///
    fn get_cache_data() -> Result<FormCache, Box<dyn std::error::Error>> {
        // Get a key to access Model data in the cache.
        let key: String = Self::key();
        // Get read access from cache.
        let mut form_store = FORM_STORE.read()?;
        // Check if there is widgets map for the Form in the cache.
        if !form_store.contains_key(key.as_str()) {
            // Unlock.
            drop(form_store);
            // Add map of widgets to cache.
            Self::widgets_to_cache()?;
            // Reaccess.
            form_store = FORM_STORE.read()?;
        }
        // Get data and return the result.
        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()
            ))?
        }
    }
}