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
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
//! Property tests for Task 5.3: Compact syntax parsing
//!
//! Feature: dx-serializer-production-ready
//! Property 19: Compact Syntax Marker Recognition
//! Property 20: Compact Syntax Key-Value Parsing
//! Property 21: Compact Syntax Object Structure
//! Validates: Requirements 5.1, 5.2, 5.3

use proptest::prelude::*;
use serializer::llm::parser::LlmParser;
use serializer::llm::types::DxLlmValue;
use std::collections::HashMap;

/// Strategy to generate valid identifier strings (keys)
fn valid_identifier() -> impl Strategy<Value = String> {
    "[a-z][a-z0-9_]{0,15}".prop_map(|s| s.to_string())
}

/// Strategy to generate valid simple values (no nested structures)
fn simple_value() -> impl Strategy<Value = String> {
    prop_oneof![
        // String values (alphanumeric, no spaces)
        "[a-zA-Z][a-zA-Z0-9._-]{0,20}".prop_map(|s| s.to_string()),
        // Integer values
        (-10000i64..10000i64).prop_map(|n| n.to_string()),
        // Float values
        (-1000.0f64..1000.0f64)
            .prop_filter("finite", |f| f.is_finite())
            .prop_map(|f| format!("{:.2}", f)),
        // Boolean values
        prop::bool::ANY.prop_map(|b| if b {
            "true".to_string()
        } else {
            "false".to_string()
        }),
    ]
}

/// Strategy to generate a single key-value pair
fn key_value_pair() -> impl Strategy<Value = (String, String)> {
    (valid_identifier(), simple_value())
}

/// Strategy to generate a map of key-value pairs (1-10 pairs)
fn key_value_map() -> impl Strategy<Value = HashMap<String, String>> {
    prop::collection::vec(key_value_pair(), 1..=10)
        .prop_map(|pairs| {
            let mut map = HashMap::new();
            for (k, v) in pairs {
                map.insert(k, v);
            }
            map
        })
        .prop_filter("unique keys", |map| !map.is_empty())
}

/// Helper to parse a simple value string into expected DxLlmValue
fn parse_expected_value(value_str: &str) -> DxLlmValue {
    // Try to parse as number
    if let Ok(num) = value_str.parse::<f64>() {
        return DxLlmValue::Num(num);
    }

    // Try to parse as boolean
    match value_str {
        "true" => return DxLlmValue::Bool(true),
        "false" => return DxLlmValue::Bool(false),
        _ => {}
    }

    // Otherwise it's a string
    DxLlmValue::Str(value_str.to_string())
}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    /// Feature: dx-serializer-production-ready, Property 19: Compact Syntax Marker Recognition
    /// **Validates: Requirements 5.1**
    ///
    /// For any compact syntax string with the `@=` marker, parsing should recognize
    /// the marker and parse the content as a compact object.
    #[test]
    fn prop_compact_syntax_marker_recognition(
        section_name in valid_identifier(),
        fields in key_value_map()
    ) {
        // Build the compact syntax string with @= marker
        let count = fields.len();
        let tokens: Vec<String> = fields
            .iter()
            .flat_map(|(k, v)| vec![k.clone(), v.clone()])
            .collect();
        let input = format!("{}:{}@=[{}]", section_name, count, tokens.join(" "));

        // Parse the input
        let result = LlmParser::parse(&input);

        prop_assert!(
            result.is_ok(),
            "Failed to parse valid compact syntax with @= marker: {:?}\nInput: {}",
            result.err(),
            input
        );

        let doc = result.unwrap();

        // Check that the section exists in the document context
        prop_assert!(
            doc.context.contains_key(&section_name),
            "Section '{}' not found in document context. Available keys: {:?}\nInput: {}",
            section_name,
            doc.context.keys().collect::<Vec<_>>(),
            input
        );

        // Check that it's an object (marker was recognized)
        prop_assert!(
            matches!(doc.context.get(&section_name), Some(DxLlmValue::Obj(_))),
            "Expected Obj variant for section '{}' (marker should be recognized), got {:?}\nInput: {}",
            section_name,
            doc.context.get(&section_name),
            input
        );
    }

    /// Feature: dx-serializer-production-ready, Property 20: Compact Syntax Key-Value Parsing
    /// **Validates: Requirements 5.2**
    ///
    /// For any compact syntax content in the format `@=[key value key value]`, parsing
    /// should correctly extract all key-value pairs without requiring `=` signs between them.
    #[test]
    fn prop_compact_syntax_key_value_parsing(
        section_name in valid_identifier(),
        fields in key_value_map()
    ) {
        // Build the compact syntax string with space-separated key-value pairs (no = signs)
        let count = fields.len();
        let tokens: Vec<String> = fields
            .iter()
            .flat_map(|(k, v)| vec![k.clone(), v.clone()])
            .collect();
        let input = format!("{}:{}@=[{}]", section_name, count, tokens.join(" "));

        // Parse the input
        let result = LlmParser::parse(&input);

        prop_assert!(
            result.is_ok(),
            "Failed to parse compact syntax key-value pairs: {:?}\nInput: {}",
            result.err(),
            input
        );

        let doc = result.unwrap();

        // Check that it's an object
        if let Some(DxLlmValue::Obj(parsed_fields)) = doc.context.get(&section_name) {
            // Check that all fields are present
            prop_assert_eq!(
                parsed_fields.len(),
                fields.len(),
                "Expected {} fields, got {}. Input: {}\nExpected keys: {:?}\nGot keys: {:?}",
                fields.len(),
                parsed_fields.len(),
                input,
                fields.keys().collect::<Vec<_>>(),
                parsed_fields.keys().collect::<Vec<_>>()
            );

            // Check each field value
            for (key, expected_value_str) in &fields {
                prop_assert!(
                    parsed_fields.contains_key(key),
                    "Key '{}' not found in parsed object. Available keys: {:?}\nInput: {}",
                    key,
                    parsed_fields.keys().collect::<Vec<_>>(),
                    input
                );

                let parsed_value = &parsed_fields[key];
                let expected_value = parse_expected_value(expected_value_str);

                // Compare values based on type
                match (&expected_value, parsed_value) {
                    (DxLlmValue::Num(exp), DxLlmValue::Num(got)) => {
                        // Use approximate equality for floats
                        let diff = (exp - got).abs();
                        prop_assert!(
                            diff < 0.01,
                            "Numeric value mismatch for key '{}': expected {}, got {}\nInput: {}",
                            key,
                            exp,
                            got,
                            input
                        );
                    }
                    (DxLlmValue::Bool(exp), DxLlmValue::Bool(got)) => {
                        prop_assert_eq!(
                            exp,
                            got,
                            "Boolean value mismatch for key '{}'\nInput: {}",
                            key,
                            input
                        );
                    }
                    (DxLlmValue::Str(exp), DxLlmValue::Str(got)) => {
                        prop_assert_eq!(
                            exp,
                            got,
                            "String value mismatch for key '{}'\nInput: {}",
                            key,
                            input
                        );
                    }
                    _ => {
                        prop_assert!(
                            false,
                            "Type mismatch for key '{}': expected {:?}, got {:?}\nInput: {}",
                            key,
                            expected_value.type_name(),
                            parsed_value.type_name(),
                            input
                        );
                    }
                }
            }
        } else {
            prop_assert!(
                false,
                "Expected Obj variant for section '{}', got {:?}\nInput: {}",
                section_name,
                doc.context.get(&section_name),
                input
            );
        }
    }

    /// Feature: dx-serializer-production-ready, Property 21: Compact Syntax Object Structure
    /// **Validates: Requirements 5.3**
    ///
    /// For any compact syntax string, parsing should produce a DxDocument with a properly
    /// structured object containing all key-value pairs.
    #[test]
    fn prop_compact_syntax_object_structure(
        section_name in valid_identifier(),
        fields in key_value_map()
    ) {
        // Build the compact syntax string
        let count = fields.len();
        let tokens: Vec<String> = fields
            .iter()
            .flat_map(|(k, v)| vec![k.clone(), v.clone()])
            .collect();
        let input = format!("{}:{}@=[{}]", section_name, count, tokens.join(" "));

        // Parse the input
        let result = LlmParser::parse(&input);

        prop_assert!(
            result.is_ok(),
            "Failed to parse compact syntax: {:?}\nInput: {}",
            result.err(),
            input
        );

        let doc = result.unwrap();

        // Verify the document structure
        prop_assert!(
            doc.context.contains_key(&section_name),
            "Section '{}' not found in document context\nInput: {}",
            section_name,
            input
        );

        // Verify it's a properly structured object
        if let Some(DxLlmValue::Obj(parsed_fields)) = doc.context.get(&section_name) {
            // Check that the object is not empty (unless input was empty)
            if !fields.is_empty() {
                prop_assert!(
                    !parsed_fields.is_empty(),
                    "Parsed object is empty but input had {} fields\nInput: {}",
                    fields.len(),
                    input
                );
            }

            // Check that all keys are valid identifiers (no corruption)
            for key in parsed_fields.keys() {
                prop_assert!(
                    !key.is_empty(),
                    "Found empty key in parsed object\nInput: {}",
                    input
                );
                prop_assert!(
                    key.chars().all(|c| c.is_alphanumeric() || c == '_'),
                    "Found invalid key '{}' in parsed object\nInput: {}",
                    key,
                    input
                );
            }

            // Check that all values are valid (not corrupted)
            for (key, value) in parsed_fields.iter() {
                match value {
                    DxLlmValue::Str(s) => {
                        prop_assert!(
                            !s.is_empty() || fields.get(key).map(|v| v.is_empty()).unwrap_or(false),
                            "Found empty string value for key '{}' but input was not empty\nInput: {}",
                            key,
                            input
                        );
                    }
                    DxLlmValue::Num(n) => {
                        prop_assert!(
                            n.is_finite(),
                            "Found non-finite number for key '{}': {}\nInput: {}",
                            key,
                            n,
                            input
                        );
                    }
                    DxLlmValue::Bool(_) => {
                        // Booleans are always valid
                    }
                    _ => {
                        prop_assert!(
                            false,
                            "Found unexpected value type for key '{}': {:?}\nInput: {}",
                            key,
                            value.type_name(),
                            input
                        );
                    }
                }
            }

            // Check that the number of fields matches
            prop_assert_eq!(
                parsed_fields.len(),
                fields.len(),
                "Field count mismatch\nInput: {}",
                input
            );
        } else {
            prop_assert!(
                false,
                "Expected Obj variant for section '{}', got {:?}\nInput: {}",
                section_name,
                doc.context.get(&section_name),
                input
            );
        }
    }

    /// Property 19 variant: Compact syntax with single field
    ///
    /// For any compact syntax with a single key-value pair, parsing should work correctly.
    #[test]
    fn prop_compact_syntax_single_field(
        section_name in valid_identifier(),
        key in valid_identifier(),
        value in simple_value()
    ) {
        let input = format!("{}:1@=[{} {}]", section_name, key, value);

        let result = LlmParser::parse(&input);

        prop_assert!(
            result.is_ok(),
            "Failed to parse single-field compact syntax: {:?}\nInput: {}",
            result.err(),
            input
        );

        let doc = result.unwrap();

        if let Some(DxLlmValue::Obj(parsed_fields)) = doc.context.get(&section_name) {
            prop_assert_eq!(
                parsed_fields.len(),
                1,
                "Expected 1 field, got {}\nInput: {}",
                parsed_fields.len(),
                input
            );

            prop_assert!(
                parsed_fields.contains_key(&key),
                "Key '{}' not found in parsed object\nInput: {}",
                key,
                input
            );
        } else {
            prop_assert!(
                false,
                "Expected Obj variant for section '{}'\nInput: {}",
                section_name,
                input
            );
        }
    }

    /// Property 20 variant: Compact syntax with multiple spaces
    ///
    /// For any compact syntax with multiple spaces between tokens,
    /// parsing should handle whitespace correctly.
    #[test]
    fn prop_compact_syntax_multiple_spaces(
        section_name in valid_identifier(),
        fields in key_value_map()
    ) {
        // Build the compact syntax string with multiple spaces between tokens
        let count = fields.len();
        let tokens: Vec<String> = fields
            .iter()
            .flat_map(|(k, v)| vec![k.clone(), v.clone()])
            .collect();
        // Use 2-5 spaces between tokens
        let input = format!("{}:{}@=[{}]", section_name, count, tokens.join("   "));

        let result = LlmParser::parse(&input);

        prop_assert!(
            result.is_ok(),
            "Failed to parse compact syntax with multiple spaces: {:?}\nInput: {}",
            result.err(),
            input
        );

        let doc = result.unwrap();

        if let Some(DxLlmValue::Obj(parsed_fields)) = doc.context.get(&section_name) {
            prop_assert_eq!(
                parsed_fields.len(),
                fields.len(),
                "Expected {} fields, got {}\nInput: {}",
                fields.len(),
                parsed_fields.len(),
                input
            );
        } else {
            prop_assert!(
                false,
                "Expected Obj variant for section '{}'\nInput: {}",
                section_name,
                input
            );
        }
    }

    /// Property 20 variant: Compact syntax with odd number of tokens should fail
    ///
    /// For any compact syntax with an odd number of tokens (unpaired key-value),
    /// parsing should return an error.
    #[test]
    fn prop_compact_syntax_odd_tokens_fails(
        section_name in valid_identifier(),
        tokens in prop::collection::vec(valid_identifier(), 1..=9)
            .prop_filter("odd length", |v| v.len() % 2 == 1)
    ) {
        let count = tokens.len() / 2; // Intentionally wrong count
        let input = format!("{}:{}@=[{}]", section_name, count, tokens.join(" "));

        let result = LlmParser::parse(&input);

        // Should fail because odd number of tokens
        prop_assert!(
            result.is_err(),
            "Expected error for odd number of tokens, but parsing succeeded\nInput: {}",
            input
        );
    }
}

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

    #[test]
    fn test_simple_compact_syntax() {
        let input = "config:2@=[host localhost port 8080]";
        let doc = LlmParser::parse(input).unwrap();

        assert!(doc.context.contains_key("config"));
        if let Some(DxLlmValue::Obj(fields)) = doc.context.get("config") {
            assert_eq!(fields.len(), 2);
            assert!(fields.contains_key("host"));
            assert!(fields.contains_key("port"));
            assert_eq!(fields.get("host").unwrap().as_str(), Some("localhost"));
            assert_eq!(fields.get("port").unwrap().as_num(), Some(8080.0));
        } else {
            panic!("Expected Obj variant");
        }
    }

    #[test]
    fn test_compact_syntax_with_numbers() {
        let input = "version:3@=[major 2 minor 1 patch 0]";
        let doc = LlmParser::parse(input).unwrap();

        if let Some(DxLlmValue::Obj(fields)) = doc.context.get("version") {
            assert_eq!(fields.len(), 3);
            assert_eq!(fields.get("major").unwrap().as_num(), Some(2.0));
            assert_eq!(fields.get("minor").unwrap().as_num(), Some(1.0));
            assert_eq!(fields.get("patch").unwrap().as_num(), Some(0.0));
        } else {
            panic!("Expected Obj variant");
        }
    }

    #[test]
    fn test_compact_syntax_with_booleans() {
        let input = "flags:2@=[debug true production false]";
        let doc = LlmParser::parse(input).unwrap();

        if let Some(DxLlmValue::Obj(fields)) = doc.context.get("flags") {
            assert_eq!(fields.len(), 2);
            assert_eq!(fields.get("debug").unwrap().as_bool(), Some(true));
            assert_eq!(fields.get("production").unwrap().as_bool(), Some(false));
        } else {
            panic!("Expected Obj variant");
        }
    }

    #[test]
    fn test_compact_syntax_single_field() {
        let input = "name:1@=[value test]";
        let doc = LlmParser::parse(input).unwrap();

        if let Some(DxLlmValue::Obj(fields)) = doc.context.get("name") {
            assert_eq!(fields.len(), 1);
            assert_eq!(fields.get("value").unwrap().as_str(), Some("test"));
        } else {
            panic!("Expected Obj variant");
        }
    }

    #[test]
    fn test_compact_syntax_empty() {
        let input = "empty:0@=[]";
        let doc = LlmParser::parse(input).unwrap();

        assert!(doc.context.contains_key("empty"));
        // Empty compact syntax should produce an empty object or null
        let value = doc.context.get("empty").unwrap();
        assert!(
            matches!(value, DxLlmValue::Obj(map) if map.is_empty())
                || matches!(value, DxLlmValue::Null)
        );
    }

    #[test]
    fn test_compact_syntax_odd_tokens_error() {
        // Odd number of tokens should fail
        let input = "config:1@=[host localhost port]";
        let result = LlmParser::parse(input);

        assert!(result.is_err(), "Expected error for odd number of tokens");
    }

    #[test]
    fn test_compact_syntax_multiple_spaces() {
        let input = "config:2@=[host   localhost   port   8080]";
        let doc = LlmParser::parse(input).unwrap();

        if let Some(DxLlmValue::Obj(fields)) = doc.context.get("config") {
            assert_eq!(fields.len(), 2);
            assert!(fields.contains_key("host"));
            assert!(fields.contains_key("port"));
        } else {
            panic!("Expected Obj variant");
        }
    }

    #[test]
    fn test_compact_syntax_marker_recognition() {
        // Test that @= marker is recognized and not confused with regular inline objects
        let input = "config:2@=[key value]";
        let doc = LlmParser::parse(input).unwrap();

        // Should parse as compact syntax (no = between key and value)
        assert!(doc.context.contains_key("config"));
        assert!(matches!(
            doc.context.get("config"),
            Some(DxLlmValue::Obj(_))
        ));
    }

    #[test]
    fn test_compact_syntax_vs_regular_object() {
        // Regular object with = signs
        let input1 = "config:2[key=value other=data]";
        let doc1 = LlmParser::parse(input1).unwrap();

        // Compact syntax without = signs
        let input2 = "config:2@=[key value other data]";
        let doc2 = LlmParser::parse(input2).unwrap();

        // Both should produce objects with the same keys
        if let (Some(DxLlmValue::Obj(fields1)), Some(DxLlmValue::Obj(fields2))) =
            (doc1.context.get("config"), doc2.context.get("config"))
        {
            assert_eq!(fields1.len(), fields2.len());
            assert!(fields1.contains_key("key"));
            assert!(fields2.contains_key("key"));
            assert!(fields1.contains_key("other"));
            assert!(fields2.contains_key("other"));
        } else {
            panic!("Expected Obj variants for both");
        }
    }
}