dx-serializer 0.1.0

A token-efficient serialization format for LLM context windows with high-performance binary encoding
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

# DX Serializer API Reference

Complete Rust API documentation for dx-serializer.

## Core Functions

### parse()

Parse DX format into typed structures.
```rust
pub fn parse(input: &[u8]) -> Result<DxValue, DxError> ```
Parameters: -`input: &[u8]` — DX format bytes (UTF-8) Returns: -`Ok(DxValue)` — Parsed data structure -`Err(DxError)` — Parse error with position and message Example:
```rust
let data = parse(b"name:Alice^age:30")?;
```
Performance: ~1.9µs for typical documents (SIMD-accelerated).


### encode()


Encode Rust structures to DX format.
```rust
pub fn encode(value: &DxValue) -> Result<Vec<u8>, DxError> ```
Parameters: -`value: &DxValue` — Data to encode Returns: -`Ok(Vec<u8>)` — DX format bytes -`Err(DxError)` — Encoding error Example:
```rust
let mut obj = DxObject::new();
obj.insert("name".to_string(), DxValue::String("Alice".into()));
let encoded = encode(&DxValue::Object(obj))?;
```
Optimizations: -Automatic ditto compression for repeated values -Alias generation for repeated keys -Optimal operator selection (`:` vs `^` vs `>`)

### format_human()

Format DX data for human display (LSP).
```rust
pub fn format_human(value: &DxValue) -> Result<String, DxError> ```
Parameters: -`value: &DxValue` — Data to format Returns: -`Ok(String)` — Beautifully formatted output -`Err(DxError)` — Formatting error Example:
```rust
let formatted = format_human(&data)?;
println!("{}", formatted);
```
Output:
```
name : Alice age : 30 active : ✓ ```
Features: -Column alignment -Unicode symbols (✓/✗ for booleans) -Table box drawing -Configurable styling

## Data Types

### DxValue

Core enum representing all DX values.
```rust
pub enum DxValue { Object(DxObject), Array(DxArray), Table(DxTable), String(Cow<'static, str>), Int(i64), Float(f64), Bool(bool), Null, Anchor(u32), }
```
Methods:

#### as_str()

```rust
pub fn as_str(&self) -> Option<&str> ```
Returns `Some(&str)` if value is String, `None` otherwise.


#### as_int()


```rust
pub fn as_int(&self) -> Option<i64> ```
Returns `Some(i64)` if value is Int, `None` otherwise.

#### as_float()

```rust
pub fn as_float(&self) -> Option<f64> ```
Returns `Some(f64)` if value is Float, `None` otherwise.


#### as_bool()


```rust
pub fn as_bool(&self) -> Option<bool> ```
Returns `Some(bool)` if value is Bool, `None` otherwise.

#### type_name()

```rust
pub fn type_name(&self) -> &'static str ```
Returns human-readable type name. Example:
```rust
let val = DxValue::Int(42);
assert_eq!(val.type_name(), "int");
assert_eq!(val.as_int(), Some(42));
```


### DxObject


Ordered key-value map.
```rust
pub struct DxObject { pub fields: Vec<(String, DxValue)>, lookup: FxHashMap<String, usize>, }
```
Methods:


#### new()


```rust
pub fn new() -> Self ```
Creates empty object.

#### insert()

```rust
pub fn insert(&mut self, key: String, value: DxValue)
```
Inserts key-value pair (preserves order).

#### get()

```rust
pub fn get(&self, key: &str) -> Option<&DxValue> ```
Gets value by key (O(1) lookup).


#### get_mut()


```rust
pub fn get_mut(&mut self, key: &str) -> Option<&mut DxValue> ```
Gets mutable value reference.

#### contains_key()

```rust
pub fn contains_key(&self, key: &str) -> bool ```
Checks if key exists. Example:
```rust
let mut obj = DxObject::new();
obj.insert("name".into(), DxValue::String("Alice".into()));
assert!(obj.contains_key("name"));
assert_eq!(obj.get("name").unwrap().as_str(), Some("Alice"));
```


### DxArray


Dynamic array of values.
```rust
pub struct DxArray { pub elements: Vec<DxValue>, }
```
Methods:


#### new()


```rust
pub fn new() -> Self ```
Creates empty array.

#### push()

```rust
pub fn push(&mut self, value: DxValue)
```
Appends value to end.

#### get()

```rust
pub fn get(&self, index: usize) -> Option<&DxValue> ```
Gets value by index.


#### len()


```rust
pub fn len(&self) -> usize ```
Returns element count. Example:
```rust
let mut arr = DxArray::new();
arr.push(DxValue::Int(1));
arr.push(DxValue::Int(2));
assert_eq!(arr.len(), 2);
```

### DxTable

Schema-guided tabular data.
```rust
pub struct DxTable { pub schema: Schema, pub rows: Vec<Vec<DxValue>>, }
```
Fields: -`schema: Schema` — Column definitions with type hints -`rows: Vec<Vec<DxValue>>` — Data rows Example:
```rust
// Parse table let data = parse(b"users=id%i name%s age%i 1 Alice 30 2 Bob 25 ")?;
if let DxValue::Table(table) = data.get("users").unwrap() { for row in &table.rows { println!("{}: {} ({})", row[0].as_int().unwrap(), row[1].as_str().unwrap(), row[2].as_int().unwrap()
);
}
}
```

### Schema

Table column schema.
```rust
pub struct Schema { pub columns: Vec<Column>, }
pub struct Column { pub name: String, pub type_hint: TypeHint, }
pub enum TypeHint { Int, // %i Float, // %f String, // %s Bool, // %b Auto, // (inferred)
}
```
Methods:

#### parse_definition()

```rust
pub fn parse_definition(input: &[u8]) -> Result<Self, DxError> ```
Parses schema string like `"id%i name%s age%i"`. Example:
```rust
let schema = Schema::parse_definition(b"id%i name%s")?;
assert_eq!(schema.columns.len(), 2);
assert_eq!(schema.columns[0].name, "id");
assert_eq!(schema.columns[0].type_hint, TypeHint::Int);
```


## Error Handling



### DxError


Comprehensive error type.
```rust
pub enum DxError { UnexpectedEof { pos: usize }, InvalidSyntax { pos: usize, msg: String }, InvalidUtf8 { pos: usize }, InvalidNumber { pos: usize, value: String }, SchemaError { msg: String }, TypeMismatch { expected: String, found: String, pos: usize }, DuplicateKey { key: String, pos: usize }, UnknownAlias { alias: String, pos: usize }, UnknownAnchor { id: u32, pos: usize }, TableSchemaRequired { pos: usize }, IoError(std::io::Error), Utf8Error(std::str::Utf8Error), }
```
Display: All errors include position and context. Example:
```rust
match parse(b"invalid:") { Err(DxError::UnexpectedEof { pos }) => { eprintln!("Parse error at position {}", pos);
}
Err(DxError::InvalidSyntax { pos, msg }) => { eprintln!("Syntax error at {}: {}", pos, msg);
}
_ => {}
}
```


## Configuration



### EncoderConfig


Configure encoder behavior.
```rust
pub struct EncoderConfig { pub use_aliases: bool, // Generate $alias shortcuts pub use_ditto: bool, // Use " for repeated values pub alias_min_length: usize, // Minimum chars for alias }
```
Default:
```rust
EncoderConfig { use_aliases: true, use_ditto: true, alias_min_length: 8, }
```
Usage:
```rust
let config = EncoderConfig { use_ditto: true, ..Default::default()
};
let encoder = Encoder::new(config);
let bytes = encoder.encode(&data)?;
```


### FormatterConfig


Configure human formatter.
```rust
pub struct FormatterConfig { pub column_padding: usize, // Spaces between columns pub use_unicode: bool, // ✓/✗ vs true/false pub add_dividers: bool, // Table separators }
```
Default:
```rust
FormatterConfig { column_padding: 4, use_unicode: true, add_dividers: true, }
```
Usage:
```rust
let config = FormatterConfig { use_unicode: false, // ASCII only ..Default::default()
};
let formatter = HumanFormatter::new(config);
let output = formatter.format(&data)?;
```


## Advanced Usage



### Zero-Copy Parsing


```rust
use std::fs;
fn parse_file(path: &str) -> Result<DxValue, DxError> { let bytes = fs::read(path)?;
// Parser operates directly on byte slice // No string allocation, no UTF-8 validation until needed parse(&bytes)
}
```
Performance: ~70% less memory than JSON parsers.


### Streaming Large Files


For files > 100MB, use streaming parser:
```rust
use std::fs::File;
use std::io::BufReader;
fn parse_stream(path: &str) -> Result<(), DxError> { let file = File::open(path)?;
let reader = BufReader::new(file);
// TODO: Implement streaming parser // Coming in v0.2.0 Ok(())
}
```


### Custom Type Conversion


Implement `From<T>` for your types:
```rust
struct User { name: String, age: u32, }
impl From<User> for DxValue { fn from(user: User) -> Self { let mut obj = DxObject::new();
obj.insert("name".into(), DxValue::String(user.name.into()));
obj.insert("age".into(), DxValue::Int(user.age as i64));
DxValue::Object(obj)
}
}
```


## Performance Tips



### 1. Use Type Hints


Slow (type inference):
```dx
count:42 ```
Fast (explicit hint):
```dx
count%i:42 ```
Type hints enable zero-copy vacuum parsing (4-5x speedup).


### 2. Reuse Buffers


```rust
let mut buffer = Vec::with_capacity(1024);
for item in items { buffer.clear();
buffer.extend_from_slice(&encode(&item)?);
// Write buffer...
}
```
Avoids repeated allocations.


### 3. Batch Operations


Slow:
```rust
for item in items { let encoded = encode(&item)?;
write(&encoded)?;
}
```
Fast:
```rust
let mut batch = DxArray::new();
for item in items { batch.push(item);
}
let encoded = encode(&DxValue::Array(batch))?;
write(&encoded)?;
```
Amortizes encoding overhead.


## Testing



### Unit Tests


```rust

#[cfg(test)]

mod tests { use super::*;

#[test]

fn test_parse_basic() { let data = parse(b"name:Alice").unwrap();
let obj = data.as_object().unwrap();
assert_eq!(obj.get("name").unwrap().as_str(), Some("Alice"));
}
}
```
Run: `cargo test`


### Benchmarks


```rust

#[cfg(test)]

mod benches { use criterion::{black_box, Criterion};
fn bench_parse(c: &mut Criterion) { let input = b"name:Alice^age:30^active:+";
c.bench_function("parse_simple", |b| { b.iter(|| parse(black_box(input)))
});
}
}
```
Run: `cargo bench`


## Integration Examples



### Axum Web Server


```rust
use axum::{Json, extract::Path};
async fn get_user(Path(id): Path<u32>) -> Json<DxValue> { let data = parse(&fetch_user_dx(id)).unwrap();
Json(data)
}
```


### Serde Integration (v0.2.0)


```rust
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]

struct User { name: String, age: u32, }
let user = User { name: "Alice".into(), age: 30 };
let bytes = dx_serializer::to_bytes(&user)?;
let parsed: User = dx_serializer::from_bytes(&bytes)?;
```
See Also: -Syntax Guide (SYNTAX.md) -Performance Guide (PERFORMANCE.md) -Examples (../examples/)