frdt 0.2.4

Edit and inspect Resonite FrDT (.brson) documents without expanding them to JSON
Documentation
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//! Length-prefixed BSON cursor, encoder, and in-place splice.
//!
//! Documents and arrays are skipped from their leading int32. Leaf strings and
//! binaries are copied as byte ranges. Doubles are raw IEEE-754 bits.

use crate::error::{Error, Result};
use crate::value::{Float3, FloatQ, Value};

pub const DOUBLE: u8 = 0x01;
pub const STRING: u8 = 0x02;
pub const DOCUMENT: u8 = 0x03;
pub const ARRAY: u8 = 0x04;
pub const BINARY: u8 = 0x05;
pub const BOOL: u8 = 0x08;
pub const NULL: u8 = 0x0A;
pub const INT32: u8 = 0x10;
pub const INT64: u8 = 0x12;

/// One BSON element inside a document or array.
#[derive(Debug, Clone, Copy)]
pub struct Element<'a> {
    pub ty: u8,
    pub key: &'a str,
    /// Absolute offset of the type byte.
    pub start: usize,
    /// Absolute offset of the value (after the key's trailing NUL).
    pub value_at: usize,
    pub value: &'a [u8],
    /// Type byte through the end of the value.
    pub raw: &'a [u8],
}

/// Iterate the elements of the document or array that starts at `doc_off`.
pub fn elements(buf: &[u8], doc_off: usize) -> Result<Vec<Element<'_>>> {
    let end = doc_end(buf, doc_off)?;
    let mut pos = doc_off + 4;
    let mut out = Vec::new();
    while pos < end - 1 {
        let ty = buf[pos];
        let key_start = pos + 1;
        let key_nul = find_nul(buf, key_start, end)?;
        let key = std::str::from_utf8(&buf[key_start..key_nul])
            .map_err(|_| Error::Invalid("field name is not utf-8".into()))?;
        let value_at = key_nul + 1;
        let value_len = value_len(buf, ty, value_at, end)?;
        let value_end = value_at + value_len;
        if value_end > end - 1 {
            return Err(Error::Truncated);
        }
        out.push(Element {
            ty,
            key,
            start: pos,
            value_at,
            value: &buf[value_at..value_end],
            raw: &buf[pos..value_end],
        });
        pos = value_end;
    }
    if pos != end - 1 || buf[end - 1] != 0 {
        return Err(Error::Invalid(
            "document is missing its trailing NUL".into(),
        ));
    }
    Ok(out)
}

pub fn doc_len(buf: &[u8], doc_off: usize) -> Result<usize> {
    if doc_off + 4 > buf.len() {
        return Err(Error::Truncated);
    }
    let len = i32::from_le_bytes(buf[doc_off..doc_off + 4].try_into().unwrap());
    if len < 5 {
        return Err(Error::Invalid(format!("document length {len}")));
    }
    Ok(len as usize)
}

pub fn doc_end(buf: &[u8], doc_off: usize) -> Result<usize> {
    let len = doc_len(buf, doc_off)?;
    let end = doc_off + len;
    if end > buf.len() {
        return Err(Error::Truncated);
    }
    Ok(end)
}

fn find_nul(buf: &[u8], start: usize, limit: usize) -> Result<usize> {
    buf[start..limit]
        .iter()
        .position(|b| *b == 0)
        .map(|i| start + i)
        .ok_or(Error::Truncated)
}

pub fn value_len(buf: &[u8], ty: u8, value_at: usize, limit: usize) -> Result<usize> {
    let need = |n: usize| -> Result<()> {
        if value_at + n > limit {
            Err(Error::Truncated)
        } else {
            Ok(())
        }
    };
    Ok(match ty {
        DOUBLE => {
            need(8)?;
            8
        }
        STRING => {
            need(4)?;
            let n = i32::from_le_bytes(buf[value_at..value_at + 4].try_into().unwrap());
            if n < 1 {
                return Err(Error::Invalid("string length".into()));
            }
            let total = 4 + n as usize;
            need(total)?;
            if buf[value_at + total - 1] != 0 {
                return Err(Error::Invalid("string is missing NUL".into()));
            }
            total
        }
        DOCUMENT | ARRAY => {
            need(4)?;
            let n = i32::from_le_bytes(buf[value_at..value_at + 4].try_into().unwrap());
            if n < 5 {
                return Err(Error::Invalid("nested document length".into()));
            }
            let total = n as usize;
            need(total)?;
            total
        }
        BINARY => {
            need(5)?;
            let n = i32::from_le_bytes(buf[value_at..value_at + 4].try_into().unwrap());
            if n < 0 {
                return Err(Error::Invalid("binary length".into()));
            }
            let total = 5 + n as usize;
            need(total)?;
            total
        }
        BOOL => {
            need(1)?;
            1
        }
        NULL => 0,
        INT32 => {
            need(4)?;
            4
        }
        INT64 => {
            need(8)?;
            8
        }
        other => return Err(Error::UnsupportedType(other)),
    })
}

pub fn decode_string(value: &[u8]) -> Result<&str> {
    if value.len() < 5 {
        return Err(Error::Truncated);
    }
    let n = i32::from_le_bytes(value[0..4].try_into().unwrap()) as usize;
    if n + 4 != value.len() || n == 0 || value[value.len() - 1] != 0 {
        return Err(Error::Invalid("string value".into()));
    }
    std::str::from_utf8(&value[4..value.len() - 1])
        .map_err(|_| Error::Invalid("string is not utf-8".into()))
}

pub fn decode_f64(value: &[u8]) -> Result<f64> {
    if value.len() != 8 {
        return Err(Error::Invalid("double".into()));
    }
    Ok(f64::from_le_bytes(value.try_into().unwrap()))
}

pub fn decode_i32(value: &[u8]) -> Result<i32> {
    if value.len() != 4 {
        return Err(Error::Invalid("int32".into()));
    }
    Ok(i32::from_le_bytes(value.try_into().unwrap()))
}

pub fn decode_i64(value: &[u8]) -> Result<i64> {
    if value.len() != 8 {
        return Err(Error::Invalid("int64".into()));
    }
    Ok(i64::from_le_bytes(value.try_into().unwrap()))
}

pub fn decode_bool(value: &[u8]) -> Result<bool> {
    match value {
        [0] => Ok(false),
        [1] => Ok(true),
        _ => Err(Error::Invalid("bool".into())),
    }
}

pub fn decode_int(value: &[u8], ty: u8) -> Result<i64> {
    match ty {
        INT32 => Ok(decode_i32(value)? as i64),
        INT64 => decode_i64(value),
        DOUBLE => Ok(decode_f64(value)? as i64),
        _ => Err(Error::Invalid("expected an integer".into())),
    }
}

/// Replace `buf[start..end]` and add the size delta to each document length
/// field. Length fields must not overlap the replaced range. A length field
/// that begins at or after `end` is shifted by the delta.
pub fn splice(
    buf: &mut Vec<u8>,
    start: usize,
    end: usize,
    new_bytes: &[u8],
    length_offs: &[usize],
) -> Result<()> {
    if start > end || end > buf.len() {
        return Err(Error::Invalid("splice range".into()));
    }
    let old_len = end - start;
    let delta_i64 = new_bytes.len() as i64 - old_len as i64;
    let delta = i32::try_from(delta_i64).map_err(|_| Error::TooLarge)?;
    for &off in length_offs {
        if off + 4 > buf.len() {
            return Err(Error::Truncated);
        }
        if off < end && off + 4 > start {
            return Err(Error::Invalid("length field overlaps splice".into()));
        }
    }
    buf.splice(start..end, new_bytes.iter().copied());
    for &off in length_offs {
        let adj = if off >= end {
            usize::try_from(off as i64 + delta_i64)
                .map_err(|_| Error::Invalid("length offset".into()))?
        } else {
            off
        };
        if adj + 4 > buf.len() {
            return Err(Error::Truncated);
        }
        let cur = i32::from_le_bytes(buf[adj..adj + 4].try_into().unwrap());
        let next = cur.checked_add(delta).ok_or(Error::TooLarge)?;
        if next < 5 {
            return Err(Error::Invalid("document length became too small".into()));
        }
        buf[adj..adj + 4].copy_from_slice(&next.to_le_bytes());
    }
    Ok(())
}

pub fn write_cstring(out: &mut Vec<u8>, key: &str) {
    out.extend_from_slice(key.as_bytes());
    out.push(0);
}

pub fn write_string_value(out: &mut Vec<u8>, value: &str) {
    let n = i32::try_from(value.len() + 1).expect("string fits in i32");
    out.extend_from_slice(&n.to_le_bytes());
    out.extend_from_slice(value.as_bytes());
    out.push(0);
}

pub fn push_element(out: &mut Vec<u8>, ty: u8, key: &str, value: &[u8]) {
    out.push(ty);
    write_cstring(out, key);
    out.extend_from_slice(value);
}

pub fn string_value(value: &str) -> Vec<u8> {
    let mut out = Vec::new();
    write_string_value(&mut out, value);
    out
}

pub fn f64_value(value: f64) -> [u8; 8] {
    value.to_le_bytes()
}

pub fn i32_value(value: i32) -> [u8; 4] {
    value.to_le_bytes()
}

pub fn i64_value(value: i64) -> [u8; 8] {
    value.to_le_bytes()
}

pub fn finish_doc(body: &[u8]) -> Result<Vec<u8>> {
    let len = i32::try_from(body.len() + 5).map_err(|_| Error::TooLarge)?;
    let mut out = Vec::with_capacity(len as usize);
    out.extend_from_slice(&len.to_le_bytes());
    out.extend_from_slice(body);
    out.push(0);
    Ok(out)
}

/// Encode a [`Value`] as the raw BSON value bytes (no type tag, no key).
pub fn encode_value(value: &Value) -> Result<Vec<u8>> {
    let mut out = Vec::new();
    match value {
        Value::F64(v) => out.extend_from_slice(&f64_value(*v)),
        Value::I64(v) => out.extend_from_slice(&i64_value(*v)),
        Value::Bool(v) => out.push(u8::from(*v)),
        Value::String(v) => write_string_value(&mut out, v),
        Value::Float3(v) => out.extend_from_slice(&encode_float3(*v)?),
        Value::FloatQ(v) => out.extend_from_slice(&encode_floatq(*v)?),
    }
    Ok(out)
}

pub fn value_type(value: &Value) -> u8 {
    match value {
        Value::F64(_) => DOUBLE,
        Value::Float3(_) | Value::FloatQ(_) => DOCUMENT,
        Value::String(_) => STRING,
        Value::I64(_) => INT64,
        Value::Bool(_) => BOOL,
    }
}

pub fn encode_float3(v: Float3) -> Result<Vec<u8>> {
    let mut body = Vec::new();
    push_element(&mut body, DOUBLE, "x", &f64_value(v.x));
    push_element(&mut body, DOUBLE, "y", &f64_value(v.y));
    push_element(&mut body, DOUBLE, "z", &f64_value(v.z));
    finish_doc(&body)
}

pub fn encode_floatq(v: FloatQ) -> Result<Vec<u8>> {
    let mut body = Vec::new();
    push_element(&mut body, DOUBLE, "x", &f64_value(v.x));
    push_element(&mut body, DOUBLE, "y", &f64_value(v.y));
    push_element(&mut body, DOUBLE, "z", &f64_value(v.z));
    push_element(&mut body, DOUBLE, "w", &f64_value(v.w));
    finish_doc(&body)
}

pub fn decode_float3(doc: &[u8]) -> Result<Float3> {
    let mut x = None;
    let mut y = None;
    let mut z = None;
    for el in elements(doc, 0)? {
        if el.ty != DOUBLE {
            continue;
        }
        let n = decode_f64(el.value)?;
        match el.key {
            "x" => x = Some(n),
            "y" => y = Some(n),
            "z" => z = Some(n),
            _ => {}
        }
    }
    match (x, y, z) {
        (Some(x), Some(y), Some(z)) => Ok(Float3 { x, y, z }),
        _ => Err(Error::Invalid("float3".into())),
    }
}

pub fn decode_floatq(doc: &[u8]) -> Result<FloatQ> {
    let mut x = None;
    let mut y = None;
    let mut z = None;
    let mut w = None;
    for el in elements(doc, 0)? {
        if el.ty != DOUBLE {
            continue;
        }
        let n = decode_f64(el.value)?;
        match el.key {
            "x" => x = Some(n),
            "y" => y = Some(n),
            "z" => z = Some(n),
            "w" => w = Some(n),
            _ => {}
        }
    }
    match (x, y, z, w) {
        (Some(x), Some(y), Some(z), Some(w)) => Ok(FloatQ { x, y, z, w }),
        _ => Err(Error::Invalid("floatQ".into())),
    }
}

/// Recursively parse every document and array. Unknown types and bad lengths fail.
pub fn validate(buf: &[u8]) -> Result<()> {
    fn walk(buf: &[u8], off: usize) -> Result<()> {
        for el in elements(buf, off)? {
            if el.ty == DOCUMENT || el.ty == ARRAY {
                walk(buf, el.value_at)?;
            }
        }
        Ok(())
    }
    if buf.is_empty() {
        return Err(Error::Truncated);
    }
    walk(buf, 0)?;
    let end = doc_end(buf, 0)?;
    if end != buf.len() {
        return Err(Error::Invalid("trailing bytes after root document".into()));
    }
    Ok(())
}

/// Incremental document builder used by the fixture generator.
#[derive(Debug, Default, Clone)]
pub struct DocBuf {
    body: Vec<u8>,
}

impl DocBuf {
    pub fn new() -> Self {
        Self { body: Vec::new() }
    }

    pub fn string(&mut self, key: &str, value: &str) -> &mut Self {
        push_element(&mut self.body, STRING, key, &string_value(value));
        self
    }

    pub fn double(&mut self, key: &str, value: f64) -> &mut Self {
        push_element(&mut self.body, DOUBLE, key, &f64_value(value));
        self
    }

    pub fn int32(&mut self, key: &str, value: i32) -> &mut Self {
        push_element(&mut self.body, INT32, key, &i32_value(value));
        self
    }

    pub fn int64(&mut self, key: &str, value: i64) -> &mut Self {
        push_element(&mut self.body, INT64, key, &i64_value(value));
        self
    }

    pub fn null(&mut self, key: &str) -> &mut Self {
        push_element(&mut self.body, NULL, key, &[]);
        self
    }

    pub fn bool(&mut self, key: &str, value: bool) -> &mut Self {
        push_element(&mut self.body, BOOL, key, &[u8::from(value)]);
        self
    }

    pub fn binary(&mut self, key: &str, subtype: u8, data: &[u8]) -> &mut Self {
        let n = i32::try_from(data.len()).expect("binary fits in i32");
        let mut value = Vec::with_capacity(5 + data.len());
        value.extend_from_slice(&n.to_le_bytes());
        value.push(subtype);
        value.extend_from_slice(data);
        push_element(&mut self.body, BINARY, key, &value);
        self
    }

    pub fn doc(&mut self, key: &str, child: &[u8]) -> &mut Self {
        push_element(&mut self.body, DOCUMENT, key, child);
        self
    }

    pub fn array(&mut self, key: &str, child: &[u8]) -> &mut Self {
        push_element(&mut self.body, ARRAY, key, child);
        self
    }

    pub fn finish(self) -> Vec<u8> {
        finish_doc(&self.body).expect("fixture document fits in i32")
    }
}

pub fn array_of_docs(docs: &[Vec<u8>]) -> Vec<u8> {
    let mut body = Vec::new();
    for (i, doc) in docs.iter().enumerate() {
        push_element(&mut body, DOCUMENT, &i.to_string(), doc);
    }
    finish_doc(&body).expect("fixture array fits in i32")
}

pub fn array_of_strings(values: &[&str]) -> Vec<u8> {
    let mut body = Vec::new();
    for (i, value) in values.iter().enumerate() {
        push_element(&mut body, STRING, &i.to_string(), &string_value(value));
    }
    finish_doc(&body).expect("fixture array fits in i32")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn double_bits_roundtrip_including_nonfinite() {
        for v in [0.0, -0.0, 1.5, f64::INFINITY, f64::NEG_INFINITY, f64::NAN] {
            let mut doc = DocBuf::new();
            doc.double("n", v);
            let bytes = doc.finish();
            validate(&bytes).unwrap();
            let el = &elements(&bytes, 0).unwrap()[0];
            assert_eq!(el.ty, DOUBLE);
            assert_eq!(decode_f64(el.value).unwrap().to_bits(), v.to_bits());
        }
    }

    #[test]
    fn skip_uses_length_prefix_without_reading_nested_fields() {
        let mut inner = DocBuf::new();
        inner.binary("blob", 0, &vec![0xAB; 1000]);
        let inner = inner.finish();
        let mut outer = DocBuf::new();
        outer.string("name", "a");
        outer.doc("child", &inner);
        outer.string("tail", "z");
        let bytes = outer.finish();
        let els = elements(&bytes, 0).unwrap();
        assert_eq!(els.len(), 3);
        assert_eq!(els[2].key, "tail");
        assert_eq!(decode_string(els[2].value).unwrap(), "z");
        // The binary's bytes were not interpreted, but the nested document still validates.
        validate(&bytes).unwrap();
    }

    #[test]
    fn splice_patches_ancestor_lengths_and_preserves_the_tail() {
        let mut doc = DocBuf::new();
        doc.string("a", "hi");
        doc.string("b", "yo");
        let mut bytes = doc.finish();
        let (start, end) = {
            let els = elements(&bytes, 0).unwrap();
            (els[0].value_at, els[0].value_at + els[0].value.len())
        };
        let new_val = string_value("hello");
        splice(&mut bytes, start, end, &new_val, &[0]).unwrap();
        validate(&bytes).unwrap();
        let els = elements(&bytes, 0).unwrap();
        assert_eq!(decode_string(els[0].value).unwrap(), "hello");
        assert_eq!(decode_string(els[1].value).unwrap(), "yo");
        let declared = i32::from_le_bytes(bytes[0..4].try_into().unwrap()) as usize;
        assert_eq!(declared, bytes.len());
    }
}