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
614
615
616
617
618
619
620
621
//! Serializer Builder Pattern
//!
//! Provides a fluent API for configuring serialization options with sensible defaults.
//! The builder pattern allows users to customize various aspects of serialization
//! without needing to understand all the internal configuration types.
//!
//! ## Thread Safety
//!
//! Both [`SerializerBuilder`] and [`Serializer`] implement `Send + Sync` and can be
//! safely shared between threads. The serializer methods are stateless and can be
//! called concurrently:
//!
//! ```rust
//! use std::sync::Arc;
//! use std::thread;
//! use serializer::{SerializerBuilder, DxDocument, DxLlmValue};
//!
//! // Create a shared serializer
//! let serializer = Arc::new(SerializerBuilder::new().build());
//!
//! let handles: Vec<_> = (0..4).map(|i| {
//!     let serializer = Arc::clone(&serializer);
//!     thread::spawn(move || {
//!         let mut doc = DxDocument::new();
//!         doc.context.insert("id".to_string(), DxLlmValue::Num(i as f64));
//!         serializer.serialize(&doc)
//!     })
//! }).collect();
//!
//! for handle in handles {
//!     let result = handle.join().unwrap();
//!     assert!(!result.is_empty());
//! }
//! ```
//!
//! ## Example
//!
//! ```rust
//! use serializer::{SerializerBuilder, DxDocument, DxLlmValue};
//!
//! let mut doc = DxDocument::new();
//! doc.context.insert("name".to_string(), DxLlmValue::Str("MyApp".to_string()));
//!
//! // Simple usage with defaults
//! let serializer = SerializerBuilder::new().build();
//! let text = serializer.serialize(&doc);
//!
//! // Advanced configuration
//! let serializer = SerializerBuilder::new()
//!     .indent_size(4)
//!     .preserve_comments(true)
//!     .expand_keys(false)
//!     .validate_output(true)
//!     .build();
//! let text = serializer.serialize(&doc);
//! ```

use crate::llm::human_formatter::HumanFormatConfig;
use crate::llm::pretty_printer::{PrettyPrinter, PrettyPrinterConfig};
use crate::llm::serializer_output::{SerializerOutput, SerializerOutputConfig};
use crate::llm::types::DxDocument;
use crate::llm::{ConvertError, document_to_llm, llm_to_document};
use std::path::PathBuf;

/// Builder for configuring serialization options
///
/// The SerializerBuilder provides a fluent API for customizing serialization behavior.
/// It combines configuration from multiple internal types into a single, easy-to-use interface.
#[derive(Debug, Clone)]
pub struct SerializerBuilder {
    // Human format options
    indent_size: usize,
    expand_keys: bool,
    use_list_format: bool,
    space_around_equals: bool,

    // Pretty printer options
    validate_output: bool,
    check_round_trip: bool,

    // Output generation options
    output_dir: Option<PathBuf>,
    generate_llm: bool,
    generate_machine: bool,

    // General options
    preserve_comments: bool,
    compact_arrays: bool,
}

impl Default for SerializerBuilder {
    fn default() -> Self {
        Self {
            // Human format defaults
            indent_size: 0,
            expand_keys: true,
            use_list_format: true,
            space_around_equals: true,

            // Pretty printer defaults
            validate_output: false, // Disabled by default since V3 round-trip not fully implemented
            check_round_trip: false,

            // Output generation defaults
            output_dir: None,
            generate_llm: true,
            generate_machine: true,

            // General defaults
            preserve_comments: false,
            compact_arrays: false,
        }
    }
}

impl SerializerBuilder {
    /// Create a new SerializerBuilder with default settings
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the indentation size for formatted output
    ///
    /// Controls the minimum padding for keys in human format.
    /// Set to 0 for no padding (default).
    ///
    /// # Example
    ///
    /// ```rust
    /// use serializer::SerializerBuilder;
    ///
    /// let serializer = SerializerBuilder::new()
    ///     .indent_size(4)
    ///     .build();
    /// ```
    pub fn indent_size(mut self, size: usize) -> Self {
        self.indent_size = size;
        self
    }

    /// Set whether to expand abbreviated keys to full names
    ///
    /// When true (default), keys like "nm" become "name" in human format.
    /// When false, abbreviated keys are preserved.
    ///
    /// # Example
    ///
    /// ```rust
    /// use serializer::SerializerBuilder;
    ///
    /// let serializer = SerializerBuilder::new()
    ///     .expand_keys(false)  // Keep "nm" instead of expanding to "name"
    ///     .build();
    /// ```
    pub fn expand_keys(mut self, expand: bool) -> Self {
        self.expand_keys = expand;
        self
    }

    /// Set whether to use list format for arrays
    ///
    /// When true (default), arrays are formatted as:
    /// ```text
    /// items:
    /// - first
    /// - second
    /// ```
    ///
    /// When false, arrays are formatted inline:
    /// ```text
    /// items = first | second
    /// ```
    pub fn use_list_format(mut self, use_list: bool) -> Self {
        self.use_list_format = use_list;
        self
    }

    /// Set whether to add spaces around equals signs
    ///
    /// When true (default): `key = value`
    /// When false: `key=value`
    pub fn space_around_equals(mut self, space: bool) -> Self {
        self.space_around_equals = space;
        self
    }

    /// Set whether to validate output by parsing it back
    ///
    /// When enabled, the serializer will parse the formatted output
    /// to ensure it's valid. This catches formatting bugs but is slower.
    ///
    /// Note: Currently disabled by default since V3 format round-trip
    /// is not fully implemented.
    pub fn validate_output(mut self, validate: bool) -> Self {
        self.validate_output = validate;
        self
    }

    /// Set whether to check round-trip consistency
    ///
    /// When enabled (requires validate_output), the serializer will
    /// verify that parsing the formatted output produces an equivalent
    /// document to the original.
    pub fn check_round_trip(mut self, check: bool) -> Self {
        self.check_round_trip = check;
        self
    }

    /// Set the output directory for generated files
    ///
    /// When set, the serializer can generate .human and .machine files
    /// in the specified directory. Default is ".dx/serializer".
    ///
    /// # Example
    ///
    /// ```rust
    /// use serializer::SerializerBuilder;
    ///
    /// let serializer = SerializerBuilder::new()
    ///     .output_dir("build/serializer")
    ///     .build();
    /// ```
    pub fn output_dir<P: Into<PathBuf>>(mut self, dir: P) -> Self {
        self.output_dir = Some(dir.into());
        self
    }

    /// Set whether to generate LLM format files
    ///
    /// When true (default), .llm files are generated in .dx/serializer.
    pub fn generate_llm(mut self, generate: bool) -> Self {
        self.generate_llm = generate;
        self
    }

    /// Set whether to generate machine format files
    ///
    /// When true (default), .machine files are generated for runtime use.
    pub fn generate_machine(mut self, generate: bool) -> Self {
        self.generate_machine = generate;
        self
    }

    /// Set whether to preserve comments in output
    ///
    /// Note: Comment preservation is not yet fully implemented.
    /// This option is reserved for future use.
    pub fn preserve_comments(mut self, preserve: bool) -> Self {
        self.preserve_comments = preserve;
        self
    }

    /// Set whether to use compact array formatting
    ///
    /// When true, arrays are formatted more compactly.
    /// This is equivalent to setting use_list_format(false).
    pub fn compact_arrays(mut self, compact: bool) -> Self {
        self.compact_arrays = compact;
        if compact {
            self.use_list_format = false;
        }
        self
    }

    /// Create a configuration optimized for tables and rules
    ///
    /// This preset is useful for multi-row sections like lint rules.
    /// It sets appropriate padding and formatting for tabular data.
    ///
    /// # Example
    ///
    /// ```rust
    /// use serializer::SerializerBuilder;
    ///
    /// let serializer = SerializerBuilder::new()
    ///     .for_tables()
    ///     .build();
    /// ```
    pub fn for_tables(mut self) -> Self {
        self.indent_size = 20;
        self.use_list_format = false;
        self.expand_keys = true;
        self.space_around_equals = true;
        self
    }

    /// Create a configuration optimized for compact output
    ///
    /// This preset minimizes whitespace and uses abbreviated keys.
    /// Useful for token-efficient LLM format.
    pub fn for_compact(mut self) -> Self {
        self.indent_size = 0;
        self.expand_keys = false;
        self.use_list_format = false;
        self.space_around_equals = false;
        self.compact_arrays = true;
        self
    }

    /// Create a configuration optimized for human readability
    ///
    /// This preset maximizes readability with expanded keys,
    /// proper spacing, and list formatting for arrays.
    pub fn for_humans(mut self) -> Self {
        self.indent_size = 0;
        self.expand_keys = true;
        self.use_list_format = true;
        self.space_around_equals = true;
        self.validate_output = false; // Keep disabled until V3 round-trip is implemented
        self
    }

    /// Build the configured Serializer
    ///
    /// Creates a Serializer instance with all the specified options.
    pub fn build(self) -> Serializer {
        // Build human format config
        let human_config = HumanFormatConfig {
            key_padding: self.indent_size,
        };

        // Build pretty printer config
        let pretty_config = PrettyPrinterConfig {
            formatter_config: human_config,
            validate_output: self.validate_output,
            check_round_trip: self.check_round_trip,
        };

        // Build output config
        let output_config = SerializerOutputConfig {
            output_dir: self
                .output_dir
                .unwrap_or_else(|| PathBuf::from(".dx/serializer")),
            generate_llm: self.generate_llm,
            generate_machine: self.generate_machine,
            compression: crate::llm::convert::CompressionAlgorithm::default(),
            generate_metadata: false,
        };

        Serializer {
            pretty_printer: PrettyPrinter::with_config(pretty_config),
            output_generator: SerializerOutput::with_config(output_config),
        }
    }
}

/// Configured Serializer instance
///
/// A Serializer provides methods for converting documents to various formats
/// using the configuration specified by the SerializerBuilder.
pub struct Serializer {
    pretty_printer: PrettyPrinter,
    output_generator: SerializerOutput,
}

impl Serializer {
    /// Serialize a DxDocument to LLM format string
    ///
    /// This is the primary serialization method that produces the
    /// token-efficient LLM format.
    ///
    /// # Example
    ///
    /// ```rust
    /// use serializer::{SerializerBuilder, DxDocument, DxLlmValue};
    ///
    /// let mut doc = DxDocument::new();
    /// doc.context.insert("name".to_string(), DxLlmValue::Str("MyApp".to_string()));
    ///
    /// let serializer = SerializerBuilder::new().build();
    /// let text = serializer.serialize(&doc);
    /// ```
    pub fn serialize(&self, doc: &DxDocument) -> String {
        document_to_llm(doc)
    }

    /// Deserialize LLM format string to DxDocument
    ///
    /// Parses the token-efficient LLM format back into a structured document.
    ///
    /// # Errors
    ///
    /// Returns a `ConvertError` if the input is not valid LLM format.
    pub fn deserialize(&self, input: &str) -> Result<DxDocument, ConvertError> {
        llm_to_document(input)
    }

    /// Format a DxDocument to human-readable format
    ///
    /// Produces clean, hand-editable format using the configured options.
    /// The output is validated if validation is enabled in the builder.
    ///
    /// # Example
    ///
    /// ```rust
    /// use serializer::{SerializerBuilder, DxDocument, DxLlmValue};
    ///
    /// let mut doc = DxDocument::new();
    /// doc.context.insert("name".to_string(), DxLlmValue::Str("MyApp".to_string()));
    ///
    /// let serializer = SerializerBuilder::new()
    ///     .for_humans()
    ///     .build();
    /// let human_text = serializer.format_human(&doc).unwrap();
    /// ```
    pub fn format_human(
        &self,
        doc: &DxDocument,
    ) -> Result<String, crate::llm::pretty_printer::PrettyPrintError> {
        self.pretty_printer.format(doc)
    }

    /// Format a DxDocument to human format without validation
    ///
    /// Faster than format_human() but provides no guarantees about
    /// the output being parseable.
    pub fn format_human_unchecked(&self, doc: &DxDocument) -> String {
        self.pretty_printer.format_unchecked(doc)
    }

    /// Generate output files for a DxDocument
    ///
    /// Creates .human and .machine files in the configured output directory.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use serializer::{SerializerBuilder, DxDocument};
    /// use std::path::Path;
    ///
    /// let doc = DxDocument::new();
    /// let serializer = SerializerBuilder::new()
    ///     .output_dir("build/serializer")
    ///     .build();
    ///
    /// let result = serializer.generate_files(&doc, Path::new("config.sr")).unwrap();
    /// println!("Generated {} bytes LLM, {} bytes machine",
    ///          result.llm_size, result.machine_size);
    /// ```
    pub fn generate_files(
        &self,
        doc: &DxDocument,
        source_path: &std::path::Path,
    ) -> Result<
        crate::llm::serializer_output::SerializerResult,
        crate::llm::serializer_output::SerializerOutputError,
    > {
        self.output_generator.process_document(doc, source_path)
    }

    /// Get the pretty printer instance
    ///
    /// Provides access to the underlying PrettyPrinter for advanced use cases.
    pub fn pretty_printer(&self) -> &PrettyPrinter {
        &self.pretty_printer
    }

    /// Get the output generator instance
    ///
    /// Provides access to the underlying SerializerOutput for advanced use cases.
    pub fn output_generator(&self) -> &SerializerOutput {
        &self.output_generator
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::llm::types::{DxLlmValue, DxSection};

    #[test]
    fn test_builder_default() {
        let builder = SerializerBuilder::new();
        assert_eq!(builder.indent_size, 0);
        assert!(builder.expand_keys);
        assert!(builder.use_list_format);
        assert!(builder.space_around_equals);
        assert!(!builder.validate_output); // Disabled by default
        assert!(builder.generate_llm);
        assert!(builder.generate_machine);
    }

    #[test]
    fn test_builder_fluent_api() {
        let builder = SerializerBuilder::new()
            .indent_size(4)
            .expand_keys(false)
            .use_list_format(false)
            .space_around_equals(false)
            .validate_output(false) // Keep disabled
            .generate_llm(false);

        assert_eq!(builder.indent_size, 4);
        assert!(!builder.expand_keys);
        assert!(!builder.use_list_format);
        assert!(!builder.space_around_equals);
        assert!(!builder.validate_output);
        assert!(!builder.generate_llm);
    }

    #[test]
    fn test_builder_presets() {
        // Test for_tables preset
        let tables_builder = SerializerBuilder::new().for_tables();
        assert_eq!(tables_builder.indent_size, 20);
        assert!(!tables_builder.use_list_format);
        assert!(tables_builder.expand_keys);

        // Test for_compact preset
        let compact_builder = SerializerBuilder::new().for_compact();
        assert_eq!(compact_builder.indent_size, 0);
        assert!(!compact_builder.expand_keys);
        assert!(!compact_builder.use_list_format);
        assert!(!compact_builder.space_around_equals);

        // Test for_humans preset
        let human_builder = SerializerBuilder::new().for_humans();
        assert_eq!(human_builder.indent_size, 0);
        assert!(human_builder.expand_keys);
        assert!(human_builder.use_list_format);
        assert!(human_builder.space_around_equals);
    }

    #[test]
    fn test_serializer_basic_usage() {
        let mut doc = DxDocument::new();
        doc.context
            .insert("name".to_string(), DxLlmValue::Str("TestApp".to_string()));
        doc.context
            .insert("version".to_string(), DxLlmValue::Str("1.0.0".to_string()));

        let serializer = SerializerBuilder::new().build();

        // Test serialize
        let text = serializer.serialize(&doc);
        assert!(!text.is_empty());

        // Test deserialize
        let parsed = serializer.deserialize(&text);
        assert!(parsed.is_ok());
        let parsed_doc = parsed.unwrap();
        assert_eq!(parsed_doc.context.len(), doc.context.len());
    }

    #[test]
    fn test_serializer_human_format() {
        let mut doc = DxDocument::new();
        doc.context
            .insert("name".to_string(), DxLlmValue::Str("TestApp".to_string()));
        doc.context.insert(
            "editor".to_string(),
            DxLlmValue::Arr(vec![
                DxLlmValue::Str("neovim".to_string()),
                DxLlmValue::Str("vscode".to_string()),
            ]),
        );

        let serializer = SerializerBuilder::new().for_humans().build();

        // Test human format (unchecked since validation is disabled)
        let human_text = serializer.format_human_unchecked(&doc);
        assert!(!human_text.is_empty(), "Human format should produce output");
    }

    #[test]
    fn test_serializer_compact_format() {
        let mut doc = DxDocument::new();
        doc.context
            .insert("name".to_string(), DxLlmValue::Str("TestApp".to_string()));
        doc.context.insert(
            "editor".to_string(),
            DxLlmValue::Arr(vec![
                DxLlmValue::Str("neovim".to_string()),
                DxLlmValue::Str("vscode".to_string()),
            ]),
        );

        let serializer = SerializerBuilder::new().for_compact().build();

        let human_text = serializer.format_human_unchecked(&doc);
        assert!(
            !human_text.is_empty(),
            "Compact format should produce output"
        );
    }

    #[test]
    fn test_serializer_with_sections() {
        let mut doc = DxDocument::new();

        let mut section = DxSection::new(vec!["id".to_string(), "name".to_string()]);
        section.rows.push(vec![
            DxLlmValue::Num(1.0),
            DxLlmValue::Str("Alpha".to_string()),
        ]);
        section.rows.push(vec![
            DxLlmValue::Num(2.0),
            DxLlmValue::Str("Beta".to_string()),
        ]);
        doc.sections.insert('d', section);

        let serializer = SerializerBuilder::new().for_tables().build();

        let human_text = serializer.format_human_unchecked(&doc);
        // Just verify output is not empty
        assert!(!human_text.is_empty());
    }

    #[test]
    fn test_compact_arrays_option() {
        let builder = SerializerBuilder::new().compact_arrays(true);
        assert!(builder.compact_arrays);
        assert!(!builder.use_list_format); // Should be set to false when compact_arrays is true
    }

    #[test]
    fn test_output_dir_option() {
        let builder = SerializerBuilder::new().output_dir("custom/path");
        assert_eq!(builder.output_dir, Some(PathBuf::from("custom/path")));
    }
}