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
use crate::ArrayValue;
use crate::Error;
use crate::FixnumValue;
use crate::HashValue;
use crate::ObjectValue;
use crate::StringValue;
use crate::SymbolValue;
use crate::TypedValueHandle;
use crate::UserDefinedValue;
use crate::Value;
use crate::ValueArena;
use crate::ValueHandle;
use crate::MAJOR_VERSION;
use crate::MINOR_VERSION;
use crate::VALUE_KIND_ARRAY;
use crate::VALUE_KIND_FALSE;
use crate::VALUE_KIND_FIXNUM;
use crate::VALUE_KIND_HASH;
use crate::VALUE_KIND_HASH_DEFAULT;
use crate::VALUE_KIND_INSTANCE_VARIABLES;
use crate::VALUE_KIND_NIL;
use crate::VALUE_KIND_OBJECT;
use crate::VALUE_KIND_OBJECT_LINK;
use crate::VALUE_KIND_STRING;
use crate::VALUE_KIND_SYMBOL;
use crate::VALUE_KIND_SYMBOL_LINK;
use crate::VALUE_KIND_TRUE;
use crate::VALUE_KIND_USER_DEFINED;
use std::io::Read;

#[derive(Debug)]
struct Loader<R> {
    reader: R,

    arena: ValueArena,

    symbol_links: Vec<TypedValueHandle<SymbolValue>>,
    object_links: Vec<ValueHandle>,
}

impl<R> Loader<R> {
    /// Make a new [`Loader`] around a reader.
    fn new(reader: R) -> Self {
        let arena = ValueArena::new();

        Self {
            reader,
            arena,
            symbol_links: Vec::new(),
            object_links: Vec::new(),
        }
    }
}

impl<R> Loader<R>
where
    R: Read,
{
    /// Read a byte
    fn read_byte(&mut self) -> Result<u8, Error> {
        let mut byte = 0;
        self.reader.read_exact(std::slice::from_mut(&mut byte))?;
        Ok(byte)
    }

    /// Read a byte string.
    ///
    /// A byte string is a fixnum length, then that number of bytes.
    fn read_byte_string(&mut self) -> Result<Vec<u8>, Error> {
        let len = self.read_fixnum_value()?;
        let len = usize::try_from(len).map_err(|error| Error::FixnumInvalidUSize { error })?;

        let mut value = vec![0; len];
        self.reader.read_exact(&mut value)?;

        Ok(value)
    }

    /// Read and validate the header.
    fn read_header(&mut self) -> Result<(), Error> {
        let major_version = self.read_byte()?;
        let minor_version = self.read_byte()?;

        if major_version != MAJOR_VERSION || minor_version > MINOR_VERSION {
            return Err(Error::InvalidVersion {
                major: major_version,
                minor: minor_version,
            });
        }

        Ok(())
    }

    /// Read a fixnum value
    fn read_fixnum_value(&mut self) -> Result<i32, Error> {
        let len = self.read_byte()?;
        if len == 0 {
            return Ok(0);
        }
        let positive = (len as i8) > 0;
        let byte = len;

        if positive {
            if byte > 4 {
                return Ok(i32::from(byte) - 5);
            }

            if usize::from(byte) > std::mem::size_of::<i32>() {
                return Err(Error::InvalidFixnumSize { size: byte });
            }

            let mut n: i32 = 0;
            for i in 0..byte {
                let byte = self.read_byte()?;
                n |= i32::from(byte) << (i * 8);
            }

            Ok(n)
        } else {
            if (byte as i8) < -4 {
                return Ok(i32::from(byte as i8) + 5);
            }

            let byte = -(byte as i8) as u8;
            if usize::from(byte) > std::mem::size_of::<i32>() {
                return Err(Error::InvalidFixnumSize { size: byte });
            }

            let mut n: i32 = -1;
            for i in 0..byte {
                n &= !(0xFF_i32 << (i * 8));
                n |= i32::from(self.read_byte()?) << (i * 8);
            }

            Ok(n)
        }
    }

    /// Read a fixnum.
    fn read_fixnum(&mut self) -> Result<TypedValueHandle<FixnumValue>, Error> {
        let value = self.read_fixnum_value()?;
        Ok(self.arena.create_fixnum(value))
    }

    /// Read a symbol.
    fn read_symbol(&mut self) -> Result<TypedValueHandle<SymbolValue>, Error> {
        let symbol = self.read_byte_string()?;
        let handle = self.arena.create_symbol(symbol);

        self.symbol_links.push(handle);

        Ok(handle)
    }

    /// Read a symbol link.
    fn read_symbol_link(&mut self) -> Result<TypedValueHandle<SymbolValue>, Error> {
        let index = self.read_fixnum_value()?;
        let index = usize::try_from(index).map_err(|error| Error::FixnumInvalidUSize { error })?;

        let value = self
            .symbol_links
            .get(index)
            .ok_or(Error::MissingSymbolLink { index })?;

        Ok(*value)
    }

    /// Read an object link
    fn read_object_link(&mut self) -> Result<ValueHandle, Error> {
        let index = self.read_fixnum_value()?;
        let index = usize::try_from(index).map_err(|error| Error::FixnumInvalidUSize { error })?;

        let value = self
            .object_links
            .get(index)
            .ok_or(Error::MissingObjectLink { index })?;

        Ok(*value)
    }

    /// Read instance variables.
    fn read_instance_variables(
        &mut self,
    ) -> Result<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>, Error> {
        let num_pairs = self.read_fixnum_value()?;
        let num_pairs =
            usize::try_from(num_pairs).map_err(|error| Error::FixnumInvalidUSize { error })?;

        // TODO: Consider making this a map.
        let mut instance_variables = Vec::with_capacity(num_pairs);
        for _ in 0..num_pairs {
            let symbol = self.read_value_symbol_like()?;
            let value = self.read_value()?;

            instance_variables.push((symbol, value));
        }

        Ok(instance_variables)
    }

    /// Read an array
    fn read_array(&mut self) -> Result<TypedValueHandle<ArrayValue>, Error> {
        let handle = self.arena.create_nil().into_raw();
        self.object_links.push(handle);

        let len = self.read_fixnum_value()?;
        let len = usize::try_from(len).map_err(|error| Error::FixnumInvalidUSize { error })?;
        let mut array_value = Vec::with_capacity(len);

        for _ in 0..len {
            let value = self.read_value()?;
            array_value.push(value);
        }

        *self.arena.get_mut(handle).unwrap() = ArrayValue::new(array_value).into();

        Ok(TypedValueHandle::new_unchecked(handle))
    }

    /// Read a hash.
    fn read_hash(&mut self, has_default_value: bool) -> Result<TypedValueHandle<HashValue>, Error> {
        let handle = self.arena.create_nil().into_raw();
        self.object_links.push(handle);

        let num_pairs = self.read_fixnum_value()?;
        let num_pairs =
            usize::try_from(num_pairs).map_err(|error| Error::FixnumInvalidUSize { error })?;

        // TODO: Consider making this a map.
        let mut pairs = Vec::with_capacity(num_pairs);
        for _ in 0..num_pairs {
            let key = self.read_value()?;
            let value = self.read_value()?;

            pairs.push((key, value));
        }

        let default_value = if has_default_value {
            Some(self.read_value()?)
        } else {
            None
        };

        *self.arena.get_mut(handle).unwrap() = HashValue::new(pairs, default_value).into();

        Ok(TypedValueHandle::new_unchecked(handle))
    }

    /// Read an object
    fn read_object(&mut self) -> Result<TypedValueHandle<ObjectValue>, Error> {
        let handle = self.arena.create_nil().into_raw();
        self.object_links.push(handle);

        let name = self.read_value_symbol_like()?;
        let instance_variables = self.read_instance_variables()?;

        *self.arena.get_mut(handle).unwrap() = ObjectValue::new(name, instance_variables).into();

        Ok(TypedValueHandle::new_unchecked(handle))
    }

    /// Read a string
    fn read_string(&mut self) -> Result<TypedValueHandle<StringValue>, Error> {
        let data = self.read_byte_string()?;

        let handle = self.arena.create_string(data);
        self.object_links.push(handle.into());

        Ok(handle)
    }

    /// Read a user defined
    fn read_user_defined(&mut self) -> Result<TypedValueHandle<UserDefinedValue>, Error> {
        let name = self.read_value_symbol_like()?;
        let value = self.read_byte_string()?;

        let handle = self.arena.create_user_defined(name, value);
        self.object_links.push(handle.into());

        Ok(handle)
    }

    /// Read the next value, failing if it is not a symbol-like value.
    fn read_value_symbol_like(&mut self) -> Result<TypedValueHandle<SymbolValue>, Error> {
        let kind = self.read_byte()?;
        match kind {
            VALUE_KIND_SYMBOL => self.read_symbol(),
            VALUE_KIND_SYMBOL_LINK => self.read_symbol_link(),
            _ => Err(Error::UnexpectedValueKind {
                expected: VALUE_KIND_SYMBOL,
                actual: kind,
            }),
        }
    }

    /// Read the next value.
    fn read_value(&mut self) -> Result<ValueHandle, Error> {
        let kind = self.read_byte()?;
        match kind {
            VALUE_KIND_NIL => Ok(self.arena.create_nil().into()),
            VALUE_KIND_TRUE => Ok(self.arena.create_bool(true).into()),
            VALUE_KIND_FALSE => Ok(self.arena.create_bool(false).into()),
            VALUE_KIND_FIXNUM => Ok(self.read_fixnum()?.into()),
            VALUE_KIND_SYMBOL => Ok(self.read_symbol()?.into()),
            VALUE_KIND_SYMBOL_LINK => Ok(self.read_symbol_link()?.into()),
            VALUE_KIND_OBJECT_LINK => Ok(self.read_object_link()?),
            VALUE_KIND_INSTANCE_VARIABLES => {
                let value = self.read_value()?;

                let instance_variables = self.read_instance_variables()?;

                match self
                    .arena
                    .get_mut(value)
                    .ok_or(Error::InvalidValueHandle { handle: value })?
                {
                    Value::String(value) => {
                        value.set_instance_variables(Some(instance_variables));
                    }
                    Value::UserDefined(value) => {
                        value.set_instance_variables(Some(instance_variables));
                    }
                    _ => return Err(Error::NotAnObject),
                }

                Ok(value)
            }
            VALUE_KIND_ARRAY => Ok(self.read_array()?.into()),
            VALUE_KIND_HASH => Ok(self.read_hash(false)?.into()),
            VALUE_KIND_HASH_DEFAULT => Ok(self.read_hash(true)?.into()),
            VALUE_KIND_OBJECT => Ok(self.read_object()?.into()),
            VALUE_KIND_STRING => Ok(self.read_string()?.into()),
            VALUE_KIND_USER_DEFINED => Ok(self.read_user_defined()?.into()),
            _ => Err(Error::InvalidValueKind { kind }),
        }
    }

    /// Load from the reader and get the value.
    fn load(mut self) -> Result<ValueArena, Error> {
        self.read_header()?;
        let root = self.read_value()?;
        let _old_root = self.arena.replace_root(root);

        // TODO: Delete old root.

        Ok(self.arena)
    }
}

/// Load from a reader.
pub fn load<R>(reader: R) -> Result<ValueArena, Error>
where
    R: Read,
{
    let loader = Loader::new(reader);
    let value_arena = loader.load()?;

    Ok(value_arena)
}