herolib_otoml 0.3.13

OTOML - Canonical TOML serialization format with compact binary representation.
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
//! Binary (OBIN) serialization and deserialization.
//!
//! OBIN is a compact, deterministic binary format for OTOML data.
//! It uses a simple type-length-value encoding optimized for:
//! - Minimal size
//! - Fast parsing
//! - Deterministic output
//!
//! ## Format Overview
//!
//! ```text
//! OBIN = HEADER BODY
//! HEADER = MAGIC(4) VERSION(1)
//! BODY = VALUE
//! VALUE = TYPE(1) DATA(*)
//! ```

use super::error::{OtomlError, Result};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use toml::Value;

/// Magic bytes for OBIN format: "OBIN"
const MAGIC: &[u8; 4] = b"OBIN";

/// Current OBIN format version
const VERSION: u8 = 1;

/// Type tags for binary encoding
mod types {
    pub const NULL: u8 = 0x00;
    pub const BOOL_FALSE: u8 = 0x01;
    pub const BOOL_TRUE: u8 = 0x02;
    pub const INT8: u8 = 0x10;
    pub const INT16: u8 = 0x11;
    pub const INT32: u8 = 0x12;
    pub const INT64: u8 = 0x13;
    pub const FLOAT64: u8 = 0x20;
    pub const STRING: u8 = 0x30;
    pub const ARRAY: u8 = 0x40;
    pub const TABLE: u8 = 0x50;
    pub const DATETIME: u8 = 0x60;
}

/// Serialize a value to compact OBIN binary format.
///
/// The output is guaranteed to be:
/// - Deterministic (same input always produces same output)
/// - Compact (minimal byte representation)
/// - Fast to parse
pub fn dump_obin<T: Serialize>(value: &T) -> Result<Vec<u8>> {
    // First serialize to toml::Value to get a canonical structure
    let toml_value =
        toml::Value::try_from(value).map_err(|e| OtomlError::BinarySerialize(e.to_string()))?;

    // Convert to canonical form with sorted keys
    let canonical = to_canonical(&toml_value);

    // Serialize to binary
    let mut buffer = Vec::new();

    // Write header
    buffer.extend_from_slice(MAGIC);
    buffer.push(VERSION);

    // Write body
    write_value(&canonical, &mut buffer)?;

    Ok(buffer)
}

/// Deserialize OBIN binary data into a value.
pub fn load_obin<T: for<'de> Deserialize<'de>>(data: &[u8]) -> Result<T> {
    if data.len() < 5 {
        return Err(OtomlError::BinaryDeserialize(
            "data too short for OBIN header".to_string(),
        ));
    }

    // Verify header
    if &data[0..4] != MAGIC {
        return Err(OtomlError::BinaryDeserialize(
            "invalid OBIN magic bytes".to_string(),
        ));
    }

    if data[4] != VERSION {
        return Err(OtomlError::BinaryDeserialize(format!(
            "unsupported OBIN version: {}",
            data[4]
        )));
    }

    // Parse body
    let mut pos = 5;
    let value = read_value(data, &mut pos)?;

    // Convert toml::Value to T via TOML string (simplest approach)
    let toml_str =
        toml::to_string(&value).map_err(|e| OtomlError::BinaryDeserialize(e.to_string()))?;

    toml::from_str(&toml_str).map_err(|e| OtomlError::BinaryDeserialize(e.to_string()))
}

/// Convert a toml::Value to canonical form with sorted keys.
fn to_canonical(value: &Value) -> Value {
    match value {
        Value::Table(table) => {
            let mut sorted: BTreeMap<String, Value> = BTreeMap::new();
            for (key, val) in table {
                sorted.insert(key.clone(), to_canonical(val));
            }
            Value::Table(sorted.into_iter().collect())
        }
        Value::Array(arr) => Value::Array(arr.iter().map(to_canonical).collect()),
        other => other.clone(),
    }
}

/// Write a value to the buffer.
fn write_value(value: &Value, buf: &mut Vec<u8>) -> Result<()> {
    match value {
        Value::Boolean(false) => {
            buf.push(types::BOOL_FALSE);
        }
        Value::Boolean(true) => {
            buf.push(types::BOOL_TRUE);
        }
        Value::Integer(i) => {
            write_integer(*i, buf);
        }
        Value::Float(f) => {
            buf.push(types::FLOAT64);
            buf.extend_from_slice(&f.to_le_bytes());
        }
        Value::String(s) => {
            buf.push(types::STRING);
            write_string(s, buf);
        }
        Value::Array(arr) => {
            buf.push(types::ARRAY);
            write_varint(arr.len() as u64, buf);
            for item in arr {
                write_value(item, buf)?;
            }
        }
        Value::Table(table) => {
            buf.push(types::TABLE);
            // Sort keys for determinism
            let mut keys: Vec<&String> = table.keys().collect();
            keys.sort();
            write_varint(keys.len() as u64, buf);
            for key in keys {
                write_string(key, buf);
                write_value(table.get(key).unwrap(), buf)?;
            }
        }
        Value::Datetime(dt) => {
            buf.push(types::DATETIME);
            write_string(&dt.to_string(), buf);
        }
    }
    Ok(())
}

/// Read a value from the buffer.
fn read_value(data: &[u8], pos: &mut usize) -> Result<Value> {
    if *pos >= data.len() {
        return Err(OtomlError::BinaryDeserialize(
            "unexpected end of data".to_string(),
        ));
    }

    let type_tag = data[*pos];
    *pos += 1;

    match type_tag {
        types::NULL => Ok(Value::String("null".to_string())),
        types::BOOL_FALSE => Ok(Value::Boolean(false)),
        types::BOOL_TRUE => Ok(Value::Boolean(true)),
        types::INT8 => {
            if *pos >= data.len() {
                return Err(OtomlError::BinaryDeserialize("unexpected end".to_string()));
            }
            let v = data[*pos] as i8 as i64;
            *pos += 1;
            Ok(Value::Integer(v))
        }
        types::INT16 => {
            if *pos + 2 > data.len() {
                return Err(OtomlError::BinaryDeserialize("unexpected end".to_string()));
            }
            let bytes: [u8; 2] = data[*pos..*pos + 2].try_into().unwrap();
            let v = i16::from_le_bytes(bytes) as i64;
            *pos += 2;
            Ok(Value::Integer(v))
        }
        types::INT32 => {
            if *pos + 4 > data.len() {
                return Err(OtomlError::BinaryDeserialize("unexpected end".to_string()));
            }
            let bytes: [u8; 4] = data[*pos..*pos + 4].try_into().unwrap();
            let v = i32::from_le_bytes(bytes) as i64;
            *pos += 4;
            Ok(Value::Integer(v))
        }
        types::INT64 => {
            if *pos + 8 > data.len() {
                return Err(OtomlError::BinaryDeserialize("unexpected end".to_string()));
            }
            let bytes: [u8; 8] = data[*pos..*pos + 8].try_into().unwrap();
            let v = i64::from_le_bytes(bytes);
            *pos += 8;
            Ok(Value::Integer(v))
        }
        types::FLOAT64 => {
            if *pos + 8 > data.len() {
                return Err(OtomlError::BinaryDeserialize("unexpected end".to_string()));
            }
            let bytes: [u8; 8] = data[*pos..*pos + 8].try_into().unwrap();
            let v = f64::from_le_bytes(bytes);
            *pos += 8;
            Ok(Value::Float(v))
        }
        types::STRING => {
            let s = read_string(data, pos)?;
            Ok(Value::String(s))
        }
        types::ARRAY => {
            let len = read_varint(data, pos)? as usize;
            let mut arr = Vec::with_capacity(len);
            for _ in 0..len {
                arr.push(read_value(data, pos)?);
            }
            Ok(Value::Array(arr))
        }
        types::TABLE => {
            let len = read_varint(data, pos)? as usize;
            let mut table = toml::map::Map::new();
            for _ in 0..len {
                let key = read_string(data, pos)?;
                let value = read_value(data, pos)?;
                table.insert(key, value);
            }
            Ok(Value::Table(table))
        }
        types::DATETIME => {
            let s = read_string(data, pos)?;
            // Parse as datetime or keep as string
            if let Ok(dt) = s.parse::<toml::value::Datetime>() {
                Ok(Value::Datetime(dt))
            } else {
                Ok(Value::String(s))
            }
        }
        _ => Err(OtomlError::BinaryDeserialize(format!(
            "unknown type tag: 0x{:02X}",
            type_tag
        ))),
    }
}

/// Write an integer using the smallest possible representation.
fn write_integer(i: i64, buf: &mut Vec<u8>) {
    if i >= i8::MIN as i64 && i <= i8::MAX as i64 {
        buf.push(types::INT8);
        buf.push(i as i8 as u8);
    } else if i >= i16::MIN as i64 && i <= i16::MAX as i64 {
        buf.push(types::INT16);
        buf.extend_from_slice(&(i as i16).to_le_bytes());
    } else if i >= i32::MIN as i64 && i <= i32::MAX as i64 {
        buf.push(types::INT32);
        buf.extend_from_slice(&(i as i32).to_le_bytes());
    } else {
        buf.push(types::INT64);
        buf.extend_from_slice(&i.to_le_bytes());
    }
}

/// Write a string with length prefix.
fn write_string(s: &str, buf: &mut Vec<u8>) {
    let bytes = s.as_bytes();
    write_varint(bytes.len() as u64, buf);
    buf.extend_from_slice(bytes);
}

/// Read a string with length prefix.
fn read_string(data: &[u8], pos: &mut usize) -> Result<String> {
    let len = read_varint(data, pos)? as usize;
    if *pos + len > data.len() {
        return Err(OtomlError::BinaryDeserialize(
            "string length exceeds data".to_string(),
        ));
    }
    let s = std::str::from_utf8(&data[*pos..*pos + len])
        .map_err(|e| OtomlError::BinaryDeserialize(e.to_string()))?
        .to_string();
    *pos += len;
    Ok(s)
}

/// Write a variable-length integer (LEB128-style).
fn write_varint(mut value: u64, buf: &mut Vec<u8>) {
    loop {
        let mut byte = (value & 0x7F) as u8;
        value >>= 7;
        if value != 0 {
            byte |= 0x80;
        }
        buf.push(byte);
        if value == 0 {
            break;
        }
    }
}

/// Read a variable-length integer (LEB128-style).
fn read_varint(data: &[u8], pos: &mut usize) -> Result<u64> {
    let mut result: u64 = 0;
    let mut shift = 0;

    loop {
        if *pos >= data.len() {
            return Err(OtomlError::BinaryDeserialize(
                "unexpected end reading varint".to_string(),
            ));
        }

        let byte = data[*pos];
        *pos += 1;

        result |= ((byte & 0x7F) as u64) << shift;

        if byte & 0x80 == 0 {
            break;
        }

        shift += 7;
        if shift > 63 {
            return Err(OtomlError::BinaryDeserialize("varint overflow".to_string()));
        }
    }

    Ok(result)
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::{Deserialize, Serialize};

    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct TestStruct {
        name: String,
        value: i32,
        enabled: bool,
    }

    #[test]
    fn test_roundtrip() {
        let data = TestStruct {
            name: "test".to_string(),
            value: 42,
            enabled: true,
        };

        let bytes = dump_obin(&data).unwrap();
        let parsed: TestStruct = load_obin(&bytes).unwrap();

        assert_eq!(data, parsed);
    }

    #[test]
    fn test_header() {
        let data = TestStruct {
            name: "test".to_string(),
            value: 42,
            enabled: true,
        };

        let bytes = dump_obin(&data).unwrap();

        // Check header
        assert_eq!(&bytes[0..4], b"OBIN");
        assert_eq!(bytes[4], VERSION);
    }

    #[test]
    fn test_deterministic() {
        let data = TestStruct {
            name: "test".to_string(),
            value: 42,
            enabled: true,
        };

        let bytes1 = dump_obin(&data).unwrap();
        let bytes2 = dump_obin(&data).unwrap();

        assert_eq!(bytes1, bytes2);
    }

    #[test]
    fn test_integer_compression() {
        #[derive(Serialize, Deserialize, PartialEq, Debug)]
        struct Ints {
            small: i8,
            medium: i16,
            large: i32,
            huge: i64,
        }

        let data = Ints {
            small: 42,
            medium: 1000,
            large: 100000,
            huge: 10000000000,
        };

        let bytes = dump_obin(&data).unwrap();
        let parsed: Ints = load_obin(&bytes).unwrap();

        assert_eq!(data.small as i64, parsed.small as i64);
        assert_eq!(data.medium as i64, parsed.medium as i64);
        assert_eq!(data.large as i64, parsed.large as i64);
        assert_eq!(data.huge, parsed.huge);
    }

    #[test]
    fn test_nested() {
        #[derive(Serialize, Deserialize, PartialEq, Debug)]
        struct Outer {
            inner: Inner,
        }

        #[derive(Serialize, Deserialize, PartialEq, Debug)]
        struct Inner {
            value: i32,
        }

        let data = Outer {
            inner: Inner { value: 42 },
        };

        let bytes = dump_obin(&data).unwrap();
        let parsed: Outer = load_obin(&bytes).unwrap();

        assert_eq!(data, parsed);
    }

    #[test]
    fn test_array() {
        #[derive(Serialize, Deserialize, PartialEq, Debug)]
        struct WithArray {
            items: Vec<i32>,
        }

        let data = WithArray {
            items: vec![1, 2, 3, 4, 5],
        };

        let bytes = dump_obin(&data).unwrap();
        let parsed: WithArray = load_obin(&bytes).unwrap();

        assert_eq!(data, parsed);
    }

    #[test]
    fn test_invalid_magic() {
        let data = b"XXXX\x01";
        let result: Result<TestStruct> = load_obin(data);
        assert!(result.is_err());
    }

    #[test]
    fn test_invalid_version() {
        let data = b"OBIN\xFF";
        let result: Result<TestStruct> = load_obin(data);
        assert!(result.is_err());
    }
}