green_barrel/fields/
hash.rs

1//! This type was created specifically for the hash field.
2
3use core::fmt::Debug;
4use mongodb::bson::oid::ObjectId;
5use serde::{Deserialize, Serialize};
6use std::error::Error;
7
8#[derive(Serialize, Deserialize, Clone, Debug)]
9pub struct HashField {
10    pub id: String, // The value is determined automatically. Format: "model-name--field-name".
11    pub label: String, // Web form field name.
12    pub field_type: String, // Field type.
13    pub input_type: String, // "hidden|text" - The value is determined automatically.
14    pub name: String, // The value is determined automatically.
15    pub value: Option<String>, // Sets the value of an element.
16    pub placeholder: String, // Displays prompt text.
17    pub minlength: usize, // The minimum number of characters allowed in the text.
18    pub maxlength: usize, // The maximum number of characters allowed in the text.
19    pub required: bool, // Mandatory field.
20    pub unique: bool, // The unique value of a field in a collection.
21    pub disabled: bool, // Blocks access and modification of the element.
22    pub readonly: bool, // Specifies that the field cannot be modified by the user.
23    pub is_hide: bool, // Hide field from user.
24    /// Example: `r# "autofocus tabindex="some number" size="some number"#`.    
25    pub other_attrs: String,
26    pub css_classes: String, // Example: "class-name-1 class-name-2".
27    pub hint: String,        // Additional explanation for the user.
28    pub warning: String,     // Warning information.
29    pub errors: Vec<String>, // The value is determined automatically.
30    pub alert: String, // Alert message for the entire web form. The value is determined automatically.
31    pub group: u32, // To optimize field traversal in the `paladins/check()` method. Hint: It is recommended not to change.
32}
33
34impl Default for HashField {
35    fn default() -> Self {
36        Self {
37            id: String::new(),
38            label: String::new(),
39            field_type: String::from("HashField"),
40            input_type: String::from("hidden"),
41            name: String::new(),
42            value: None,
43            placeholder: String::new(),
44            minlength: 12,
45            maxlength: 12,
46            required: false,
47            unique: false,
48            disabled: true,
49            readonly: false,
50            is_hide: true,
51            other_attrs: String::new(),
52            css_classes: String::new(),
53            hint: String::new(),
54            warning: String::new(),
55            errors: Vec::new(),
56            alert: String::new(),
57            group: 1,
58        }
59    }
60}
61
62impl HashField {
63    pub fn get(&self) -> Option<String> {
64        self.value.clone()
65    }
66    pub fn set(&mut self, value: &str) {
67        self.value = Some(String::from(value));
68    }
69
70    pub fn obj_id(&self) -> Result<Option<ObjectId>, Box<dyn Error>> {
71        let hash = self.value.clone().unwrap_or_default();
72        if let Ok(obj_id) = ObjectId::parse_str(hash.as_str()) {
73            return Ok(Some(obj_id));
74        }
75        Ok(None)
76    }
77    pub fn set_obj_id(&mut self, obj_id: ObjectId) {
78        self.value = Some(obj_id.to_hex());
79    }
80}