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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use crate::workflows::metadata::MetadataValueType;
use std::collections::HashMap;

/// How much to shift a u16 in order to store/read the
const VALUE_TYPE_SHIFT: u16 = 12;

/// Distinctly identifies a single metadata attribute that can have data stored for it.
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub struct MetadataKey {
    pub(super) klv_id: u16,
    pub(super) value_type: MetadataValueType,
}

/// Creates distinct metadata keys for each unique combination of metadata name and value type.
/// If the same name and type pair are registered on the same `MetadataKeyMap` instance, then those
/// keys returned are guaranteed to be equal to each other.
///
/// Keys created between different instances of `MetadataKeyMap` are not guaranteed (nor likely)
/// to be consistent. Therefore, keys should only be compared with keys created by the same map
/// instance, and in most cases you probably only want one instance for the whole process.
#[derive(Default)]
pub struct MetadataKeyMap {
    name_to_key_map: HashMap<MetadataNameTypePair, MetadataKey>,
    next_id: u16,
}

#[derive(PartialEq, Eq, Hash)]
struct MetadataNameTypePair {
    name: &'static str,
    value_type: MetadataValueType,
}

impl MetadataKey {
    pub(super) fn from_klv_id(klv_id: u16) -> Self {
        let value_type = value_type_from_klv_id(klv_id);

        MetadataKey { klv_id, value_type }
    }
}

impl MetadataKeyMap {
    /// Creates a new `MetadataKeyMap` instance
    pub fn new() -> Self {
        Default::default()
    }

    /// Registers the supplied metadata name and value type pair with the key map. If this
    /// pair has not been registered yet then a new `MetadataKey` will be generated and returned.
    /// If the same pair has already been registered, then the pre-generated key will be returned
    pub fn register(&mut self, name: &'static str, value_type: MetadataValueType) -> MetadataKey {
        let name_type_pair = MetadataNameTypePair { name, value_type };
        match self.name_to_key_map.get(&name_type_pair) {
            Some(key) => *key,
            None => {
                let id = apply_value_type_to_klv_id(self.next_id, value_type);
                self.next_id = match self.next_id.checked_add(1) {
                    Some(num) => num,
                    None => panic!("Too many metadata key name and type pairs added to key map, only 4,095 are allowed"),
                };

                let key = MetadataKey {
                    klv_id: id,
                    value_type,
                };

                self.name_to_key_map.insert(name_type_pair, key);

                key
            }
        }
    }
}

fn apply_value_type_to_klv_id(id: u16, value_type: MetadataValueType) -> u16 {
    let mut type_id = match value_type {
        MetadataValueType::U8 => 1,
        MetadataValueType::U16 => 2,
        MetadataValueType::U32 => 3,
        MetadataValueType::U64 => 4,
        MetadataValueType::I8 => 5,
        MetadataValueType::I16 => 6,
        MetadataValueType::I32 => 7,
        MetadataValueType::I64 => 8,
        MetadataValueType::Bool => 9,
        MetadataValueType::Bytes => 10,
    };

    type_id <<= VALUE_TYPE_SHIFT;
    id | type_id
}

fn value_type_from_klv_id(klv_id: u16) -> MetadataValueType {
    let value_type_id = klv_id >> VALUE_TYPE_SHIFT;
    match value_type_id {
        1 => MetadataValueType::U8,
        2 => MetadataValueType::U16,
        3 => MetadataValueType::U32,
        4 => MetadataValueType::U64,
        5 => MetadataValueType::I8,
        6 => MetadataValueType::I16,
        7 => MetadataValueType::I32,
        8 => MetadataValueType::I64,
        9 => MetadataValueType::Bool,
        10 => MetadataValueType::Bytes,
        x => panic!("Unknown value type id of {}", x),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn can_apply_u8_value_type_to_klv_id() {
        let original_id = 5;
        let id = apply_value_type_to_klv_id(original_id, MetadataValueType::U8);
        assert_ne!(
            id, original_id,
            "Applied id should not have been the same as the original id"
        );

        let value_type = value_type_from_klv_id(id);
        assert_eq!(value_type, MetadataValueType::U8);
    }

    #[test]
    fn can_apply_u16_value_type_to_klv_id() {
        let original_id = 5;
        let id = apply_value_type_to_klv_id(original_id, MetadataValueType::U16);
        assert_ne!(
            id, original_id,
            "Applied id should not have been the same as the original id"
        );

        let value_type = value_type_from_klv_id(id);
        assert_eq!(value_type, MetadataValueType::U16);
    }

    #[test]
    fn can_apply_u32_value_type_to_klv_id() {
        let original_id = 5;
        let id = apply_value_type_to_klv_id(original_id, MetadataValueType::U32);
        assert_ne!(
            id, original_id,
            "Applied id should not have been the same as the original id"
        );

        let value_type = value_type_from_klv_id(id);
        assert_eq!(value_type, MetadataValueType::U32);
    }

    #[test]
    fn can_apply_u64_value_type_to_klv_id() {
        let original_id = 5;
        let id = apply_value_type_to_klv_id(original_id, MetadataValueType::U64);
        assert_ne!(
            id, original_id,
            "Applied id should not have been the same as the original id"
        );

        let value_type = value_type_from_klv_id(id);
        assert_eq!(value_type, MetadataValueType::U64);
    }

    #[test]
    fn can_apply_i8_value_type_to_klv_id() {
        let original_id = 5;
        let id = apply_value_type_to_klv_id(original_id, MetadataValueType::I8);
        assert_ne!(
            id, original_id,
            "Applied id should not have been the same as the original id"
        );

        let value_type = value_type_from_klv_id(id);
        assert_eq!(value_type, MetadataValueType::I8);
    }

    #[test]
    fn can_apply_i16_value_type_to_klv_id() {
        let original_id = 5;
        let id = apply_value_type_to_klv_id(original_id, MetadataValueType::I16);
        assert_ne!(
            id, original_id,
            "Applied id should not have been the same as the original id"
        );

        let value_type = value_type_from_klv_id(id);
        assert_eq!(value_type, MetadataValueType::I16);
    }

    #[test]
    fn can_apply_i32_value_type_to_klv_id() {
        let original_id = 5;
        let id = apply_value_type_to_klv_id(original_id, MetadataValueType::I32);
        assert_ne!(
            id, original_id,
            "Applied id should not have been the same as the original id"
        );

        let value_type = value_type_from_klv_id(id);
        assert_eq!(value_type, MetadataValueType::I32);
    }

    #[test]
    fn can_apply_i64_value_type_to_klv_id() {
        let original_id = 5;
        let id = apply_value_type_to_klv_id(original_id, MetadataValueType::I64);
        assert_ne!(
            id, original_id,
            "Applied id should not have been the same as the original id"
        );

        let value_type = value_type_from_klv_id(id);
        assert_eq!(value_type, MetadataValueType::I64);
    }

    #[test]
    fn can_apply_bool_value_type_to_klv_id() {
        let original_id = 5;
        let id = apply_value_type_to_klv_id(original_id, MetadataValueType::Bool);
        assert_ne!(
            id, original_id,
            "Applied id should not have been the same as the original id"
        );

        let value_type = value_type_from_klv_id(id);
        assert_eq!(value_type, MetadataValueType::Bool);
    }

    #[test]
    fn can_apply_bytes_value_type_to_klv_id() {
        let original_id = 5;
        let id = apply_value_type_to_klv_id(original_id, MetadataValueType::Bytes);
        assert_ne!(
            id, original_id,
            "Applied id should not have been the same as the original id"
        );

        let value_type = value_type_from_klv_id(id);
        assert_eq!(value_type, MetadataValueType::Bytes);
    }

    #[test]
    fn same_name_type_pair_gets_same_key_returned() {
        let name = "test123";

        let mut map = MetadataKeyMap::default();
        let key1 = map.register(name, MetadataValueType::U32);
        let key2 = map.register(name, MetadataValueType::U32);

        assert_eq!(key1, key2);
    }

    #[test]
    fn different_name_gets_different_keys_returned() {
        let name1 = "test123";
        let name2 = "3456";

        let mut map = MetadataKeyMap::default();
        let key1 = map.register(name1, MetadataValueType::Bool);
        let key2 = map.register(name2, MetadataValueType::Bool);

        assert_ne!(key1, key2);
    }

    #[test]
    fn different_type_gets_different_keys_returned() {
        let name = "test123";

        let mut map = MetadataKeyMap::default();
        let key1 = map.register(name, MetadataValueType::Bool);
        let key2 = map.register(name, MetadataValueType::U32);

        assert_ne!(key1, key2);
    }
}