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
#![allow(dead_code)]

use std::{collections::HashMap, hash::Hash};

use bytes_kman::TBytes;
use serde::{Deserialize, Serialize};

#[derive(Default, Debug, Serialize, Deserialize, bytes_kman::Bytes)]
pub struct CustomEnum {
    data: Vec<String>,
    pub active: Option<usize>,
    locked: bool,
}

impl PartialEq for CustomEnum {
    fn eq(&self, other: &Self) -> bool {
        self.data == other.data
    }
}

impl Hash for CustomEnum {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.data.hash(state);
        self.active.hash(state);
        self.locked.hash(state);
    }
}

impl Clone for CustomEnum {
    fn clone(&self) -> Self {
        Self {
            data: self.data.clone(),
            active: self.active,
            locked: false,
        }
    }
}

impl CustomEnum {
    pub fn add(&mut self, field: &str) {
        if !self.has(field) && !self.locked {
            self.data.push(field.to_owned());
        }
    }

    pub fn has(&self, field: &str) -> bool {
        for _field in self.data.iter() {
            if _field.trim() == field.trim() {
                return true;
            }
        }

        false
    }

    pub fn get_fields(&self) -> Vec<String> {
        self.data.clone()
    }

    pub fn get_active(&self) -> Option<String> {
        if let Some(active) = self.active {
            return Some(self.data.get(active).unwrap().clone());
        }
        None
    }

    pub fn set_active(&mut self, active: Option<usize>) -> bool {
        if let Some(active) = active {
            if self.data.len() > active {
                self.active = Some(active);
                return true;
            }
            return false;
        }

        self.active = active;
        true
    }

    pub fn lock(&mut self) {
        self.locked = true;
    }
}

#[cfg(test)]
mod test_custom_enum {
    use super::CustomEnum;

    #[test]
    fn add_field() {
        let mut custom_enum = CustomEnum::default();
        custom_enum.add("TestingField");

        assert_eq!(
            custom_enum,
            CustomEnum {
                data: vec!["TestingField".to_owned()],
                locked: false,
                active: None
            }
        );
    }

    #[test]
    fn set_active_field() {
        let mut custom_enum = CustomEnum::default();
        custom_enum.add("TestingField");
        custom_enum.add("FieldTesting");

        custom_enum.set_active(Some(1)); // FieldTesting

        assert_eq!(
            custom_enum,
            CustomEnum {
                data: vec!["TestingField".to_owned(), "FieldTesting".to_owned()],
                locked: false,
                active: Some(1)
            }
        );
    }
}

#[derive(Default, Debug, Serialize, Deserialize, bytes_kman::Bytes)]
pub struct AdvanceEnum {
    pub data: HashMap<String, bool>,
}

impl PartialEq for AdvanceEnum {
    fn eq(&self, other: &Self) -> bool {
        self.data == other.data
    }
}

impl Hash for AdvanceEnum {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        let _ = self
            .data
            .iter()
            .map(|(k, v)| (k.hash(state), v.hash(state)));
    }
}

impl Clone for AdvanceEnum {
    fn clone(&self) -> Self {
        Self {
            data: self.data.clone(),
        }
    }
}

impl AdvanceEnum {
    fn is_active(&self, key: &str) -> bool {
        if let Some(has) = self.data.get(key) {
            *has
        } else {
            false
        }
    }

    fn set(&mut self, key: impl Into<String>, value: bool) -> Option<bool> {
        self.data.insert(key.into(), value)
    }
}