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
use crate::Variable;
use std::collections::HashMap;

pub trait ObjectValue<V> {
    type Iterator<'a>: Iterator<Item = (&'a str, &'a V)>
    where
        Self: 'a,
        V: 'a;

    fn iter(&self) -> Self::Iterator<'_>;
}

pub trait IntegerValue {
    fn to_i32(&self) -> i32;
}

impl IntegerValue for i32 {
    fn to_i32(&self) -> i32 {
        *self
    }
}

pub trait FloatValue {
    fn to_f64(&self) -> f64;
}

impl FloatValue for f64 {
    fn to_f64(&self) -> f64 {
        *self
    }
}

pub trait StringValue: AsRef<str> {}

impl StringValue for String {}

pub trait BooleanValue {
    fn to_bool(&self) -> bool;
}

impl BooleanValue for bool {
    fn to_bool(&self) -> bool {
        *self
    }
}

pub trait EnumValue: AsRef<str> {}

impl EnumValue for String {}

pub trait ListValue<V>: AsRef<[V]> {}

pub trait AbstractValue<const CONST: bool>:
    Into<
        Value<
            CONST,
            Self::Variable,
            Self::Integer,
            Self::Float,
            Self::String,
            Self::Boolean,
            Self::Null,
            Self::Enum,
            Self::List,
            Self::Object,
        >,
    > + AsRef<
        Value<
            CONST,
            Self::Variable,
            Self::Integer,
            Self::Float,
            Self::String,
            Self::Boolean,
            Self::Null,
            Self::Enum,
            Self::List,
            Self::Object,
        >,
    >
{
    type Variable: Variable;
    type Integer: IntegerValue;
    type Float: FloatValue;
    type String: StringValue;
    type Boolean: BooleanValue;
    type Null;
    type Enum: EnumValue;
    type List: ListValue<
        Value<
            CONST,
            Self::Variable,
            Self::Integer,
            Self::Float,
            Self::String,
            Self::Boolean,
            Self::Null,
            Self::Enum,
            Self::List,
            Self::Object,
        >,
    >;
    type Object: ObjectValue<
        Value<
            CONST,
            Self::Variable,
            Self::Integer,
            Self::Float,
            Self::String,
            Self::Boolean,
            Self::Null,
            Self::Enum,
            Self::List,
            Self::Object,
        >,
    >;
}

pub trait AbstractConstValue: AbstractValue<true> {}
pub trait AbstractVariableValue: AbstractValue<false> {}

#[derive(Debug, Clone, strum::Display)]
#[strum(serialize_all = "lowercase")]
pub enum Value<
    const CONST: bool,
    V: Variable,
    I: IntegerValue,
    F: FloatValue,
    S: StringValue,
    B: BooleanValue,
    N,
    E: EnumValue,
    L: ListValue<Self>,
    O: ObjectValue<Self>,
> {
    Variable(V),
    Integer(I),
    Float(F),
    String(S),
    Boolean(B),
    Null(N),
    Enum(E),
    List(L),
    Object(O),
}

impl<
        const CONST: bool,
        V: Variable,
        I: IntegerValue,
        F: FloatValue,
        S: StringValue,
        B: BooleanValue,
        N,
        E: EnumValue,
        L: ListValue<Self>,
        O: ObjectValue<Self>,
    > AsRef<Value<CONST, V, I, F, S, B, N, E, L, O>> for Value<CONST, V, I, F, S, B, N, E, L, O>
{
    fn as_ref(&self) -> &Value<CONST, V, I, F, S, B, N, E, L, O> {
        self
    }
}

impl<
        const CONST: bool,
        V: Variable,
        I: IntegerValue,
        F: FloatValue,
        S: StringValue,
        B: BooleanValue,
        N,
        E: EnumValue,
        L: ListValue<Self>,
        O: ObjectValue<Self>,
    > std::cmp::PartialEq for Value<CONST, V, I, F, S, B, N, E, L, O>
{
    fn eq(&self, other: &Self) -> bool {
        match self {
            Self::Variable(v) => {
                matches!(other, Self::Variable(other_v) if v.name() == other_v.name())
            }
            Self::Integer(i) => {
                matches!(other, Self::Integer(other_i) if i.to_i32() == other_i.to_i32())
            }
            Self::Float(f) => {
                matches!(other, Self::Float(other_f) if f.to_f64() == other_f.to_f64())
            }
            Self::String(s) => {
                matches!(other, Self::String(other_s) if s.as_ref() == other_s.as_ref())
            }
            Self::Boolean(b) => {
                matches!(other, Self::Boolean(other_b) if b.to_bool() == other_b.to_bool())
            }
            Self::Null(_) => matches!(other, Self::Null(_)),
            Self::Enum(e) => matches!(other, Self::Enum(other_e) if e.as_ref() == other_e.as_ref()),
            Self::List(l) => matches!(other, Self::List(other_l) if l.as_ref() == other_l.as_ref()),
            Self::Object(o) => matches!(other, Self::Object(other_o) if {
                let lhs: HashMap<&str, &Self> = HashMap::from_iter(o.iter());
                let rhs: HashMap<&str, &Self> = HashMap::from_iter(other_o.iter());
                lhs == rhs
            }),
        }
    }
}

impl<
        const CONST: bool,
        V: Variable,
        I: IntegerValue,
        F: FloatValue,
        S: StringValue,
        B: BooleanValue,
        N,
        E: EnumValue,
        L: ListValue<Self>,
        O: ObjectValue<Self>,
    > AbstractValue<CONST> for Value<CONST, V, I, F, S, B, N, E, L, O>
{
    type Variable = V;
    type Integer = I;
    type Float = F;
    type String = S;
    type Boolean = B;
    type Null = N;
    type Enum = E;
    type List = L;
    type Object = O;
}

impl<T: AbstractValue<true>> AbstractConstValue for T {}
impl<T: AbstractValue<false>> AbstractVariableValue for T {}