green_barrel/fields/
hash.rs1use 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, pub label: String, pub field_type: String, pub input_type: String, pub name: String, pub value: Option<String>, pub placeholder: String, pub minlength: usize, pub maxlength: usize, pub required: bool, pub unique: bool, pub disabled: bool, pub readonly: bool, pub is_hide: bool, pub other_attrs: String,
26 pub css_classes: String, pub hint: String, pub warning: String, pub errors: Vec<String>, pub alert: String, pub group: u32, }
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}