muzzman_lib/
enums.rs

1#![allow(dead_code)]
2
3use std::{collections::HashMap, hash::Hash};
4
5use bytes_kman::TBytes;
6use serde::{Deserialize, Serialize};
7
8#[derive(Default, Debug, Serialize, Deserialize, bytes_kman::Bytes)]
9pub struct CustomEnum {
10    data: Vec<String>,
11    pub active: Option<usize>,
12    locked: bool,
13}
14
15impl PartialEq for CustomEnum {
16    fn eq(&self, other: &Self) -> bool {
17        self.data == other.data
18    }
19}
20
21impl Hash for CustomEnum {
22    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
23        self.data.hash(state);
24        self.active.hash(state);
25        self.locked.hash(state);
26    }
27}
28
29impl Clone for CustomEnum {
30    fn clone(&self) -> Self {
31        Self {
32            data: self.data.clone(),
33            active: self.active,
34            locked: false,
35        }
36    }
37}
38
39impl CustomEnum {
40    pub fn add(&mut self, field: &str) {
41        if !self.has(field) && !self.locked {
42            self.data.push(field.to_owned());
43        }
44    }
45
46    pub fn has(&self, field: &str) -> bool {
47        for _field in self.data.iter() {
48            if _field.trim() == field.trim() {
49                return true;
50            }
51        }
52
53        false
54    }
55
56    pub fn get_fields(&self) -> Vec<String> {
57        self.data.clone()
58    }
59
60    pub fn get_active(&self) -> Option<String> {
61        if let Some(active) = self.active {
62            return Some(self.data.get(active).unwrap().clone());
63        }
64        None
65    }
66
67    pub fn set_active(&mut self, active: Option<usize>) -> bool {
68        if let Some(active) = active {
69            if self.data.len() > active {
70                self.active = Some(active);
71                return true;
72            }
73            return false;
74        }
75
76        self.active = active;
77        true
78    }
79
80    pub fn lock(&mut self) {
81        self.locked = true;
82    }
83}
84
85#[cfg(test)]
86mod test_custom_enum {
87    use super::CustomEnum;
88
89    #[test]
90    fn add_field() {
91        let mut custom_enum = CustomEnum::default();
92        custom_enum.add("TestingField");
93
94        assert_eq!(
95            custom_enum,
96            CustomEnum {
97                data: vec!["TestingField".to_owned()],
98                locked: false,
99                active: None
100            }
101        );
102    }
103
104    #[test]
105    fn set_active_field() {
106        let mut custom_enum = CustomEnum::default();
107        custom_enum.add("TestingField");
108        custom_enum.add("FieldTesting");
109
110        custom_enum.set_active(Some(1)); // FieldTesting
111
112        assert_eq!(
113            custom_enum,
114            CustomEnum {
115                data: vec!["TestingField".to_owned(), "FieldTesting".to_owned()],
116                locked: false,
117                active: Some(1)
118            }
119        );
120    }
121}
122
123#[derive(Default, Debug, Serialize, Deserialize, bytes_kman::Bytes)]
124pub struct AdvanceEnum {
125    pub data: HashMap<String, bool>,
126}
127
128impl PartialEq for AdvanceEnum {
129    fn eq(&self, other: &Self) -> bool {
130        self.data == other.data
131    }
132}
133
134impl Hash for AdvanceEnum {
135    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
136        let _ = self
137            .data
138            .iter()
139            .map(|(k, v)| (k.hash(state), v.hash(state)));
140    }
141}
142
143impl Clone for AdvanceEnum {
144    fn clone(&self) -> Self {
145        Self {
146            data: self.data.clone(),
147        }
148    }
149}
150
151impl AdvanceEnum {
152    fn is_active(&self, key: &str) -> bool {
153        if let Some(has) = self.data.get(key) {
154            *has
155        } else {
156            false
157        }
158    }
159
160    fn set(&mut self, key: impl Into<String>, value: bool) -> Option<bool> {
161        self.data.insert(key.into(), value)
162    }
163}