clnrm-template 1.3.0

Cleanroom Testing Framework - Template Engine
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
//! Comprehensive TOML file operations for Cleanroom templates
//!
//! Provides TOML-specific functionality for template development:
//! - TOML file loading and parsing
//! - TOML validation and schema checking
//! - TOML file writing and formatting
//! - TOML merging and composition
//! - TOML diff and patch operations
//! - Template file organization and management

use crate::error::{Result, TemplateError};
use serde_json::Value;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};

/// TOML file representation with metadata
#[derive(Debug, Clone)]
pub struct TomlFile {
    /// File path
    pub path: PathBuf,
    /// TOML content as string
    pub content: String,
    /// Parsed TOML as JSON Value for manipulation
    pub parsed: Value,
    /// File metadata
    pub metadata: TomlMetadata,
}

/// TOML file metadata for tracking and validation
#[derive(Debug, Clone)]
pub struct TomlMetadata {
    /// File size in bytes
    pub size: u64,
    /// Last modification time
    pub modified: std::time::SystemTime,
    /// File permissions
    pub permissions: std::fs::Permissions,
    /// Template variables used (for analysis)
    pub variables_used: HashSet<String>,
    /// Template functions used (for analysis)
    pub functions_used: HashSet<String>,
}

/// TOML file loader with comprehensive parsing capabilities
#[derive(Debug, Clone)]
pub struct TomlLoader {
    /// Base directories to search for TOML files
    search_paths: Vec<PathBuf>,
    /// File extensions to consider (default: toml, clnrm.toml)
    extensions: Vec<String>,
    /// Enable recursive directory scanning
    recursive: bool,
    /// Validation rules to apply during loading
    validation_rules: Vec<crate::validation::ValidationRule>,
}

impl Default for TomlLoader {
    fn default() -> Self {
        Self {
            search_paths: Vec::new(),
            extensions: vec!["toml".to_string(), "clnrm.toml".to_string()],
            recursive: true,
            validation_rules: Vec::new(),
        }
    }
}

impl TomlLoader {
    /// Create new TOML loader
    pub fn new() -> Self {
        Self::default()
    }

    /// Add search path for TOML files
    pub fn with_search_path<P: AsRef<Path>>(mut self, path: P) -> Self {
        self.search_paths.push(path.as_ref().to_path_buf());
        self
    }

    /// Add multiple search paths
    pub fn with_search_paths<I, P>(mut self, paths: I) -> Self
    where
        I: IntoIterator<Item = P>,
        P: AsRef<Path>,
    {
        for path in paths {
            self.search_paths.push(path.as_ref().to_path_buf());
        }
        self
    }

    /// Set file extensions to include
    pub fn with_extensions<I, S>(mut self, extensions: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.extensions = extensions.into_iter().map(|s| s.into()).collect();
        self
    }

    /// Enable/disable recursive scanning
    pub fn recursive(mut self, recursive: bool) -> Self {
        self.recursive = recursive;
        self
    }

    /// Add validation rule for loaded TOML files
    pub fn with_validation_rule(mut self, rule: crate::validation::ValidationRule) -> Self {
        self.validation_rules.push(rule);
        self
    }

    /// Load single TOML file
    ///
    /// # Arguments
    /// * `path` - Path to TOML file
    pub fn load_file<P: AsRef<Path>>(&self, path: P) -> Result<TomlFile> {
        let path = path.as_ref();

        if !path.exists() {
            return Err(TemplateError::IoError(format!(
                "TOML file not found: {}",
                path.display()
            )));
        }

        if !path.is_file() {
            return Err(TemplateError::IoError(format!(
                "Path is not a file: {}",
                path.display()
            )));
        }

        // Check file extension
        if let Some(ext) = path.extension().and_then(|s| s.to_str()) {
            if !self.extensions.contains(&ext.to_string()) {
                return Err(TemplateError::ValidationError(format!(
                    "File extension '{}' not supported. Expected: {:?}",
                    ext, self.extensions
                )));
            }
        }

        let content = fs::read_to_string(path)
            .map_err(|e| TemplateError::IoError(format!("Failed to read TOML file: {}", e)))?;

        let parsed = toml::from_str::<Value>(&content)
            .map_err(|e| TemplateError::ValidationError(format!("Invalid TOML format: {}", e)))?;

        let metadata = path
            .metadata()
            .map_err(|e| TemplateError::IoError(format!("Failed to read file metadata: {}", e)))?;

        let file = TomlFile {
            path: path.to_path_buf(),
            content,
            parsed,
            metadata: TomlMetadata {
                size: metadata.len(),
                modified: metadata.modified().map_err(|e| {
                    TemplateError::IoError(format!("Failed to get modification time: {}", e))
                })?,
                permissions: metadata.permissions(),
                variables_used: HashSet::new(),
                functions_used: HashSet::new(),
            },
        };

        // Apply validation rules
        for rule in &self.validation_rules {
            rule.validate(&file.parsed, &file.path.to_string_lossy())?;
        }

        Ok(file)
    }

    /// Load all TOML files from search paths
    ///
    /// Returns map of file paths to TomlFile objects
    pub fn load_all(&self) -> Result<HashMap<PathBuf, TomlFile>> {
        let mut files = HashMap::new();

        for search_path in &self.search_paths {
            self.scan_directory(search_path, &mut files)?;
        }

        Ok(files)
    }

    /// Scan directory for TOML files
    fn scan_directory(&self, dir: &Path, files: &mut HashMap<PathBuf, TomlFile>) -> Result<()> {
        use walkdir::WalkDir;

        let walker = if self.recursive {
            WalkDir::new(dir)
        } else {
            WalkDir::new(dir).max_depth(1)
        };

        for entry in walker {
            let entry = entry.map_err(|e| {
                TemplateError::IoError(format!("Failed to read directory entry: {}", e))
            })?;

            if entry.file_type().is_file() {
                let path = entry.path();

                // Check if file has supported extension
                if let Some(ext) = path.extension().and_then(|s| s.to_str()) {
                    if self.extensions.contains(&ext.to_string()) {
                        match self.load_file(path) {
                            Ok(file) => {
                                files.insert(path.to_path_buf(), file);
                            }
                            Err(e) => {
                                eprintln!("Warning: Failed to load TOML file {:?}: {}", path, e);
                            }
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Load TOML files matching glob pattern
    pub fn load_glob(&self, pattern: &str) -> Result<HashMap<PathBuf, TomlFile>> {
        use globset::{Glob, GlobSetBuilder};

        let glob = Glob::new(pattern).map_err(|e| {
            TemplateError::ConfigError(format!("Invalid glob pattern '{}': {}", pattern, e))
        })?;

        let glob_set = GlobSetBuilder::new()
            .add(glob)
            .build()
            .map_err(|e| TemplateError::ConfigError(format!("Failed to build glob set: {}", e)))?;

        let mut files = HashMap::new();

        for search_path in &self.search_paths {
            self.scan_glob_pattern(search_path, &glob_set, &mut files)?;
        }

        Ok(files)
    }

    /// Scan directory with glob pattern
    fn scan_glob_pattern(
        &self,
        dir: &Path,
        glob_set: &globset::GlobSet,
        files: &mut HashMap<PathBuf, TomlFile>,
    ) -> Result<()> {
        use walkdir::WalkDir;

        let walker = if self.recursive {
            WalkDir::new(dir)
        } else {
            WalkDir::new(dir).max_depth(1)
        };

        for entry in walker {
            let entry = entry.map_err(|e| {
                TemplateError::IoError(format!("Failed to read directory entry: {}", e))
            })?;

            if entry.file_type().is_file() {
                let path_str = entry.path().to_string_lossy();
                if glob_set.is_match(&*path_str) {
                    match self.load_file(entry.path()) {
                        Ok(file) => {
                            files.insert(entry.path().to_path_buf(), file);
                        }
                        Err(e) => {
                            eprintln!(
                                "Warning: Failed to load TOML file {:?}: {}",
                                entry.path(),
                                e
                            );
                        }
                    }
                }
            }
        }

        Ok(())
    }
}

/// TOML file writer with formatting and validation
#[derive(Debug, Clone)]
pub struct TomlWriter {
    /// Enable pretty formatting
    pretty: bool,
    /// Backup files before writing
    backup: bool,
    /// Validate before writing
    validate: bool,
    /// Custom header comment for generated files
    header: Option<String>,
}

impl Default for TomlWriter {
    fn default() -> Self {
        Self {
            pretty: true,
            backup: true,
            validate: true,
            header: Some("# Generated by clnrm-template".to_string()),
        }
    }
}

impl TomlWriter {
    /// Create new TOML writer
    pub fn new() -> Self {
        Self::default()
    }

    /// Enable/disable pretty formatting
    pub fn pretty(mut self, pretty: bool) -> Self {
        self.pretty = pretty;
        self
    }

    /// Enable/disable backup creation
    pub fn backup(mut self, backup: bool) -> Self {
        self.backup = backup;
        self
    }

    /// Enable/disable validation before writing
    pub fn validate(mut self, validate: bool) -> Self {
        self.validate = validate;
        self
    }

    /// Set custom header comment
    pub fn with_header<S: Into<String>>(mut self, header: S) -> Self {
        self.header = Some(header.into());
        self
    }

    /// Write TOML content to file
    ///
    /// # Arguments
    /// * `path` - Target file path
    /// * `content` - TOML content to write
    /// * `validator` - Optional validator to run before writing
    pub fn write_file<P: AsRef<Path>>(
        &self,
        path: P,
        content: &str,
        validator: Option<&crate::validation::TemplateValidator>,
    ) -> Result<()> {
        let path = path.as_ref();

        // Validate before writing if enabled
        if self.validate {
            if let Some(validator) = validator {
                validator.validate(content, &path.to_string_lossy())?;
            }
        }

        // Create backup if enabled and file exists
        if self.backup && path.exists() {
            self.create_backup(path)?;
        }

        // Prepare content with header
        let final_content = if let Some(ref header) = self.header {
            format!("{}\n{}\n", header, content)
        } else {
            content.to_string()
        };

        // Write file
        let mut file = fs::File::create(path)
            .map_err(|e| TemplateError::IoError(format!("Failed to create file: {}", e)))?;

        file.write_all(final_content.as_bytes())
            .map_err(|e| TemplateError::IoError(format!("Failed to write file: {}", e)))?;

        file.sync_all()
            .map_err(|e| TemplateError::IoError(format!("Failed to sync file: {}", e)))?;

        Ok(())
    }

    /// Create backup of existing file
    fn create_backup(&self, path: &Path) -> Result<()> {
        let _backup_path = self.backup_path(path);

        fs::copy(path, &_backup_path)
            .map_err(|e| TemplateError::IoError(format!("Failed to create backup: {}", e)))?;

        Ok(())
    }

    /// Generate backup file path
    fn backup_path(&self, path: &Path) -> PathBuf {
        let timestamp = chrono::Utc::now().format("%Y%m%d_%H%M%S");
        let stem = path.file_stem().unwrap_or_default().to_string_lossy();
        let _ext = path.extension().unwrap_or_default().to_string_lossy();

        path.with_file_name(format!("{}.{}.bak", stem, timestamp))
    }
}

/// TOML merger for combining multiple TOML sources
pub struct TomlMerger {
    /// Merge strategy for conflicting keys
    strategy: MergeStrategy,
    /// Preserve comments and formatting
    preserve_formatting: bool,
    /// Deep merge nested structures
    deep_merge: bool,
}

pub enum MergeStrategy {
    /// Overwrite existing values (default)
    Overwrite,
    /// Merge arrays by concatenation
    MergeArrays,
    /// Preserve existing values
    Preserve,
    /// Custom merge function
    Custom,
}

impl std::fmt::Debug for MergeStrategy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MergeStrategy::Overwrite => write!(f, "Overwrite"),
            MergeStrategy::MergeArrays => write!(f, "MergeArrays"),
            MergeStrategy::Preserve => write!(f, "Preserve"),
            MergeStrategy::Custom => write!(f, "Custom"),
        }
    }
}

impl Clone for MergeStrategy {
    fn clone(&self) -> Self {
        match self {
            MergeStrategy::Overwrite => MergeStrategy::Overwrite,
            MergeStrategy::MergeArrays => MergeStrategy::MergeArrays,
            MergeStrategy::Preserve => MergeStrategy::Preserve,
            MergeStrategy::Custom => MergeStrategy::Custom,
        }
    }
}

impl Default for TomlMerger {
    fn default() -> Self {
        Self {
            strategy: MergeStrategy::Overwrite,
            preserve_formatting: false,
            deep_merge: true,
        }
    }
}

impl TomlMerger {
    /// Create new TOML merger
    pub fn new() -> Self {
        Self::default()
    }

    /// Set merge strategy
    pub fn with_strategy(mut self, strategy: MergeStrategy) -> Self {
        self.strategy = strategy;
        self
    }

    /// Enable/disable formatting preservation
    pub fn preserve_formatting(mut self, preserve: bool) -> Self {
        self.preserve_formatting = preserve;
        self
    }

    /// Enable/disable deep merging
    pub fn deep_merge(mut self, deep: bool) -> Self {
        self.deep_merge = deep;
        self
    }

    /// Merge two TOML values
    ///
    /// # Arguments
    /// * `base` - Base TOML value
    /// * `overlay` - TOML value to merge on top
    pub fn merge(&self, base: &Value, overlay: &Value) -> Result<Value> {
        match (&base, &overlay) {
            (Value::Object(base_obj), Value::Object(overlay_obj)) => {
                let mut result = base_obj.clone();

                for (key, overlay_value) in overlay_obj {
                    if let Some(base_value) = base_obj.get(key) {
                        let merged = self.merge_values(base_value, overlay_value)?;
                        result.insert(key.clone(), merged);
                    } else {
                        result.insert(key.clone(), overlay_value.clone());
                    }
                }

                Ok(Value::Object(result))
            }
            _ => {
                // For non-objects, use overlay strategy
                Ok(overlay.clone())
            }
        }
    }

    /// Merge individual values based on strategy
    fn merge_values(&self, base: &Value, overlay: &Value) -> Result<Value> {
        match &self.strategy {
            MergeStrategy::Overwrite => Ok(overlay.clone()),
            MergeStrategy::Preserve => Ok(base.clone()),
            MergeStrategy::MergeArrays => {
                if let (Value::Array(base_arr), Value::Array(overlay_arr)) = (base, overlay) {
                    let mut merged = base_arr.clone();
                    merged.extend(overlay_arr.iter().cloned());
                    Ok(Value::Array(merged))
                } else {
                    Ok(overlay.clone())
                }
            }
            MergeStrategy::Custom => Ok(overlay.clone()), // Simplified for now
        }
    }

    /// Merge multiple TOML files
    ///
    /// # Arguments
    /// * `files` - Vector of TomlFile objects to merge
    pub fn merge_files(&self, files: &[&TomlFile]) -> Result<TomlFile> {
        if files.is_empty() {
            return Err(TemplateError::ValidationError(
                "No files to merge".to_string(),
            ));
        }

        let mut merged_value = files[0].parsed.clone();

        for file in &files[1..] {
            merged_value = self.merge(&merged_value, &file.parsed)?;
        }

        // Create new TomlFile with merged content
        let merged_content = if self.preserve_formatting {
            // Try to preserve formatting (simplified)
            toml::to_string_pretty(&merged_value)
                .unwrap_or_else(|_| toml::to_string(&merged_value).unwrap_or_default())
        } else {
            toml::to_string(&merged_value).map_err(|e| {
                TemplateError::ValidationError(format!("Failed to serialize merged TOML: {}", e))
            })?
        };

        Ok(TomlFile {
            path: files[0].path.with_extension("merged.toml"),
            content: merged_content,
            parsed: merged_value,
            metadata: files[0].metadata.clone(), // Use first file's metadata
        })
    }
}

/// TOML file utilities for common operations
pub struct TomlUtils;

impl TomlUtils {
    /// Extract variables from TOML content for template analysis
    ///
    /// # Arguments
    /// * `content` - TOML content as string
    pub fn extract_variables(content: &str) -> Result<HashSet<String>> {
        let parsed = toml::from_str::<Value>(content).map_err(|e| {
            TemplateError::ValidationError(format!("Invalid TOML for variable extraction: {}", e))
        })?;

        let mut variables = HashSet::new();
        Self::extract_variables_recursive(&parsed, &mut variables, "");
        Ok(variables)
    }

    /// Recursively extract variable references from TOML
    fn extract_variables_recursive(value: &Value, variables: &mut HashSet<String>, prefix: &str) {
        match value {
            Value::String(s) => {
                // Look for template variable patterns {{ variable }}
                if s.contains("{{") && s.contains("}}") {
                    // Simple extraction - in real implementation would use regex
                    if let Some(start) = s.find("{{") {
                        if let Some(end) = s.find("}}") {
                            let var_part = &s[start + 2..end];
                            if !var_part.trim().is_empty() {
                                let var_name = if prefix.is_empty() {
                                    var_part.trim().to_string()
                                } else {
                                    format!("{}.{}", prefix, var_part.trim())
                                };
                                variables.insert(var_name);
                            }
                        }
                    }
                }
            }
            Value::Object(obj) => {
                for (key, value) in obj {
                    let new_prefix = if prefix.is_empty() {
                        key.clone()
                    } else {
                        format!("{}.{}", prefix, key)
                    };
                    Self::extract_variables_recursive(value, variables, &new_prefix);
                }
            }
            Value::Array(arr) => {
                for (i, value) in arr.iter().enumerate() {
                    let new_prefix = if prefix.is_empty() {
                        format!("{}", i)
                    } else {
                        format!("{}.{}", prefix, i)
                    };
                    Self::extract_variables_recursive(value, variables, &new_prefix);
                }
            }
            _ => {} // Other types don't contain variables
        }
    }

    /// Validate TOML file structure
    ///
    /// # Arguments
    /// * `file` - TomlFile to validate
    /// * `required_sections` - Required top-level sections
    pub fn validate_structure(file: &TomlFile, required_sections: &[&str]) -> Result<()> {
        let obj = file
            .parsed
            .as_object()
            .ok_or_else(|| TemplateError::ValidationError("TOML must be an object".to_string()))?;

        for section in required_sections {
            if !obj.contains_key(*section) {
                return Err(TemplateError::ValidationError(format!(
                    "Required section '{}' missing in TOML file: {}",
                    section,
                    file.path.display()
                )));
            }
        }

        Ok(())
    }

    /// Compare two TOML files for differences
    ///
    /// # Arguments
    /// * `file1` - First TOML file
    /// * `file2` - Second TOML file
    pub fn diff(file1: &TomlFile, file2: &TomlFile) -> TomlDiff {
        let mut added = Vec::new();
        let mut removed = Vec::new();
        let mut changed = Vec::new();

        // Compare top-level keys
        if let (Some(obj1), Some(obj2)) = (file1.parsed.as_object(), file2.parsed.as_object()) {
            for (key, value2) in obj2 {
                if let Some(value1) = obj1.get(key) {
                    if value1 != value2 {
                        changed.push((key.clone(), value1.clone(), value2.clone()));
                    }
                } else {
                    added.push((key.clone(), value2.clone()));
                }
            }

            for (key, _) in obj1 {
                if !obj2.contains_key(key) {
                    removed.push(key.clone());
                }
            }
        }

        TomlDiff {
            added,
            removed,
            changed,
        }
    }

    /// Pretty format TOML content
    ///
    /// # Arguments
    /// * `content` - TOML content to format
    pub fn format_toml(content: &str) -> Result<String> {
        let parsed = toml::from_str::<Value>(content).map_err(|e| {
            TemplateError::ValidationError(format!("Invalid TOML for formatting: {}", e))
        })?;

        toml::to_string_pretty(&parsed)
            .map_err(|e| TemplateError::ValidationError(format!("Failed to format TOML: {}", e)))
    }

    /// Validate TOML syntax and structure
    ///
    /// # Arguments
    /// * `content` - TOML content to validate
    pub fn validate_toml_syntax(content: &str) -> Result<()> {
        // Parse to check syntax
        toml::from_str::<Value>(content)
            .map_err(|e| TemplateError::ValidationError(format!("Invalid TOML syntax: {}", e)))?;

        Ok(())
    }

    /// Extract all keys from TOML content
    ///
    /// # Arguments
    /// * `content` - TOML content
    pub fn extract_keys(content: &str) -> Result<HashSet<String>> {
        let parsed = toml::from_str::<Value>(content).map_err(|e| {
            TemplateError::ValidationError(format!("Invalid TOML for key extraction: {}", e))
        })?;

        let mut keys = HashSet::new();
        Self::extract_keys_recursive(&parsed, &mut keys, "");
        Ok(keys)
    }

    /// Recursively extract all keys from TOML
    fn extract_keys_recursive(value: &Value, keys: &mut HashSet<String>, prefix: &str) {
        match value {
            Value::Object(obj) => {
                for (key, value) in obj {
                    let full_key = if prefix.is_empty() {
                        key.clone()
                    } else {
                        format!("{}.{}", prefix, key)
                    };
                    keys.insert(full_key.clone());
                    Self::extract_keys_recursive(value, keys, &full_key);
                }
            }
            Value::Array(arr) => {
                for (i, value) in arr.iter().enumerate() {
                    let index_key = if prefix.is_empty() {
                        format!("{}", i)
                    } else {
                        format!("{}.{}", prefix, i)
                    };
                    Self::extract_keys_recursive(value, keys, &index_key);
                }
            }
            _ => {} // Leaf values don't have keys
        }
    }

    /// Check if TOML content contains template variables
    ///
    /// # Arguments
    /// * `content` - TOML content
    pub fn contains_templates(content: &str) -> bool {
        content.contains("{{") || content.contains("{%") || content.contains("{#")
    }

    /// Count template variables in TOML content
    ///
    /// # Arguments
    /// * `content` - TOML content
    pub fn count_variables(content: &str) -> usize {
        let mut count = 0;
        let mut in_braces = false;

        for ch in content.chars() {
            match ch {
                '{' => {
                    if let Some(next) = content.chars().nth(count + 1) {
                        if next == '{' {
                            in_braces = true;
                        }
                    }
                }
                '}' => {
                    if let Some(prev) = content.chars().nth(count - 1) {
                        if prev == '}' && in_braces {
                            in_braces = false;
                        }
                    }
                }
                _ => {
                    if in_braces {
                        // Count variables (simplified - would need proper parsing)
                        count += 1;
                    }
                }
            }
        }

        count
    }
}

/// TOML file differences for comparison
#[derive(Debug, Clone)]
pub struct TomlDiff {
    /// Keys added in second file
    pub added: Vec<(String, Value)>,
    /// Keys removed from first file
    pub removed: Vec<String>,
    /// Keys with different values
    pub changed: Vec<(String, Value, Value)>,
}

/// Fluent API for TOML file operations
pub struct TomlFileBuilder {
    loader: TomlLoader,
    writer: TomlWriter,
    merger: TomlMerger,
}

impl TomlFileBuilder {
    /// Start building TOML file operations
    pub fn new() -> Self {
        Self {
            loader: TomlLoader::new(),
            writer: TomlWriter::new(),
            merger: TomlMerger::new(),
        }
    }

    /// Configure loader
    pub fn loader<F>(mut self, f: F) -> Self
    where
        F: FnOnce(TomlLoader) -> TomlLoader,
    {
        self.loader = f(self.loader);
        self
    }

    /// Configure writer
    pub fn writer<F>(mut self, f: F) -> Self
    where
        F: FnOnce(TomlWriter) -> TomlWriter,
    {
        self.writer = f(self.writer);
        self
    }

    /// Configure merger
    pub fn merger<F>(mut self, f: F) -> Self
    where
        F: FnOnce(TomlMerger) -> TomlMerger,
    {
        self.merger = f(self.merger);
        self
    }

    /// Load TOML file
    pub fn load<P: AsRef<Path>>(self, path: P) -> Result<TomlFile> {
        self.loader.load_file(path)
    }

    /// Write TOML file
    pub fn write<P: AsRef<Path>>(
        self,
        path: P,
        content: &str,
        validator: Option<&crate::validation::TemplateValidator>,
    ) -> Result<()> {
        self.writer.write_file(path, content, validator)
    }

    /// Merge TOML files
    pub fn merge(self, files: &[&TomlFile]) -> Result<TomlFile> {
        self.merger.merge_files(files)
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    #[test]
    fn test_toml_file_loading() {
        let temp_dir = tempdir().unwrap();
        let toml_file = temp_dir.path().join("test.toml");

        let content = r#"
[service]
name = "test-service"

[meta]
version = "1.0.0"
        "#;

        fs::write(&toml_file, content).unwrap();

        let loader = TomlLoader::new()
            .with_search_path(&temp_dir)
            .with_extensions(vec!["toml"]);

        let file = loader.load_file(&toml_file).unwrap();

        assert_eq!(file.path, toml_file);
        assert_eq!(file.content, content);
        assert!(file.parsed.get("service").is_some());
        assert!(file.parsed.get("meta").is_some());
    }

    #[test]
    fn test_toml_merging() {
        let base_content = r#"
[service]
name = "base-service"

[meta]
version = "1.0.0"
        "#;

        let overlay_content = r#"
[service]
description = "overlay description"

[config]
debug = true
        "#;

        let base_parsed = toml::from_str::<Value>(base_content).unwrap();
        let overlay_parsed = toml::from_str::<Value>(overlay_content).unwrap();

        let merger = TomlMerger::new();
        let merged = merger.merge(&base_parsed, &overlay_parsed).unwrap();

        // Should have both service.name and service.description
        assert!(merged.get("service").unwrap().get("name").is_some());
        assert!(merged.get("service").unwrap().get("description").is_some());
        assert!(merged.get("meta").is_some());
        assert!(merged.get("config").is_some());
    }

    #[test]
    fn test_variable_extraction() {
        let content = r#"
service = "{{ service_name }}"
config = "{{ config.env }}"
        "#;

        let variables = TomlUtils::extract_variables(content).unwrap();
        assert!(variables.contains("service_name"));
        assert!(variables.contains("config.env"));
    }

    #[test]
    fn test_toml_validation() {
        let temp_dir = tempdir().unwrap();
        let toml_file = temp_dir.path().join("config.toml");

        let content = r#"
[service]
name = "test-service"

[meta]
version = "1.0.0"
        "#;

        fs::write(&toml_file, content).unwrap();

        let file = TomlLoader::new().load_file(&toml_file).unwrap();
        TomlUtils::validate_structure(&file, &["service", "meta"]).unwrap();
    }

    #[test]
    fn test_toml_formatting() {
        let content = r#"[service]name="test"[meta]version="1.0.0""#;
        let formatted = TomlUtils::format_toml(content).unwrap();

        assert!(formatted.contains("[service]"));
        assert!(formatted.contains("[meta]"));
        assert!(formatted.contains("name = \"test\""));
        assert!(formatted.contains("version = \"1.0.0\""));
    }

    #[test]
    fn test_toml_key_extraction() {
        let content = r#"
[service]
name = "test"

[config]
debug = true

[database]
host = "localhost"
        "#;

        let keys = TomlUtils::extract_keys(content).unwrap();
        assert!(keys.contains("service"));
        assert!(keys.contains("service.name"));
        assert!(keys.contains("config"));
        assert!(keys.contains("config.debug"));
        assert!(keys.contains("database"));
        assert!(keys.contains("database.host"));
    }
}