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
//! Cache Generator for DX Serializer
//!
//! Automatically generates LLM and Machine format cache files from Human format sources.
//! Cache files are stored in `.dx/cache` with path preservation.
//!
//! ## Cache Structure
//!
//! ```text
//! .dx/cache/
//! ├── llm/
//! │   ├── config.dx.llm
//! │   └── subdir/
//! │       └── data.dx.llm
//! └── machine/
//!     ├── config.dx.bin
//!     └── subdir/
//!         └── data.dx.bin
//! ```

use crate::llm::convert::{ConvertError, document_to_llm, document_to_machine};
use crate::llm::human_parser::HumanParser;
use crate::llm::types::DxDocument;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use thiserror::Error;

/// Cache generation errors
#[derive(Debug, Error)]
pub enum CacheError {
    /// Filesystem or stream error while reading or writing cache files.
    #[error("IO error: {0}")]
    Io(#[from] io::Error),

    /// Human-format parse failure before cache generation.
    #[error("Parse error: {0}")]
    Parse(String),

    /// Format conversion failure while creating cache outputs.
    #[error("Conversion error: {0}")]
    Convert(#[from] ConvertError),

    /// Source or destination path failed validation.
    #[error("Invalid path: {0}")]
    InvalidPath(String),

    /// Cache directory could not be created.
    #[error("Cache directory creation failed: {0}")]
    DirectoryCreation(String),
}

/// Configuration for cache generation
#[derive(Debug, Clone)]
pub struct CacheConfig {
    /// Root directory for cache files (default: .dx/cache)
    pub cache_root: PathBuf,
    /// Generate LLM format cache files
    pub generate_llm: bool,
    /// Generate Machine format cache files
    pub generate_machine: bool,
    /// Use atomic writes (temp file + rename)
    pub atomic_writes: bool,
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            cache_root: PathBuf::from(".dx/cache"),
            generate_llm: true,
            generate_machine: true,
            atomic_writes: true,
        }
    }
}

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

    /// Set the cache root directory
    pub fn with_cache_root(mut self, root: impl Into<PathBuf>) -> Self {
        self.cache_root = root.into();
        self
    }

    /// Set whether to generate LLM format
    pub fn with_llm(mut self, generate: bool) -> Self {
        self.generate_llm = generate;
        self
    }

    /// Set whether to generate Machine format
    pub fn with_machine(mut self, generate: bool) -> Self {
        self.generate_machine = generate;
        self
    }

    /// Set whether to use atomic writes
    pub fn with_atomic_writes(mut self, atomic: bool) -> Self {
        self.atomic_writes = atomic;
        self
    }
}

/// Cache generator for DX documents
pub struct CacheGenerator {
    config: CacheConfig,
    parser: HumanParser,
}

impl CacheGenerator {
    /// Create a new cache generator with default config
    pub fn new() -> Self {
        Self {
            config: CacheConfig::default(),
            parser: HumanParser::new(),
        }
    }

    /// Create a cache generator with custom config
    pub fn with_config(config: CacheConfig) -> Self {
        Self {
            config,
            parser: HumanParser::new(),
        }
    }

    /// Map a source path to cache paths
    ///
    /// Preserves the relative path structure in the cache directory.
    /// Example: `src/config/data.dx` -> `.dx/cache/llm/src/config/data.dx.llm`
    pub fn map_path_to_cache(&self, source_path: &Path, base_path: &Path) -> CachePaths {
        // Get relative path from base
        let relative = source_path.strip_prefix(base_path).unwrap_or(source_path);

        // Normalize path separators
        let relative_str = relative.to_string_lossy().replace('\\', "/");

        // Build cache paths
        let llm_path = self
            .config
            .cache_root
            .join("llm")
            .join(&relative_str)
            .with_extension("dx.llm");

        let machine_path = self
            .config
            .cache_root
            .join("machine")
            .join(&relative_str)
            .with_extension("dx.bin");

        CachePaths {
            source: source_path.to_path_buf(),
            llm: llm_path,
            machine: machine_path,
        }
    }

    /// Generate cache files from a Human format source file
    pub fn generate(
        &self,
        source_path: &Path,
        base_path: &Path,
    ) -> Result<CacheResult, CacheError> {
        // Read source file
        let content = fs::read_to_string(source_path)?;

        // Parse to document
        let doc = self
            .parser
            .parse(&content)
            .map_err(|e| CacheError::Parse(e.to_string()))?;

        // Generate cache files
        self.generate_from_document(&doc, source_path, base_path)
    }

    /// Generate cache files from a DxDocument
    pub fn generate_from_document(
        &self,
        doc: &DxDocument,
        source_path: &Path,
        base_path: &Path,
    ) -> Result<CacheResult, CacheError> {
        let paths = self.map_path_to_cache(source_path, base_path);
        let mut result = CacheResult {
            paths: paths.clone(),
            llm_generated: false,
            machine_generated: false,
        };

        // Generate LLM format
        if self.config.generate_llm {
            let llm_content = document_to_llm(doc);
            self.write_cache_file(&paths.llm, llm_content.as_bytes())?;
            result.llm_generated = true;
        }

        // Generate Machine format
        if self.config.generate_machine {
            let machine_content = document_to_machine(doc);
            self.write_cache_file(&paths.machine, &machine_content.data)?;
            result.machine_generated = true;
        }

        Ok(result)
    }

    /// Write cache file with optional atomic write
    fn write_cache_file(&self, path: &Path, content: &[u8]) -> Result<(), CacheError> {
        // Ensure parent directory exists
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).map_err(|e| {
                CacheError::DirectoryCreation(format!("{}: {}", parent.display(), e))
            })?;
        }

        if self.config.atomic_writes {
            // Write to temp file first, then rename
            let temp_path = path.with_extension("tmp");
            fs::write(&temp_path, content)?;
            fs::rename(&temp_path, path)?;
        } else {
            fs::write(path, content)?;
        }

        Ok(())
    }

    /// Get the config
    pub fn config(&self) -> &CacheConfig {
        &self.config
    }
}

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

/// Paths for cache files
#[derive(Debug, Clone)]
pub struct CachePaths {
    /// Original source path
    pub source: PathBuf,
    /// LLM format cache path
    pub llm: PathBuf,
    /// Machine format cache path
    pub machine: PathBuf,
}

/// Result of cache generation
#[derive(Debug)]
pub struct CacheResult {
    /// Cache paths
    pub paths: CachePaths,
    /// Whether LLM format was generated
    pub llm_generated: bool,
    /// Whether Machine format was generated
    pub machine_generated: bool,
}

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

    #[test]
    fn test_cache_config_default() {
        let config = CacheConfig::default();
        assert_eq!(config.cache_root, PathBuf::from(".dx/cache"));
        assert!(config.generate_llm);
        assert!(config.generate_machine);
        assert!(config.atomic_writes);
    }

    #[test]
    fn test_cache_config_builder() {
        let config = CacheConfig::new()
            .with_cache_root("/custom/cache")
            .with_llm(false)
            .with_machine(true)
            .with_atomic_writes(false);

        assert_eq!(config.cache_root, PathBuf::from("/custom/cache"));
        assert!(!config.generate_llm);
        assert!(config.generate_machine);
        assert!(!config.atomic_writes);
    }

    #[test]
    fn test_map_path_to_cache_simple() {
        let generator = CacheGenerator::new();
        let source = Path::new("config.dx");
        let base = Path::new(".");

        let paths = generator.map_path_to_cache(source, base);

        assert_eq!(paths.source, PathBuf::from("config.dx"));
        assert!(paths.llm.to_string_lossy().contains("llm"));
        assert!(paths.llm.to_string_lossy().contains("config.dx.llm"));
        assert!(paths.machine.to_string_lossy().contains("machine"));
        assert!(paths.machine.to_string_lossy().contains("config.dx.bin"));
    }

    #[test]
    fn test_map_path_to_cache_nested() {
        let generator = CacheGenerator::new();
        let source = Path::new("src/config/data.dx");
        let base = Path::new(".");

        let paths = generator.map_path_to_cache(source, base);

        // Should preserve directory structure
        let llm_str = paths.llm.to_string_lossy();
        assert!(llm_str.contains("src") || llm_str.contains("config"));
        assert!(llm_str.contains("data.dx.llm"));
    }

    #[test]
    fn test_map_path_to_cache_with_base() {
        let generator = CacheGenerator::new();
        let source = Path::new("/project/src/config/data.dx");
        let base = Path::new("/project");

        let paths = generator.map_path_to_cache(source, base);

        // Should strip base path
        let llm_str = paths.llm.to_string_lossy();
        assert!(!llm_str.contains("project") || llm_str.contains("src"));
    }

    #[test]
    fn test_cache_generator_from_document() {
        use crate::llm::types::{DxDocument, DxLlmValue};
        use std::env;

        // Use a temp directory in the target folder
        let temp_dir = env::temp_dir().join("dx_cache_test_1");
        let _ = fs::remove_dir_all(&temp_dir); // Clean up any previous run

        let config = CacheConfig::new()
            .with_cache_root(temp_dir.join("cache"))
            .with_atomic_writes(false);

        let generator = CacheGenerator::with_config(config);

        // Create a simple document
        let mut doc = DxDocument::new();
        doc.context
            .insert("nm".to_string(), DxLlmValue::Str("Test".to_string()));

        let source = Path::new("test.dx");
        let base = Path::new(".");

        let result = generator
            .generate_from_document(&doc, source, base)
            .unwrap();

        assert!(result.llm_generated);
        assert!(result.machine_generated);
        assert!(result.paths.llm.exists());
        assert!(result.paths.machine.exists());

        // Verify LLM content
        let llm_content = fs::read_to_string(&result.paths.llm).unwrap();
        assert!(llm_content.contains("nm") || llm_content.contains("Test"));

        // Clean up
        let _ = fs::remove_dir_all(&temp_dir);
    }

    #[test]
    fn test_cache_generator_llm_only() {
        use crate::llm::types::{DxDocument, DxLlmValue};
        use std::env;

        let temp_dir = env::temp_dir().join("dx_cache_test_2");
        let _ = fs::remove_dir_all(&temp_dir);

        let config = CacheConfig::new()
            .with_cache_root(temp_dir.join("cache"))
            .with_llm(true)
            .with_machine(false);

        let generator = CacheGenerator::with_config(config);

        let mut doc = DxDocument::new();
        doc.context
            .insert("nm".to_string(), DxLlmValue::Str("Test".to_string()));

        let source = Path::new("test.dx");
        let base = Path::new(".");

        let result = generator
            .generate_from_document(&doc, source, base)
            .unwrap();

        assert!(result.llm_generated);
        assert!(!result.machine_generated);
        assert!(result.paths.llm.exists());
        assert!(!result.paths.machine.exists());

        // Clean up
        let _ = fs::remove_dir_all(&temp_dir);
    }

    #[test]
    fn test_cache_paths_structure() {
        let paths = CachePaths {
            source: PathBuf::from("src/data.dx"),
            llm: PathBuf::from(".dx/cache/llm/src/data.dx.llm"),
            machine: PathBuf::from(".dx/cache/machine/src/data.dx.bin"),
        };

        assert_eq!(paths.source.file_name().unwrap(), "data.dx");
        assert!(paths.llm.extension().unwrap() == "llm");
        assert!(paths.machine.extension().unwrap() == "bin");
    }
}