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
// SPDX-License-Identifier: MIT
// Copyright 2023 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,
    str,
    u8_blob,
    u16_blob,
    u32_blob,
    u64_blob,
    null,
}

///
/// A shuttle struct to pass around a primitive type and an associated value of the same type
///
#[allow(non_camel_case_types)]
#[derive(Debug, 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),
    str(String),
    u8_blob(Vec<u8>),
    u16_blob(Vec<u8>),
    u32_blob(Vec<u8>),
    u64_blob(Vec<u8>),
    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::str(_) => Primitives::str,
            PrimitiveValue::null => Primitives::null,
            PrimitiveValue::u8_blob(_) => Primitives::u8_blob,
            PrimitiveValue::u16_blob(_) => Primitives::u16_blob,
            PrimitiveValue::u32_blob(_) => Primitives::u32_blob,
            PrimitiveValue::u64_blob(_) => Primitives::u64_blob,
        }
    }
}

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::str(v) => v.to_string(),
            PrimitiveValue::u8_blob(v)
            | PrimitiveValue::u16_blob(v)
            | PrimitiveValue::u32_blob(v)
            | PrimitiveValue::u64_blob(v) => String::from_utf8_lossy(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
    }
}

#[derive(Debug, Copy, Clone, EnumName)]
pub enum PrimitiveType {
    Primitive(Primitives),
    Array(Primitives, usize),
}

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