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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// SPDX-License-Identifier: MIT
// Copyright 2024 IROX Contributors
//

//!
//! This module contains a rudimentary reflection/type system
//!

use irox_enums_derive::{EnumIterItem, EnumName, EnumTryFromStr};

#[cfg(feature = "syn")]
pub use crate::syn::*;

///
/// A set of possible primitives
#[allow(non_camel_case_types)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, EnumName, EnumIterItem, EnumTryFromStr)]
#[non_exhaustive]
pub enum Primitives {
    u8,
    i8,
    u16,
    i16,
    u32,
    i32,
    f32,
    u64,
    i64,
    f64,
    u128,
    i128,

    bool,
    char,

    null,
}

impl Primitives {
    /// Returns the number of bytes required to encode/decode this value.
    #[must_use]
    #[allow(clippy::match_same_arms)]
    pub fn bytes_length(&self) -> usize {
        match self {
            Primitives::u8 => 1,
            Primitives::i8 => 1,
            Primitives::u16 => 2,
            Primitives::i16 => 2,
            Primitives::u32 => 4,
            Primitives::i32 => 4,
            Primitives::f32 => 4,
            Primitives::u64 => 8,
            Primitives::i64 => 8,
            Primitives::f64 => 8,
            Primitives::u128 => 16,
            Primitives::i128 => 16,
            Primitives::bool => 1,
            Primitives::char => 4,
            Primitives::null => 0,
        }
    }
}

///
/// A shuttle struct to pass around a primitive type and an associated value of the same type
///
#[allow(non_camel_case_types)]
#[derive(Debug, Copy, Clone, PartialEq, EnumName)]
#[non_exhaustive]
pub enum PrimitiveValue {
    u8(u8),
    i8(i8),
    u16(u16),
    i16(i16),
    u32(u32),
    i32(i32),
    f32(f32),
    u64(u64),
    i64(i64),
    f64(f64),
    u128(u128),
    i128(i128),
    bool(bool),
    char(char),

    null,
}

impl PrimitiveValue {
    /// Returns the type of this primitive
    #[must_use]
    pub const fn primitive(&self) -> Primitives {
        match self {
            PrimitiveValue::u8(_) => Primitives::u8,
            PrimitiveValue::i8(_) => Primitives::i8,
            PrimitiveValue::u16(_) => Primitives::u16,
            PrimitiveValue::i16(_) => Primitives::i16,
            PrimitiveValue::u32(_) => Primitives::u32,
            PrimitiveValue::i32(_) => Primitives::i32,
            PrimitiveValue::f32(_) => Primitives::f32,
            PrimitiveValue::u64(_) => Primitives::u64,
            PrimitiveValue::i64(_) => Primitives::i64,
            PrimitiveValue::f64(_) => Primitives::f64,
            PrimitiveValue::u128(_) => Primitives::u128,
            PrimitiveValue::i128(_) => Primitives::i128,
            PrimitiveValue::bool(_) => Primitives::bool,
            PrimitiveValue::char(_) => Primitives::char,
            PrimitiveValue::null => Primitives::null,
        }
    }
}

impl ToString for PrimitiveValue {
    fn to_string(&self) -> String {
        match self {
            PrimitiveValue::u8(v) => v.to_string(),
            PrimitiveValue::i8(v) => v.to_string(),
            PrimitiveValue::u16(v) => v.to_string(),
            PrimitiveValue::i16(v) => v.to_string(),
            PrimitiveValue::u32(v) => v.to_string(),
            PrimitiveValue::i32(v) => v.to_string(),
            PrimitiveValue::f32(v) => v.to_string(),
            PrimitiveValue::u64(v) => v.to_string(),
            PrimitiveValue::i64(v) => v.to_string(),
            PrimitiveValue::f64(v) => v.to_string(),
            PrimitiveValue::u128(v) => v.to_string(),
            PrimitiveValue::i128(v) => v.to_string(),
            PrimitiveValue::bool(v) => v.to_string(),
            PrimitiveValue::char(v) => v.to_string(),

            PrimitiveValue::null => "null".to_string(),
        }
    }
}

///
/// A struct to "Name" a primitive - like a Field with an associated type
///
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct NamedPrimitive {
    name: String,
    primitive: Primitives,
}

impl NamedPrimitive {
    #[must_use]
    pub fn new(name: String, primitive: Primitives) -> NamedPrimitive {
        NamedPrimitive { name, primitive }
    }

    /// Returns the name of the field
    #[must_use]
    pub fn name(&self) -> &String {
        &self.name
    }

    /// Returns the type of the field
    #[must_use]
    pub fn primitive(&self) -> Primitives {
        self.primitive
    }
}

///
/// A struct to "Name" a primitive with an associated value, like a Field with a value
///
#[derive(Debug, Clone, PartialEq)]
pub struct NamedPrimitiveValue {
    name: String,
    value: PrimitiveValue,
}

impl NamedPrimitiveValue {
    #[must_use]
    pub fn new(name: String, value: PrimitiveValue) -> NamedPrimitiveValue {
        NamedPrimitiveValue { name, value }
    }

    /// Returns the name of this field
    #[must_use]
    pub fn name(&self) -> &String {
        &self.name
    }

    /// Returns the stored value of the field
    #[must_use]
    pub fn value(&self) -> &PrimitiveValue {
        &self.value
    }
}

///
/// An enumeration to describe the type of a pseudo-primitve
#[derive(Debug, Copy, Clone, Eq, PartialEq, EnumName)]
pub enum PrimitiveType {
    Primitive(Primitives),
    Array(Primitives, usize),
    DynamicallySized(VariableType),
}

impl PrimitiveType {
    /// Returns the number of bytes required to encode/decode this value.  If dynamic, returns None.
    #[must_use]
    pub fn bytes_length(&self) -> Option<usize> {
        match self {
            PrimitiveType::Primitive(p) => Some(p.bytes_length()),
            PrimitiveType::Array(p, l) => Some(p.bytes_length() * *l),
            PrimitiveType::DynamicallySized(_) => None,
        }
    }
}

impl From<Primitives> for PrimitiveType {
    fn from(value: Primitives) -> Self {
        PrimitiveType::Primitive(value)
    }
}

impl From<VariableType> for PrimitiveType {
    fn from(value: VariableType) -> Self {
        PrimitiveType::DynamicallySized(value)
    }
}

///
/// An enumeration to describe a variable-length type
#[allow(non_camel_case_types)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, EnumName, EnumIterItem, EnumTryFromStr)]
#[non_exhaustive]
pub enum VariableType {
    str,
    u8_blob,
    u16_blob,
    u32_blob,
    u64_blob,
}

///
/// An enumeration to store the value of a dynamic/variable sized element
#[allow(non_camel_case_types)]
#[derive(Debug, Clone, Eq, PartialEq, EnumName)]
#[non_exhaustive]
pub enum DynamicallySizedValue {
    /// A string
    str(String),
    /// A blob of u8 of max length 255 (u8)
    u8_blob(Vec<u8>),
    /// A blob of u8 of max length 65535 (u16)
    u16_blob(Vec<u8>),
    /// A blob of u8 of max length [`u32::MAX`] (u32)
    u32_blob(Vec<u8>),
    /// A blob of u8 of max length [`u64::MAX`] (u64)
    u64_blob(Vec<u8>),
}

impl DynamicallySizedValue {
    /// Returns the type of this primitive
    #[must_use]
    pub const fn primitive(&self) -> VariableType {
        match self {
            DynamicallySizedValue::str(_) => VariableType::str,
            DynamicallySizedValue::u8_blob(_) => VariableType::u8_blob,
            DynamicallySizedValue::u16_blob(_) => VariableType::u16_blob,
            DynamicallySizedValue::u32_blob(_) => VariableType::u32_blob,
            DynamicallySizedValue::u64_blob(_) => VariableType::u64_blob,
        }
    }
}

impl ToString for DynamicallySizedValue {
    fn to_string(&self) -> String {
        match self {
            DynamicallySizedValue::str(v) => v.to_string(),
            DynamicallySizedValue::u8_blob(v)
            | DynamicallySizedValue::u16_blob(v)
            | DynamicallySizedValue::u32_blob(v)
            | DynamicallySizedValue::u64_blob(v) => String::from_utf8_lossy(v).to_string(),
        }
    }
}

impl From<DynamicallySizedValue> for VariableValue {
    fn from(value: DynamicallySizedValue) -> Self {
        VariableValue::DynamicallySized(value)
    }
}

impl From<PrimitiveValue> for VariableValue {
    fn from(value: PrimitiveValue) -> Self {
        VariableValue::Primitive(value)
    }
}
///
/// A value type that can be either statically sized (primitive) or variably sized (dynamic)
#[derive(Debug, Clone, PartialEq, EnumName)]
pub enum VariableValue {
    Primitive(PrimitiveValue),
    DynamicallySized(DynamicallySizedValue),
}

impl ToString for VariableValue {
    fn to_string(&self) -> String {
        match self {
            VariableValue::Primitive(p) => p.to_string(),
            VariableValue::DynamicallySized(d) => d.to_string(),
        }
    }
}

/// An element that has both a Name and a Type
#[derive(Debug, Clone, PartialEq)]
pub struct NamedVariable {
    name: String,
    ty: PrimitiveType,
}

impl NamedVariable {
    #[must_use]
    pub fn new(name: String, ty: PrimitiveType) -> Self {
        Self { name, ty }
    }

    /// Returns the name of the field
    #[must_use]
    pub fn name(&self) -> &String {
        &self.name
    }

    /// Returns the type of the field
    #[must_use]
    pub fn variable_type(&self) -> PrimitiveType {
        self.ty
    }
}

/// An element that has both a Name and a Type
#[derive(Debug, Clone, PartialEq)]
pub struct NamedVariableValue {
    name: String,
    value: VariableValue,
}
impl NamedVariableValue {
    #[must_use]
    pub fn new(name: String, value: VariableValue) -> NamedVariableValue {
        NamedVariableValue { name, value }
    }

    /// Returns the name of this field
    #[must_use]
    pub fn name(&self) -> &String {
        &self.name
    }

    /// Returns the stored value of the field
    #[must_use]
    pub fn value(&self) -> &VariableValue {
        &self.value
    }
}
impl From<NamedPrimitiveValue> for NamedVariableValue {
    fn from(value: NamedPrimitiveValue) -> Self {
        NamedVariableValue {
            name: value.name,
            value: VariableValue::Primitive(value.value),
        }
    }
}

#[cfg(feature = "syn")]
mod syn;