oxihuman-export 0.2.0

Export pipeline for OxiHuman — glTF, COLLADA, STL, and streaming formats
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
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0
#![allow(dead_code)]

//! Apache Thrift binary protocol encoding stub.

/// Thrift field types.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ThriftType {
    Stop = 0,
    Bool = 2,
    Byte = 3,
    Double = 4,
    I16 = 6,
    I32 = 8,
    I64 = 10,
    String = 11,
    Struct = 12,
    Map = 13,
    Set = 14,
    List = 15,
}

/// A Thrift binary encoder.
#[allow(dead_code)]
#[derive(Debug, Clone, Default)]
pub struct ThriftEncoder {
    pub buf: Vec<u8>,
}

impl ThriftEncoder {
    #[allow(dead_code)]
    pub fn new() -> Self {
        ThriftEncoder::default()
    }

    /// Write a field header (type + field id).
    #[allow(dead_code)]
    pub fn write_field_begin(&mut self, ftype: ThriftType, field_id: i16) {
        self.buf.push(ftype as u8);
        self.buf.extend_from_slice(&field_id.to_be_bytes());
    }

    /// Write field stop marker.
    #[allow(dead_code)]
    pub fn write_field_stop(&mut self) {
        self.buf.push(ThriftType::Stop as u8);
    }

    /// Write an i32 value.
    #[allow(dead_code)]
    pub fn write_i32(&mut self, val: i32) {
        self.buf.extend_from_slice(&val.to_be_bytes());
    }

    /// Write an i64 value.
    #[allow(dead_code)]
    pub fn write_i64(&mut self, val: i64) {
        self.buf.extend_from_slice(&val.to_be_bytes());
    }

    /// Write a bool value.
    #[allow(dead_code)]
    pub fn write_bool(&mut self, val: bool) {
        self.buf.push(if val { 1 } else { 0 });
    }

    /// Write a string (length-prefixed).
    #[allow(dead_code)]
    pub fn write_string(&mut self, s: &str) {
        self.buf.extend_from_slice(&(s.len() as i32).to_be_bytes());
        self.buf.extend_from_slice(s.as_bytes());
    }

    /// Write a double.
    #[allow(dead_code)]
    pub fn write_double(&mut self, val: f64) {
        self.buf.extend_from_slice(&val.to_bits().to_be_bytes());
    }

    /// Byte length.
    #[allow(dead_code)]
    pub fn len(&self) -> usize {
        self.buf.len()
    }

    /// Is empty.
    #[allow(dead_code)]
    pub fn is_empty(&self) -> bool {
        self.buf.is_empty()
    }

    /// Get encoded bytes.
    #[allow(dead_code)]
    pub fn as_bytes(&self) -> &[u8] {
        &self.buf
    }
}

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

    #[test]
    fn new_is_empty() {
        let enc = ThriftEncoder::new();
        assert!(enc.is_empty());
    }

    #[test]
    fn write_field_begin_three_bytes() {
        let mut enc = ThriftEncoder::new();
        enc.write_field_begin(ThriftType::I32, 1);
        assert_eq!(enc.len(), 3);
    }

    #[test]
    fn write_field_stop_one_byte() {
        let mut enc = ThriftEncoder::new();
        enc.write_field_stop();
        assert_eq!(enc.buf[0], 0);
    }

    #[test]
    fn write_i32_four_bytes() {
        let mut enc = ThriftEncoder::new();
        enc.write_i32(42);
        assert_eq!(enc.len(), 4);
    }

    #[test]
    fn write_i64_eight_bytes() {
        let mut enc = ThriftEncoder::new();
        enc.write_i64(1_000_000);
        assert_eq!(enc.len(), 8);
    }

    #[test]
    fn write_bool_one_byte() {
        let mut enc = ThriftEncoder::new();
        enc.write_bool(true);
        assert_eq!(enc.buf[0], 1);
    }

    #[test]
    fn write_string_length_prefixed() {
        let mut enc = ThriftEncoder::new();
        enc.write_string("hi");
        assert_eq!(enc.len(), 6);
    }

    #[test]
    fn write_double_eight_bytes() {
        let mut enc = ThriftEncoder::new();
        enc.write_double(std::f64::consts::PI);
        assert_eq!(enc.len(), 8);
    }

    #[test]
    fn as_bytes_matches_buf() {
        let mut enc = ThriftEncoder::new();
        enc.write_i32(1);
        assert_eq!(enc.as_bytes(), &enc.buf[..]);
    }

    #[test]
    fn full_field_round_trip() {
        let mut enc = ThriftEncoder::new();
        enc.write_field_begin(ThriftType::I32, 1);
        enc.write_i32(99);
        enc.write_field_stop();
        assert_eq!(enc.len(), 8);
    }
}

// ─── Thrift Compact Protocol ─────────────────────────────────────────────────

/// Compact-protocol type codes.
#[allow(dead_code)]
pub(crate) mod compact_type {
    pub const BOOLEAN_TRUE: u8 = 1;
    pub const BOOLEAN_FALSE: u8 = 2;
    pub const BYTE: u8 = 3;
    pub const I16: u8 = 4;
    pub const I32: u8 = 5;
    pub const I64: u8 = 6;
    pub const DOUBLE: u8 = 7;
    pub const BINARY: u8 = 8;
    pub const LIST: u8 = 9;
    pub const SET: u8 = 10;
    pub const MAP: u8 = 11;
    pub const STRUCT: u8 = 12;
}

/// An Apache Thrift Compact Protocol encoder.
///
/// Implements the compact binary wire format used by Parquet metadata and
/// other Thrift-based protocols.  Encoding rules:
/// - Integers are zigzag-encoded then written as varints.
/// - Doubles are 8-byte little-endian IEEE 754.
/// - Strings/binary have a varint length prefix then raw bytes.
/// - Field headers use delta encoding when the delta fits in 4 bits.
/// - Structs begin implicitly and end with a 0x00 stop byte.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct ThriftCompactEncoder {
    buf: Vec<u8>,
    /// Stack of `last_field_id` values for nested structs.
    field_stack: Vec<i16>,
    last_field: i16,
}

impl ThriftCompactEncoder {
    /// Create a new encoder.
    #[allow(dead_code)]
    pub fn new() -> Self {
        ThriftCompactEncoder {
            buf: Vec::new(),
            field_stack: Vec::new(),
            last_field: 0,
        }
    }

    /// Begin a new struct scope, pushing the current `last_field_id` onto the
    /// stack and resetting it to 0.
    #[allow(dead_code)]
    pub fn begin_struct(&mut self) {
        self.field_stack.push(self.last_field);
        self.last_field = 0;
    }

    /// End the current struct scope: write the 0x00 stop byte and pop the
    /// saved `last_field_id` from the stack.
    #[allow(dead_code)]
    pub fn end_struct(&mut self) {
        self.buf.push(0x00);
        if let Some(prev) = self.field_stack.pop() {
            self.last_field = prev;
        } else {
            self.last_field = 0;
        }
    }

    /// Write a boolean field.
    #[allow(dead_code)]
    pub fn write_bool_field(&mut self, field_id: i16, value: bool) {
        let ctype = if value {
            compact_type::BOOLEAN_TRUE
        } else {
            compact_type::BOOLEAN_FALSE
        };
        self.write_field_header(field_id, ctype);
        // Boolean value is embedded in the type nibble — no extra byte.
    }

    /// Write an i32 field (zigzag varint).
    #[allow(dead_code)]
    pub fn write_i32_field(&mut self, field_id: i16, value: i32) {
        self.write_field_header(field_id, compact_type::I32);
        let zz = Self::zigzag32(value);
        self.write_varint(zz as u64);
    }

    /// Write an i64 field (zigzag varint).
    #[allow(dead_code)]
    pub fn write_i64_field(&mut self, field_id: i16, value: i64) {
        self.write_field_header(field_id, compact_type::I64);
        let zz = Self::zigzag64(value);
        self.write_varint(zz);
    }

    /// Write a double field (8-byte little-endian IEEE 754).
    #[allow(dead_code)]
    pub fn write_double_field(&mut self, field_id: i16, value: f64) {
        self.write_field_header(field_id, compact_type::DOUBLE);
        self.buf.extend_from_slice(&value.to_bits().to_le_bytes());
    }

    /// Write a binary / string field (varint length then raw bytes).
    #[allow(dead_code)]
    pub fn write_string_field(&mut self, field_id: i16, value: &[u8]) {
        self.write_field_header(field_id, compact_type::BINARY);
        self.write_varint(value.len() as u64);
        self.buf.extend_from_slice(value);
    }

    /// Write a list-field header.
    ///
    /// `element_type` should be one of the `compact_type::*` constants.
    /// After this call the caller must write `count` elements directly.
    #[allow(dead_code)]
    pub fn write_list_field_header(&mut self, field_id: i16, element_type: u8, count: usize) {
        self.write_field_header(field_id, compact_type::LIST);
        if count < 15 {
            self.buf.push(((count as u8) << 4) | element_type);
        } else {
            self.buf.push(0xf0 | element_type);
            self.write_varint(count as u64);
        }
    }

    /// Write a nested-struct field header (type = STRUCT, 0x0C) and enter the
    /// struct scope so that subsequent field writes belong to the nested struct.
    #[allow(dead_code)]
    pub fn write_struct_field_begin(&mut self, field_id: i16) {
        self.write_field_header(field_id, compact_type::STRUCT);
        self.field_stack.push(self.last_field);
        self.last_field = 0;
    }

    /// Write raw bytes directly into the output buffer (used for embedding
    /// pre-serialised sub-structs).
    #[allow(dead_code)]
    pub fn write_raw(&mut self, data: &[u8]) {
        self.buf.extend_from_slice(data);
    }

    /// Consume the encoder and return its byte buffer.
    #[allow(dead_code)]
    pub fn into_bytes(self) -> Vec<u8> {
        self.buf
    }

    /// Borrow the current byte buffer.
    #[allow(dead_code)]
    pub fn as_bytes(&self) -> &[u8] {
        &self.buf
    }

    // ── Public helpers needed by sibling modules ──────────────────────────────

    /// Public wrapper around `zigzag32` for use in sibling modules.
    #[allow(dead_code)]
    #[inline]
    pub fn zigzag32_pub(n: i32) -> u32 {
        Self::zigzag32(n)
    }

    /// Encode a single varint value and return it as a `Vec<u8>`.
    #[allow(dead_code)]
    pub fn encode_varint_pub(v: u64) -> Vec<u8> {
        let mut enc = ThriftCompactEncoder::new();
        enc.write_varint(v);
        enc.into_bytes()
    }

    // ── Private helpers ──────────────────────────────────────────────────────

    /// Encode a field header using delta compression when possible.
    ///
    /// If `field_id - last_field` fits in [1..=15], the header is a single
    /// byte `(delta << 4) | compact_type`.  Otherwise a 0x00 byte is written
    /// followed by the zigzag-encoded i16 field id as a varint.
    pub fn write_field_header(&mut self, field_id: i16, compact_type: u8) {
        let delta = field_id.wrapping_sub(self.last_field);
        if (1..=15).contains(&delta) {
            self.buf.push(((delta as u8) << 4) | compact_type);
        } else {
            self.buf.push(compact_type);
            let zz = Self::zigzag32(field_id as i32);
            self.write_varint(zz as u64);
        }
        self.last_field = field_id;
    }

    /// Write a variable-length integer in little-endian 7-bit groups.
    fn write_varint(&mut self, mut v: u64) {
        loop {
            let byte = (v & 0x7f) as u8;
            v >>= 7;
            if v == 0 {
                self.buf.push(byte);
                break;
            }
            self.buf.push(byte | 0x80);
        }
    }

    /// Zigzag-encode an i32: maps signed integers to unsigned so that small
    /// absolute values produce small varints.
    #[inline]
    fn zigzag32(n: i32) -> u32 {
        ((n << 1) ^ (n >> 31)) as u32
    }

    /// Zigzag-encode an i64.
    #[inline]
    fn zigzag64(n: i64) -> u64 {
        ((n << 1) ^ (n >> 63)) as u64
    }
}

impl Default for ThriftCompactEncoder {
    fn default() -> Self {
        Self::new()
    }
}

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

    /// An empty struct must encode as exactly the single stop byte 0x00.
    #[test]
    fn empty_struct_is_stop_byte() {
        let mut enc = ThriftCompactEncoder::new();
        enc.begin_struct();
        enc.end_struct();
        assert_eq!(enc.as_bytes(), &[0x00]);
    }

    /// Field 1, i32 value 0.
    /// Header: delta=1, type=I32=5 → (1<<4)|5 = 0x15.
    /// Zigzag(0) = 0 → varint = 0x00.
    #[test]
    fn i32_field_1_value_0() {
        let mut enc = ThriftCompactEncoder::new();
        enc.begin_struct();
        enc.write_i32_field(1, 0);
        enc.end_struct();
        // [0x15, 0x00, 0x00]
        let b = enc.as_bytes();
        assert_eq!(b[0], 0x15, "field header byte");
        assert_eq!(b[1], 0x00, "zigzag varint for 0");
        assert_eq!(b[2], 0x00, "stop byte");
    }

    /// A string field must have a varint length prefix followed by the raw
    /// UTF-8 bytes.
    #[test]
    fn string_field_has_length_prefix() {
        let mut enc = ThriftCompactEncoder::new();
        enc.begin_struct();
        enc.write_string_field(1, b"hi");
        enc.end_struct();
        let b = enc.as_bytes();
        // Header byte (0x18 = (1<<4)|8), then varint(2)=0x02, then 'h','i', then 0x00
        assert_eq!(b[0], (1u8 << 4) | compact_type::BINARY);
        assert_eq!(b[1], 2u8); // varint length 2
        assert_eq!(b[2], b'h');
        assert_eq!(b[3], b'i');
        assert_eq!(b[4], 0x00);
    }

    /// Verify that a known struct round-trips: field 1 = i32(42), field 2 = i64(1).
    #[test]
    fn known_struct_round_trip() {
        let mut enc = ThriftCompactEncoder::new();
        enc.begin_struct();
        enc.write_i32_field(1, 42);
        enc.write_i64_field(2, 1);
        enc.end_struct();
        let b = enc.into_bytes();
        // Must end with stop byte
        assert_eq!(*b.last().unwrap(), 0x00);
        // Must start with field-1 header (delta=1, type=I32=5) → 0x15
        assert_eq!(b[0], 0x15);
        // zigzag(42) = 84 = 0x54 (single varint byte)
        assert_eq!(b[1], 84u8);
        // field-2 header: delta=1, type=I64=6 → 0x16
        assert_eq!(b[2], 0x16);
        // zigzag(1) = 2 = 0x02
        assert_eq!(b[3], 2u8);
    }

    /// Double field must produce 8 bytes of LE IEEE-754 bits after the header.
    #[test]
    fn double_field_le_ieee754() {
        let mut enc = ThriftCompactEncoder::new();
        enc.begin_struct();
        enc.write_double_field(1, 1.0_f64);
        enc.end_struct();
        let b = enc.as_bytes();
        // 1.0f64 bits = 0x3FF0000000000000 LE → [0,0,0,0,0,0,0xF0,0x3F]
        let expected_le = 1.0_f64.to_bits().to_le_bytes();
        assert_eq!(&b[1..9], &expected_le);
    }

    /// Nested struct round-trip: outer field 5 is a struct, inner has field 1.
    #[test]
    fn nested_struct_round_trip() {
        let mut enc = ThriftCompactEncoder::new();
        enc.begin_struct();
        enc.write_struct_field_begin(5);
        enc.write_i32_field(1, 7);
        enc.end_struct(); // end inner
        enc.end_struct(); // end outer
        let b = enc.into_bytes();
        assert_eq!(*b.last().unwrap(), 0x00);
        // outer field 5 header (non-delta since 5 fits in delta from 0): delta=5, type=STRUCT=12 → (5<<4)|12 = 0x5C
        assert_eq!(b[0], (5u8 << 4) | compact_type::STRUCT);
    }

    /// List field header encoding for a small count.
    #[test]
    fn list_field_header_small_count() {
        let mut enc = ThriftCompactEncoder::new();
        enc.begin_struct();
        enc.write_list_field_header(1, compact_type::STRUCT, 3);
        enc.end_struct();
        let b = enc.as_bytes();
        // field header: delta=1, type=LIST=9 → 0x19
        assert_eq!(b[0], (1u8 << 4) | compact_type::LIST);
        // list header: count=3 < 15, element_type=STRUCT=12 → (3<<4)|12 = 0x3C
        assert_eq!(b[1], (3u8 << 4) | compact_type::STRUCT);
    }

    /// Boolean true field.
    #[test]
    fn bool_true_field() {
        let mut enc = ThriftCompactEncoder::new();
        enc.begin_struct();
        enc.write_bool_field(1, true);
        enc.end_struct();
        let b = enc.as_bytes();
        // Boolean value encoded in type nibble, no extra byte.
        // Header: delta=1, type=BOOL_TRUE=1 → (1<<4)|1 = 0x11
        assert_eq!(b[0], (1u8 << 4) | compact_type::BOOLEAN_TRUE);
        // Next byte is stop byte
        assert_eq!(b[1], 0x00);
    }
}