senax-encoder 0.2.2

A fast, compact, and schema-evolution-friendly binary serialization library for Rust.
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# senax-encoder Pack Format Specification

**Version:** 1.0  
**Date:** 2025  
**Status:** Draft

## Table of Contents

1. [Overview]#overview
2. [Format Basics]#format-basics
3. [Data Type Specifications]#data-type-specifications
4. [Struct and Enum Packing]#struct-and-enum-packing
5. [Optimization Features]#optimization-features
6. [Implementation Notes]#implementation-notes

## 1. Overview

The senax-encoder pack format is designed for maximum compactness and performance when schema evolution is not required. Unlike the encode format, pack does not include field IDs for struct fields, resulting in significantly smaller binary size but requiring strict field order compatibility.

### Key Design Principles

- **Maximum Compactness:** No field IDs for struct fields, minimal overhead
- **High Performance:** Direct binary representation without metadata
- **Order Dependent:** Field order must match exactly between pack/unpack
- **Little Endian:** Consistent byte order across platforms

### Pack vs Encode Comparison

| Feature | Pack Format | Encode Format |
|---------|-------------|---------------|
| Binary Size | Smaller | Larger |
| Schema Evolution | Not Supported | Full Support |
| Field Order | Must Match | Independent |
| Performance | Faster | Moderate |
| Use Case | High-performance, stable schemas | Evolving APIs, long-term storage |

## 2. Format Basics

### 2.1 Magic Numbers

The senax-encoder library uses magic numbers to distinguish between different serialization formats when using convenience functions:

**Encode Format:** `0xA55A` (2 bytes, little-endian)
- Used by `encode()` and `encode_to()` functions
- Supports schema evolution with field IDs and type tags
- Binary starts with: `[0x5A, 0xA5]`

**Pack Format:** `0xDADA` (2 bytes, little-endian)  
- Used by `pack()` and `pack_to()` functions
- Compact format without schema evolution support
- Binary starts with: `[0xDA, 0xDA]`

**Important Notes:**
- Magic numbers are **only added by convenience functions** (`pack()`, `encode()`, etc.)
- Direct trait method calls (`Packer::pack`, `Encoder::encode`) do **not** include magic numbers
- The `unpack()` and `decode()` functions validate and consume the magic numbers
- This specification describes the format **after** magic number (raw pack format)

### 2.2 Byte Order

All multi-byte integers are encoded in **little-endian** format.

### 2.3 Basic Structure

Pack format uses direct binary representation with minimal tagging:

**Primitives:**
```
[DATA:variable]  // Direct encoding without type tags (except for optimizations)
```

**Containers:**
```
[LENGTH:variable] [DATA:variable]  // Length-prefixed for collections
```

**Structs:**
```
[HASH:u64_le] [FIELD1] [FIELD2] ...  // Named structs: structure hash + fields in order
[COUNT:variable] [FIELD1] [FIELD2] ...  // Tuple structs: field count + fields in order
// (no data)  // Unit structs: no additional data
```

**Enums:**
```
[VARIANT_ID:variable] [HASH:u64_le] [FIELD1] [FIELD2] ...  // Named variants
[VARIANT_ID:variable] [COUNT:variable] [FIELD1] [FIELD2] ...  // Tuple variants  
[VARIANT_ID:variable]  // Unit variants
```

### 2.4 Structure Hash

Named structs and enum variants include a CRC64 structure hash for validation:
- Computed from type name, field names, and field types
- Ensures pack/unpack compatibility
- Detects structural mismatches at runtime

## 3. Data Type Specifications

### 3.1 Boolean

**Pack Format:**
- `false`: `0x00`
- `true`: `0x01`

**Unpack Behavior:**
- `0x00``false`
- Any non-zero value → `true` (relaxed validation)

### 3.2 Unsigned Integers (u16, u32, u64, u128, usize)

**Pack Format (Variable-Length Encoding):**

All unsigned integers except u8 use the same variable-length encoding as the encode format:

```
// Small values (0-127)
value -> [TAG_ZERO + value]  // Direct encoding in tag byte

// Medium values (128-383)  
value -> [TAG_U8] [value - 128]  // 2 bytes total

// Larger values use appropriate width tags
u16 -> [TAG_U16] [value:u16_le]  // if value > 383
u32 -> [TAG_U32] [value:u32_le]  // if value > 65535
u64 -> [TAG_U64] [value:u64_le]  // if value > 4294967295
u128 -> [TAG_U128] [value:u128_le]  // if value > 18446744073709551615
usize -> (same as u32 or u64 depending on platform)
```

**Tags:**
- `TAG_ZERO = 0x00` to `TAG_U8_127 = 0x7F` (values 0-127)
- `TAG_U8 = 0x83` (131)
- `TAG_U16 = 0x84` (132)
- `TAG_U32 = 0x85` (133)
- `TAG_U64 = 0x86` (134)
- `TAG_U128 = 0x87` (135)

**Size Examples:**
- 0-127: 1 byte
- 128-383: 2 bytes
- 384-65535: 3 bytes (u16)
- 65536-4294967295: 5 bytes (u32)
- etc.

### 3.3 u8 (Special Case)

**Pack Format:**
```
u8 -> [value:u8]  // Direct 1-byte encoding without tags
```

**Size:** Always 1 byte (no tag overhead)

### 3.4 Signed Integers (i8, i16, i32, i64, i128, isize)

**Pack Format (Variable-Length Encoding):**

All signed integers use the same variable-length encoding as the encode format:

```
// Positive values (0 and above)
value -> (encoded as corresponding unsigned type)

// Negative values  
value -> [TAG_NEGATIVE] [encoded_inverted_value]
```

Where `encoded_inverted_value` is `!(value as unsigned_type)` encoded using the unsigned format.

**Tag:**
- `TAG_NEGATIVE = 0x88` (136)

**Examples:**
```
0     -> [0x00]                    // 1 byte
42    -> [0x2A]                    // 1 byte  
-1    -> [0x88] [0x00]             // 2 bytes (TAG_NEGATIVE + inverted 0)
-42   -> [0x88] [0x29]             // 2 bytes (TAG_NEGATIVE + inverted 41)
1000  -> [0x84] [0xE8, 0x03]       // 3 bytes (TAG_U16 + 1000 as u16_le)
-1000 -> [0x88] [0x84] [0xE7, 0x03] // 4 bytes (TAG_NEGATIVE + TAG_U16 + inverted 999)
```

### 3.5 Floating Point (f32, f64)

**Pack Format with Optimization:**
```
f32 -> [TAG_NONE]                    // if value == 0.0
    -> [TAG_F32] [value:f32_le]      // if value != 0.0

f64 -> [TAG_NONE]                    // if value == 0.0  
    -> [TAG_F64] [value:f64_le]      // if value != 0.0
```

**Tags:**
- `TAG_NONE = 0x80`
- `TAG_F32 = 0x89`
- `TAG_F64 = 0x8A`

**Size:**
- 0.0 values: 1 byte
- Non-zero f32: 5 bytes (1 tag + 4 data)
- Non-zero f64: 9 bytes (1 tag + 8 data)

### 3.6 Character (char)

**Pack Format (Variable-Length Encoding):**
```
char -> (encoded as u32 using variable-length format)
```

The Unicode code point is encoded using the same variable-length format as u32:
- Small code points (0-127): 1 byte
- Medium code points (128-383): 2 bytes  
- Larger code points: 3-5 bytes depending on value

**Size:** 1-5 bytes depending on Unicode code point value

### 3.7 String

**Pack Format:**
```
// Short strings (0-40 characters)
String -> [TAG_STRING_BASE + len] [utf8_bytes]

// Long strings (41+ characters)  
String -> [TAG_STRING_LONG] [len:variable] [utf8_bytes]
```

**Tags:**
- `TAG_STRING_BASE = 0x8B` (139)
- `TAG_STRING_LONG = 0xB4` (180)

**Examples:**
```
""      -> [0x8B]                    // Empty string
"Hi"    -> [0x8D, 0x48, 0x69]        // 2-char string
"Long..." -> [0xB4, len, utf8_bytes] // Long string
```

### 3.8 Option<T>

**Pack Format:**
```
None    -> [TAG_NONE]
Some(v) -> [TAG_SOME] [packed_value]
```

**Tags:**
- `TAG_NONE = 0x80`
- `TAG_SOME = 0x81`

### 3.9 Vec<T>

**Pack Format:**
```
// Short vectors (0-5 elements)
Vec<T> -> [TAG_ARRAY_VEC_SET_BASE + len] [element1] [element2] ...

// Long vectors (6+ elements)
Vec<T> -> [TAG_ARRAY_VEC_SET_LONG] [len:variable] [element1] [element2] ...
```

**Tags:**
- `TAG_ARRAY_VEC_SET_BASE = 0xBC` (188)
- `TAG_ARRAY_VEC_SET_LONG = 0xC2` (194)

### 3.10 Arrays [T; N]

**Pack Format:**
```
// Short arrays (0-5 elements)
[T; N] -> [TAG_ARRAY_VEC_SET_BASE + N] [element1] [element2] ... [elementN]

// Long arrays (6+ elements)
[T; N] -> [TAG_ARRAY_VEC_SET_LONG] [N:variable] [element1] [element2] ... [elementN]
```

**Tags:**
- `TAG_ARRAY_VEC_SET_BASE = 0xBC` (188)
- `TAG_ARRAY_VEC_SET_LONG = 0xC2` (194)

**Note:** Array length N is encoded and validated against the expected size during unpack.

### 3.11 HashMap<K, V>

**Pack Format:**
```
HashMap -> [TAG_MAP] [len:variable] [key1] [value1] [key2] [value2] ...
```

**Tag:**
- `TAG_MAP = 0xC4` (196)

### 3.12 Tuples

**Pack Format:**
```
() -> [TAG_TUPLE] [0:variable]
(T1,) -> [TAG_TUPLE] [1:variable] [element1]
(T1, T2) -> [TAG_TUPLE] [2:variable] [element1] [element2]
...
```

**Tag:**
- `TAG_TUPLE = 0xC3` (195)

### 3.13 Bytes

**Pack Format:**
```
Bytes -> [TAG_BINARY] [len:variable] [byte_data]
```

**Tag:**
- `TAG_BINARY = 0xB5` (181)

### 3.14 Extended Types (Feature-Dependent)

#### DateTime (chrono feature)

**Pack Format:**
```
DateTime<Utc> -> [TAG_NONE]                          // if default value
              -> [TAG_CHRONO_DATETIME] [seconds:i64] [nanos:u32]  // if non-default

DateTime<Local> -> [TAG_NONE]                        // if default value  
                -> [TAG_CHRONO_DATETIME] [seconds:i64] [nanos:u32]  // if non-default
```

All DateTime types are normalized to UTC for storage.

**Tags:**
- `TAG_NONE = 0x80`
- `TAG_CHRONO_DATETIME = 0xC5` (197)

#### NaiveDate (chrono feature)

**Pack Format:**
```
NaiveDate -> [TAG_CHRONO_NAIVE_DATE] [days_from_epoch:i64]
```

**Tag:**
- `TAG_CHRONO_NAIVE_DATE = 0xC6` (198)

**Epoch:** 1970-01-01

#### NaiveTime (chrono feature)

**Pack Format:**
```
NaiveTime -> [TAG_CHRONO_NAIVE_TIME] [seconds_from_midnight:u32] [nanoseconds:u32]
```

**Tag:**
- `TAG_CHRONO_NAIVE_TIME = 0xC7` (199)

#### NaiveDateTime (chrono feature)

**Pack Format:**
```
NaiveDateTime -> [TAG_NONE]                              // if default value
              -> [TAG_CHRONO_NAIVE_DATETIME] [seconds:i64] [nanos:u32]  // if non-default
```

Stores as seconds and nanoseconds since Unix epoch (1970-01-01 00:00:00 UTC).

**Tags:**
- `TAG_NONE = 0x80`
- `TAG_CHRONO_NAIVE_DATETIME = 0xD0` (208)

#### Decimal (rust_decimal feature)

**Pack Format:**
```
Decimal -> [TAG_DECIMAL] [mantissa:i128] [scale:u32]
```

**Tag:**
- `TAG_DECIMAL = 0xC8` (200)

#### UUID/ULID (uuid/ulid features)

**Pack Format:**
```
UUID -> [TAG_NONE]               // if default/nil value
     -> [TAG_UUID] [value:u128_le]  // if non-default

ULID -> [TAG_NONE]               // if default/nil value  
     -> [TAG_UUID] [value:u128_le]  // if non-default
```

**Tags:**
- `TAG_NONE = 0x80`
- `TAG_UUID = 0xC9` (201)

**Note:** UUID and ULID share the same tag and are binary compatible at the encoding level.

## 4. Struct and Enum Packing

### 4.1 Named Structs

**Pack Format:**
```
struct MyStruct {
    field1: T1,
    field2: T2,
} -> [structure_hash:u64_le] [packed_field1] [packed_field2]
```

**Structure Hash:** CRC64 of "type:MyStruct|struct|named|field1:T1|field2:T2"

**Example:**
```rust
struct User {
    id: u32,
    name: String,
}

// Packed as: [hash:u64] [id:u32] [name:String]
```

### 4.2 Tuple Structs

**Pack Format:**
```
struct MyStruct(T1, T2) -> [field_count:variable] [packed_field1] [packed_field2]
```

**Example:**
```rust
struct Point(f32, f32);

// Packed as: [2:variable] [x:f32] [y:f32]
```

### 4.3 Unit Structs

**Pack Format:**
```
struct MyStruct; -> // (no additional data)
```

### 4.4 Enums

#### Named Variants
```
enum MyEnum {
    Variant { field1: T1, field2: T2 }
} -> [variant_id:variable] [structure_hash:u64_le] [packed_field1] [packed_field2]
```

#### Tuple Variants
```
enum MyEnum {
    Variant(T1, T2)
} -> [variant_id:variable] [field_count:variable] [packed_field1] [packed_field2]
```

#### Unit Variants
```
enum MyEnum {
    Variant
} -> [variant_id:variable]
```

**Variant IDs:** CRC64 hash of variant name (same as encode format)

## 5. Optimization Features

### 5.1 Zero/Default Value Optimization

**f32/f64:** 0.0 values use single `TAG_NONE` byte instead of full representation.

**chrono types:** Default values use single `TAG_NONE` byte:
- `DateTime<Utc>::default()``[TAG_NONE]`
- `DateTime<Local>::default()``[TAG_NONE]`
- `NaiveDateTime::default()``[TAG_NONE]`

**UUID/ULID:** Nil/default values use single `TAG_NONE` byte:
- `Uuid::nil()``[TAG_NONE]`
- `Ulid::nil()``[TAG_NONE]`

### 5.2 Short Collection Optimization

**Vectors/Arrays/Sets:** Collections with 0-5 elements encode length in the tag byte.

### 5.3 Short String Optimization

**Strings:** Strings with 0-40 characters encode length in the tag byte.

### 5.4 Direct Integer Encoding

**Integers:** Variable-length encoding with optimized representation for small values.

## 6. Implementation Notes

### 6.1 Field Order Dependency

**Critical:** Pack format requires exact field order matching between pack and unpack operations.

```rust
// This works
struct A { x: u32, y: String }
struct B { x: u32, y: String }  // Same order

// This fails  
struct A { x: u32, y: String }
struct C { y: String, x: u32 }  // Different order
```

### 6.2 Structure Hash Validation

Named structs and enum variants validate structure hash during unpack:
- Prevents accidental type mismatches
- Detects field reordering
- Ensures pack/unpack compatibility

### 6.3 Cross-Type Compatibility

Pack format supports the same cross-type decoding as encode format:
- Integer widening (u16 → u32)
- Signed/unsigned conversion (where safe)

### 6.4 Performance Characteristics

**Pack Advantages:**
- 20-50% smaller binary size (no field IDs)
- Faster encoding/decoding (less metadata)
- Direct memory mapping potential

**Pack Limitations:**
- No schema evolution support
- Field order dependency
- Requires exact type matching

### 6.5 Use Case Guidelines

**Use Pack When:**
- Maximum performance is required
- Binary size is critical
- Schema is stable and controlled
- Both ends use the same code version

**Use Encode When:**
- Schema evolution is needed
- Long-term data storage
- API versioning is required
- Forward/backward compatibility is important

## 7. Examples

### 7.1 Simple Struct

```rust
#[derive(Pack, Unpack)]
struct Point {
    x: f32,
    y: f32,
}

let point = Point { x: 1.0, y: 2.0 };
// Packed: [hash:8] [TAG_F32][1.0:4] [TAG_F32][2.0:4] = 18 bytes
```

### 7.2 Enum with Mixed Variants

```rust
#[derive(Pack, Unpack)]
enum Message {
    #[senax(id=1)]
    Text(String),
    #[senax(id=2)]
    Data { id: u32, payload: Vec<u8> },
    Ping,
}

let msg = Message::Data { id: 42, payload: vec![1, 2, 3] };
// Packed: [variant_id] [hash:8] [42:1] [TAG_ARRAY+3][1][2][3] = 14 bytes
```

### 7.3 Optimization Examples

```rust
// Floating point optimization
let zero_f32 = 0.0f32;        // Packed: [TAG_NONE] = 1 byte
let nonzero_f32 = 3.14f32;    // Packed: [TAG_F32][3.14:4] = 5 bytes

// Collection optimization
let empty_vec: Vec<u8> = vec![];     // Packed: [TAG_ARRAY+0] = 1 byte  
let small_vec = vec![1u8, 2, 3];     // Packed: [TAG_ARRAY+3][1][2][3] = 4 bytes
let large_vec = vec![0u8; 100];      // Packed: [TAG_ARRAY_LONG][100][data] = ~103 bytes

// String optimization
let short_str = "Hi";                // Packed: [TAG_STRING+2][Hi] = 3 bytes
let empty_str = "";                  // Packed: [TAG_STRING+0] = 1 byte

// Chrono type optimization
use chrono::{DateTime, NaiveDateTime, Utc};
let default_dt = DateTime::<Utc>::default();  // Packed: [TAG_NONE] = 1 byte
let actual_dt = DateTime::from_timestamp(1640995200, 0).unwrap();  
// Packed: [TAG_CHRONO_DATETIME][seconds:i64][nanos:u32] = 13 bytes

let default_naive = NaiveDateTime::default();  // Packed: [TAG_NONE] = 1 byte
let actual_naive = DateTime::from_timestamp(1640995200, 123456789).unwrap().naive_utc();
// Packed: [TAG_CHRONO_NAIVE_DATETIME][seconds:i64][nanos:u32] = 13 bytes

// UUID optimization  
use uuid::Uuid;
let nil_uuid = Uuid::nil();          // Packed: [TAG_NONE] = 1 byte
let actual_uuid = Uuid::new_v4();    // Packed: [TAG_UUID][value:u128] = 17 bytes
```

## 8. Migration Guide

### 8.1 From Encode to Pack

1. Ensure schema stability
2. Verify field order consistency
3. Test with actual data
4. Measure size/performance gains

### 8.2 From Pack to Encode

1. Add field IDs if needed
2. Update error handling for unknown fields
3. Test schema evolution scenarios
4. Consider migration strategy for existing data