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
371
372
373
use crate::TypedValueHandle;
use crate::ValueHandle;

/// A Ruby Value
#[derive(Debug)]
pub enum Value {
    /// Nil
    Nil(NilValue),

    /// A Bool
    Bool(BoolValue),

    /// A Fixnum
    Fixnum(FixnumValue),

    /// A Symbol
    Symbol(SymbolValue),

    /// An Array
    Array(ArrayValue),

    /// A hash value
    Hash(HashValue),

    /// An Object
    Object(ObjectValue),

    /// A String
    String(StringValue),

    /// A User Defined Value
    UserDefined(UserDefinedValue),
}

impl Value {
    /// Get a ref to the [`SymbolValue`], if it is a symbol.
    pub fn as_symbol(&self) -> Option<&SymbolValue> {
        match self {
            Self::Symbol(value) => Some(value),
            _ => None,
        }
    }

    /// Get a ref to the [`ObjectValue`], if it is an object.
    pub fn as_object(&self) -> Option<&ObjectValue> {
        match self {
            Self::Object(value) => Some(value),
            _ => None,
        }
    }

    /// Get a ref to the [`StringValue`], if it is a string.
    pub fn as_string(&self) -> Option<&StringValue> {
        match self {
            Self::String(value) => Some(value),
            _ => None,
        }
    }

    /// Get the kind of value.
    pub fn kind(&self) -> ValueKind {
        match self {
            Self::Nil(_) => ValueKind::Nil,
            Self::Bool(_) => ValueKind::Bool,
            Self::Fixnum(_) => ValueKind::Fixnum,
            Self::Symbol(_) => ValueKind::Symbol,
            Self::Array(_) => ValueKind::Array,
            Self::Hash(_) => ValueKind::Hash,
            Self::Object(_) => ValueKind::Object,
            Self::String(_) => ValueKind::String,
            Self::UserDefined(_) => ValueKind::UserDefined,
        }
    }
}

impl From<NilValue> for Value {
    fn from(value: NilValue) -> Self {
        Self::Nil(value)
    }
}

impl From<BoolValue> for Value {
    fn from(value: BoolValue) -> Self {
        Self::Bool(value)
    }
}

impl From<FixnumValue> for Value {
    fn from(value: FixnumValue) -> Self {
        Self::Fixnum(value)
    }
}

impl From<SymbolValue> for Value {
    fn from(value: SymbolValue) -> Self {
        Self::Symbol(value)
    }
}

impl From<ArrayValue> for Value {
    fn from(value: ArrayValue) -> Self {
        Self::Array(value)
    }
}

impl From<HashValue> for Value {
    fn from(value: HashValue) -> Self {
        Self::Hash(value)
    }
}

impl From<ObjectValue> for Value {
    fn from(value: ObjectValue) -> Self {
        Self::Object(value)
    }
}

impl From<StringValue> for Value {
    fn from(value: StringValue) -> Self {
        Self::String(value)
    }
}

impl From<UserDefinedValue> for Value {
    fn from(value: UserDefinedValue) -> Self {
        Self::UserDefined(value)
    }
}

/// A Nil value.
#[derive(Debug)]
pub struct NilValue;

/// A bool value.
#[derive(Debug, Copy, Clone)]
pub struct BoolValue {
    value: bool,
}

impl BoolValue {
    /// Create a new [`BoolValue`].
    pub(super) fn new(value: bool) -> Self {
        Self { value }
    }

    /// Get the inner value
    pub fn value(self) -> bool {
        self.value
    }
}

/// A Fixnum Value
#[derive(Debug, Copy, Clone)]
pub struct FixnumValue {
    value: i32,
}

impl FixnumValue {
    /// Create a new [`FixnumValue`].
    pub(super) fn new(value: i32) -> Self {
        Self { value }
    }

    /// Get the inner value
    pub fn value(self) -> i32 {
        self.value
    }
}

/// A Symbol
#[derive(Debug)]
pub struct SymbolValue {
    value: Vec<u8>,
}

impl SymbolValue {
    /// Create a new [`SymbolValue`].
    pub(super) fn new(value: Vec<u8>) -> Self {
        Self { value }
    }

    /// Get the inner value.
    pub fn value(&self) -> &[u8] {
        &self.value
    }
}

/// An Array
#[derive(Debug)]
pub struct ArrayValue {
    value: Vec<ValueHandle>,
}

impl ArrayValue {
    /// Create a new [`Array`].
    pub(crate) fn new(value: Vec<ValueHandle>) -> Self {
        Self { value }
    }

    /// Get the inner value.
    pub fn value(&self) -> &[ValueHandle] {
        &self.value
    }

    /// Get the number of elements in the array
    pub fn len(&self) -> usize {
        self.value.len()
    }

    /// Check if this is empty
    pub fn is_empty(&self) -> bool {
        self.value.is_empty()
    }
}

/// A Hash
#[derive(Debug)]
pub struct HashValue {
    value: Vec<(ValueHandle, ValueHandle)>,
    default_value: Option<ValueHandle>,
}

impl HashValue {
    /// Create a new [`HashValue`].
    pub(crate) fn new(
        value: Vec<(ValueHandle, ValueHandle)>,
        default_value: Option<ValueHandle>,
    ) -> Self {
        Self {
            value,
            default_value,
        }
    }

    /// Get the inner value.
    pub fn value(&self) -> &[(ValueHandle, ValueHandle)] {
        &self.value
    }

    /// Get the default value.
    pub fn default_value(&self) -> Option<ValueHandle> {
        self.default_value
    }
}

/// An object
#[derive(Debug)]
pub struct ObjectValue {
    name: TypedValueHandle<SymbolValue>,
    instance_variables: Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>,
}

impl ObjectValue {
    /// Create a new [`ObjectValue`].
    pub(crate) fn new(
        name: TypedValueHandle<SymbolValue>,
        instance_variables: Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>,
    ) -> Self {
        Self {
            name,
            instance_variables,
        }
    }

    /// Get the name.
    pub fn name(&self) -> TypedValueHandle<SymbolValue> {
        self.name
    }

    /// Get the instance variables
    pub fn instance_variables(&self) -> &[(TypedValueHandle<SymbolValue>, ValueHandle)] {
        &self.instance_variables
    }
}

/// A String
#[derive(Debug)]
pub struct StringValue {
    value: Vec<u8>,
    instance_variables: Option<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>>,
}

impl StringValue {
    /// Create a new [`String`].
    pub(crate) fn new(value: Vec<u8>) -> Self {
        Self {
            value,
            instance_variables: None,
        }
    }

    /// Get the inner value.
    pub fn value(&self) -> &[u8] {
        &self.value
    }

    /// Get the instance variables
    pub fn instance_variables(&self) -> Option<&[(TypedValueHandle<SymbolValue>, ValueHandle)]> {
        self.instance_variables.as_deref()
    }

    /// Set the instance variables.
    ///
    /// # Returns
    /// Returns the old instance variables
    pub(crate) fn set_instance_variables(
        &mut self,
        mut instance_variables: Option<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>>,
    ) -> Option<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>> {
        std::mem::swap(&mut self.instance_variables, &mut instance_variables);
        instance_variables
    }
}

/// A User Defined value
#[derive(Debug)]
pub struct UserDefinedValue {
    name: TypedValueHandle<SymbolValue>,
    value: Vec<u8>,
    instance_variables: Option<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>>,
}

impl UserDefinedValue {
    /// Create a new [`UserDefinedValue`].
    pub(crate) fn new(name: TypedValueHandle<SymbolValue>, value: Vec<u8>) -> Self {
        Self {
            name,
            value,
            instance_variables: None,
        }
    }

    /// Get the name.
    pub fn name(&self) -> TypedValueHandle<SymbolValue> {
        self.name
    }

    /// Get the inner value.
    pub fn value(&self) -> &[u8] {
        &self.value
    }

    /// Get the instance variables
    pub fn instance_variables(&self) -> Option<&[(TypedValueHandle<SymbolValue>, ValueHandle)]> {
        self.instance_variables.as_deref()
    }

    /// Set the instance variables.
    ///
    /// # Returns
    /// Returns the old instance variables
    pub(crate) fn set_instance_variables(
        &mut self,
        mut instance_variables: Option<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>>,
    ) -> Option<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>> {
        std::mem::swap(&mut self.instance_variables, &mut instance_variables);
        instance_variables
    }
}

/// The kind of value
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum ValueKind {
    Nil,
    Bool,
    Fixnum,
    Symbol,
    Array,
    Hash,
    Object,
    String,
    UserDefined,
}