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
use crate::internal::encodings::varint::*;
use crate::prelude::*;
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
use std::fmt;
use std::ops::Deref;

/// This wrapper is just to make the Debug impl not write every byte
pub struct Bytes<'a>(&'a [u8]);

impl fmt::Debug for Bytes<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("Bytes").field(&self.0.len()).finish()
    }
}

impl<'a> From<&'a [u8]> for Bytes<'a> {
    #[inline]
    fn from(value: &'a [u8]) -> Self {
        Bytes(value)
    }
}

impl Deref for Bytes<'_> {
    type Target = [u8];
    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[derive(Debug)]
pub enum ArrayFloat<'a> {
    F64(Bytes<'a>),
    F32(Bytes<'a>),
    DoubleGorilla(Bytes<'a>),
}

#[derive(Debug)]
pub struct ArrayEnumVariant<'a> {
    pub ident: Ident<'a>,
    pub data: DynArrayBranch<'a>,
}

#[derive(Debug)]
pub enum DynArrayBranch<'a> {
    Object {
        fields: HashMap<Ident<'a>, DynArrayBranch<'a>>,
    },
    Tuple {
        fields: Vec<DynArrayBranch<'a>>,
    },
    Array0,
    Array {
        len: Box<DynArrayBranch<'a>>,
        values: Box<DynArrayBranch<'a>>,
    },
    Map0,
    Map {
        len: Box<DynArrayBranch<'a>>,
        keys: Box<DynArrayBranch<'a>>,
        values: Box<DynArrayBranch<'a>>,
    },
    Integer(ArrayInteger<'a>),
    Nullable {
        opt: Bytes<'a>,
        values: Box<DynArrayBranch<'a>>,
    },
    Boolean(Bytes<'a>),
    Float(ArrayFloat<'a>),
    Void,
    String(Bytes<'a>),
    Enum {
        discriminants: Box<DynArrayBranch<'a>>,
        variants: Vec<ArrayEnumVariant<'a>>,
    },
    // TODO:
    // In any array context, we can have a 'dynamic' value, which resolves to an array of DynRootBranch (like a nested file)
    // This generally should not be used, but the existance of it is an escape hatch bringing the capability to use truly unstructured
    // data when necessary. // TODO: The hard-line appraoch would be to enforce the use of enum instead.
    // Dynamic(Bytes<'a>),
}

pub fn read_next_array<'a>(bytes: &'a [u8], offset: &'_ mut usize, lens: &'_ mut usize) -> ReadResult<DynArrayBranch<'a>> {
    let id = ArrayTypeId::read_next(bytes, offset)?;

    use ArrayTypeId::*;

    fn read_ints<'a>(bytes: &'a [u8], offset: &'_ mut usize, lens: &'_ mut usize, encoding: ArrayIntegerEncoding) -> ReadResult<DynArrayBranch<'a>> {
        let bytes = read_bytes_from_len(bytes, offset, lens)?.into();
        Ok(DynArrayBranch::Integer(ArrayInteger { bytes, encoding }))
    }

    fn read_bytes_from_len<'a>(bytes: &'a [u8], offset: &'_ mut usize, lens: &'_ mut usize) -> ReadResult<Bytes<'a>> {
        let len = decode_suffix_varint(bytes, lens)?;
        Ok(read_bytes(len as usize, bytes, offset)?.into())
    }

    // See also e25db64d-8424-46b9-bdc1-cdb618807513
    fn read_tuple<'a>(num_fields: usize, bytes: &'a [u8], offset: &'_ mut usize, lens: &'_ mut usize) -> ReadResult<DynArrayBranch<'a>> {
        let mut fields = Vec::with_capacity(num_fields);
        for _ in 0..num_fields {
            let child = read_next_array(bytes, offset, lens)?;
            fields.push(child);
        }
        Ok(DynArrayBranch::Tuple { fields })
    }

    // See also 47a1482f-5ce3-4b78-b356-30c66dc60cda
    fn read_obj<'a>(num_fields: usize, bytes: &'a [u8], offset: &'_ mut usize, lens: &'_ mut usize) -> ReadResult<DynArrayBranch<'a>> {
        let mut fields = HashMap::with_capacity(num_fields);
        for _ in 0..num_fields {
            let name = crate::internal::read_ident(bytes, offset)?;
            let child = read_next_array(bytes, offset, lens)?;
            fields.insert(name, child);
        }
        Ok(DynArrayBranch::Object { fields })
    }

    let branch = match id {
        Nullable => {
            let opt = read_bytes_from_len(bytes, offset, lens)?;
            let values = read_next_array(bytes, offset, lens)?;
            let values = Box::new(values);
            DynArrayBranch::Nullable { opt, values }
        }
        Void => DynArrayBranch::Void,
        Tuple2 => read_tuple(2, bytes, offset, lens)?,
        Tuple3 => read_tuple(3, bytes, offset, lens)?,
        Tuple4 => read_tuple(4, bytes, offset, lens)?,
        Tuple5 => read_tuple(5, bytes, offset, lens)?,
        Tuple6 => read_tuple(6, bytes, offset, lens)?,
        Tuple7 => read_tuple(7, bytes, offset, lens)?,
        Tuple8 => read_tuple(8, bytes, offset, lens)?,
        TupleN => read_tuple(decode_prefix_varint(bytes, offset)? as usize + 9, bytes, offset, lens)?,
        ArrayVar => {
            let len = read_next_array(bytes, offset, lens)?;
            match len {
                DynArrayBranch::Void => DynArrayBranch::Array0,
                _ => {
                    // FIXME: Verify that len is Integer here. If not, the file is invalid.
                    // This may not be verified later if the schema is selectively matched.
                    let len = Box::new(len);
                    let values = read_next_array(bytes, offset, lens)?;
                    let values = Box::new(values);
                    DynArrayBranch::Array { len, values }
                }
            }
        }
        Map => {
            let len = read_next_array(bytes, offset, lens)?;
            match len {
                DynArrayBranch::Void => DynArrayBranch::Map0,
                _ => {
                    // FIXME: Verify that len is Integer here. If not, the file is invalid.
                    // This may not be verified later if the schema is selectively matched.
                    let len = Box::new(len);
                    let keys = read_next_array(bytes, offset, lens)?;
                    let keys = Box::new(keys);
                    let values = read_next_array(bytes, offset, lens)?;
                    let values = Box::new(values);
                    DynArrayBranch::Map { len, keys, values }
                }
            }
        }

        // See also: fadaec14-35ad-4dc1-b6dc-6106ab811669
        Obj0 => read_obj(0, bytes, offset, lens)?,
        Obj1 => read_obj(1, bytes, offset, lens)?,
        Obj2 => read_obj(2, bytes, offset, lens)?,
        Obj3 => read_obj(3, bytes, offset, lens)?,
        Obj4 => read_obj(4, bytes, offset, lens)?,
        Obj5 => read_obj(5, bytes, offset, lens)?,
        Obj6 => read_obj(6, bytes, offset, lens)?,
        Obj7 => read_obj(7, bytes, offset, lens)?,
        Obj8 => read_obj(8, bytes, offset, lens)?,
        ObjN => read_obj(decode_prefix_varint(bytes, offset)? as usize + 9, bytes, offset, lens)?,
        Boolean => {
            let bytes = read_bytes_from_len(bytes, offset, lens)?;
            DynArrayBranch::Boolean(bytes)
        }
        IntSimple16 => read_ints(bytes, offset, lens, ArrayIntegerEncoding::Simple16)?,
        IntPrefixVar => read_ints(bytes, offset, lens, ArrayIntegerEncoding::PrefixVarInt)?,
        F32 => {
            let bytes = read_bytes_from_len(bytes, offset, lens)?;
            DynArrayBranch::Float(ArrayFloat::F32(bytes))
        }
        F64 => {
            let bytes = read_bytes_from_len(bytes, offset, lens)?;
            DynArrayBranch::Float(ArrayFloat::F64(bytes))
        }
        Utf8 => {
            let bytes = read_bytes_from_len(bytes, offset, lens)?;
            DynArrayBranch::String(bytes)
        }
        DoubleGorilla => {
            let bytes = read_bytes_from_len(bytes, offset, lens)?;
            DynArrayBranch::Float(ArrayFloat::DoubleGorilla(bytes))
        }
        Enum => {
            let count = decode_prefix_varint(bytes, offset)? as usize;
            let mut variants = Vec::with_capacity(count);

            // TODO: Elide discriminants when there are 0 or 1 variants
            let discriminants = read_next_array(bytes, offset, lens)?.into();

            if count != 0 {
                for _ in 0..count {
                    variants.push(ArrayEnumVariant {
                        ident: read_ident(bytes, offset)?,
                        data: read_next_array(bytes, offset, lens)?,
                    });
                }
            }

            DynArrayBranch::Enum { discriminants, variants }
        }
    };

    Ok(branch)
}

impl<'a> Default for DynArrayBranch<'a> {
    fn default() -> Self {
        DynArrayBranch::Void
    }
}

impl_type_id!(ArrayTypeId, [
    Nullable: 1,
    ArrayVar: 2,
    Boolean: 3,
    IntSimple16: 4,
    IntPrefixVar: 5,
    F32: 6,
    F64: 7,
    Utf8: 8,
    DoubleGorilla: 9,
    Map: 10,
    Enum: 11,
]);

#[derive(Debug)]
pub struct ArrayInteger<'a> {
    pub bytes: Bytes<'a>,
    //delta: bool,
    //zigzag: bool,
    pub encoding: ArrayIntegerEncoding,
}

#[derive(Debug)]
pub enum ArrayIntegerEncoding {
    PrefixVarInt,
    Simple16,
}