settings_loader 1.0.0

Opinionated configuration settings load mechanism for Rust applications
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
#![cfg(feature = "editor")]
//! Core configuration editor that coordinates multi-layer edits.
//!
//! `ConfigEditor` uses provenance metadata (`SourceMap`) to determine which
//! source file provided each configuration value. This allows targeted edits
//! that persist changes back to the original source while preserving comments
//! and formatting (for supported formats like TOML).

use serde::{de::DeserializeOwned, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use super::{Editor, EditorError, LayerEditor, SettingsEditor, SettingsLoaderEditor};
use crate::provenance::{SettingSource, SourceMap};

/// Orchestrates multi-layer configuration editing using provenance data.
#[derive(Debug, Default)]
pub struct ConfigEditor {
    /// Tracking map of key -> source metadata
    source_map: SourceMap,
    /// Cache of open layer editors, keyed by their absolute file path
    editors: HashMap<PathBuf, Editor>,
    /// Default target file for new keys
    default_target: Option<PathBuf>,
}

impl ConfigEditor {
    /// Create a new `ConfigEditor` with the given source map.
    pub fn new(source_map: SourceMap) -> Self {
        Self {
            source_map,
            editors: HashMap::new(),
            default_target: None,
        }
    }

    /// Set the default target file for new keys.
    ///
    /// When setting a key that doesn't exist in any source, the `ConfigEditor`
    /// will use this file as the target.
    pub fn set_default_target(&mut self, path: PathBuf) {
        self.default_target = Some(path);
    }

    /// Get the source map used by this editor.
    pub fn source_map(&self) -> &SourceMap {
        &self.source_map
    }

    /// Get a value for a specific key.
    ///
    /// This retrieves the value from the current state of the editors. If the
    /// editor for the source of this key is not yet open, it will be opened.
    pub fn get<T: DeserializeOwned>(&mut self, key: &str) -> Result<Option<T>, EditorError> {
        let path = {
            let metadata = match self.source_map.source_of(key) {
                Some(meta) => meta,
                None => return Ok(None),
            };

            match &metadata.source {
                SettingSource::File { path, .. } => path.clone(),
                _ => return Ok(None),
            }
        };

        let editor = self.get_or_open_editor(&path)?;
        Ok(editor.get(key))
    }

    /// Set a value for a specific key.
    ///
    /// If the key exists, it will be updated in its original source file.
    /// If the key is new, it will be added to the default target file.
    ///
    /// # Errors
    ///
    /// Returns `EditorError::KeyNotFound` if the key is new and no default target is set.
    pub fn set<T: Serialize>(&mut self, key: &str, value: T) -> Result<(), EditorError> {
        let (path, is_default_target) = if let Some(meta) = self.source_map.source_of(key) {
            match &meta.source {
                SettingSource::File { path, .. } => (path.clone(), false),
                other => {
                    return Err(EditorError::InvalidPath(format!(
                        "Source for key '{}' is not a file: {:?}",
                        key, other
                    )));
                },
            }
        } else if let Some(target) = &self.default_target {
            (target.clone(), true)
        } else {
            return Err(EditorError::KeyNotFound(format!(
                "Key '{}' not found and no default target set",
                key
            )));
        };

        // Use get_or_create for default target (may not exist yet), get_or_open for existing keys
        let editor = if is_default_target {
            self.get_or_create_editor(&path)?
        } else {
            self.get_or_open_editor(&path)?
        };
        editor.set(key, value)?;
        Ok(())
    }

    /// Unset a key.
    ///
    /// Removes the key from its original source file.
    pub fn unset(&mut self, key: &str) -> Result<(), EditorError> {
        let path = {
            let metadata = self
                .source_map
                .source_of(key)
                .ok_or_else(|| EditorError::KeyNotFound(key.to_string()))?;

            match &metadata.source {
                SettingSource::File { path, .. } => path.clone(),
                _ => {
                    return Err(EditorError::InvalidPath(format!(
                        "Source for key '{}' is not a file",
                        key
                    )));
                },
            }
        };

        let editor = self.get_or_open_editor(&path)?;
        editor.unset(key)
    }

    /// Save all changes back to their respective files.
    ///
    /// This performs atomic writes for all "dirty" layer editors.
    pub fn save(&mut self) -> Result<(), EditorError> {
        for editor in self.editors.values_mut() {
            if editor.is_dirty() {
                editor.save()?;
            }
        }
        Ok(())
    }

    /// Check if any of the open editors have unsaved changes.
    pub fn is_dirty(&self) -> bool {
        self.editors.values().any(|e| e.is_dirty())
    }

    /// Get a list of all files that have been modified.
    pub fn dirty_files(&self) -> Vec<PathBuf> {
        self.editors
            .iter()
            .filter(|(_, e)| e.is_dirty())
            .map(|(p, _)| p.clone())
            .collect()
    }

    /// Helper to get an editor from cache or open it.
    fn get_or_open_editor(&mut self, path: &Path) -> Result<&mut Editor, EditorError> {
        if !self.editors.contains_key(path) {
            // Always try to open the file first - don't create non-existent files
            let editor = SettingsLoaderEditor::open(path)?;
            self.editors.insert(path.to_path_buf(), editor);
        }

        Ok(self.editors.get_mut(path).unwrap())
    }

    /// Helper to get or create an editor (used when setting new keys to default target).
    fn get_or_create_editor(&mut self, path: &Path) -> Result<&mut Editor, EditorError> {
        if !self.editors.contains_key(path) {
            let editor = if path.exists() {
                SettingsLoaderEditor::open(path)?
            } else {
                // Determine format from extension
                let format = super::ConfigFormat::from_path(path).ok_or(EditorError::FormatMismatch)?;
                SettingsLoaderEditor::create(path, format)?
            };
            self.editors.insert(path.to_path_buf(), editor);
        }

        Ok(self.editors.get_mut(path).unwrap())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::provenance::{SourceMetadata, SourceScope};
    use serde_json::json;
    use std::fs;
    use tempfile::{tempdir, TempDir};

    struct TestEnv {
        _temp_dir: TempDir,
        file1_path: PathBuf,
        file2_path: PathBuf,
        file3_path: PathBuf, // For new keys
        source_map: SourceMap,
    }

    fn setup_test_environment() -> TestEnv {
        let temp_dir = tempdir().unwrap();
        let file1_path = temp_dir.path().join("config1.toml");
        let file2_path = temp_dir.path().join("config2.json");
        let file3_path = temp_dir.path().join("config3.yaml");

        fs::write(
            &file1_path,
            r#"
                [database]
                host = "localhost"
                port = 5432
                enabled = true
                api_key = "secret_toml"
            "#,
        )
        .unwrap();

        fs::write(
            &file2_path,
            r#"{
                "server": {
                    "port": 8080,
                    "timeout": 300
                },
                "database": {
                    "host": "remotehost"
                },
                "feature_flags": {
                    "new_ui": true
                }
            }"#,
        )
        .unwrap();

        fs::write(&file3_path, "").unwrap(); // Empty file for default target

        let mut source_map = SourceMap::new();
        // Simulate keys coming from config1.toml (layer 0)
        source_map.insert(
            "database.host".to_string(),
            SourceMetadata::file(file1_path.clone(), Some(SourceScope::ProjectLocal), 0),
        );
        source_map.insert(
            "database.port".to_string(),
            SourceMetadata::file(file1_path.clone(), Some(SourceScope::ProjectLocal), 0),
        );
        source_map.insert(
            "database.enabled".to_string(),
            SourceMetadata::file(file1_path.clone(), Some(SourceScope::ProjectLocal), 0),
        );
        source_map.insert(
            "api_key".to_string(),
            SourceMetadata::file(file1_path.clone(), Some(SourceScope::ProjectLocal), 0),
        );

        // Simulate keys coming from config2.json (layer 1, higher precedence)
        source_map.insert(
            "server.port".to_string(),
            SourceMetadata::file(file2_path.clone(), Some(SourceScope::UserGlobal), 1),
        );
        source_map.insert(
            "server.timeout".to_string(),
            SourceMetadata::file(file2_path.clone(), Some(SourceScope::UserGlobal), 1),
        );
        source_map.insert(
            "database.host".to_string(), // Overrides config1.toml
            SourceMetadata::file(file2_path.clone(), Some(SourceScope::UserGlobal), 1),
        );
        source_map.insert(
            "feature_flags.new_ui".to_string(),
            SourceMetadata::file(file2_path.clone(), Some(SourceScope::UserGlobal), 1),
        );

        TestEnv {
            _temp_dir: temp_dir,
            file1_path,
            file2_path,
            file3_path,
            source_map,
        }
    }

    #[test]
    fn test_config_editor_new_and_source_map() {
        let env = setup_test_environment();
        let editor = ConfigEditor::new(env.source_map.clone());
        assert_eq!(editor.source_map().entries().len(), env.source_map.entries().len());
    }

    #[test]
    fn test_config_editor_set_default_target() {
        let env = setup_test_environment();
        let mut editor = ConfigEditor::new(env.source_map.clone());
        editor.set_default_target(env.file3_path.clone());
        assert_eq!(editor.default_target, Some(env.file3_path));
    }

    #[test]
    fn test_config_editor_get_existing_keys() {
        let env = setup_test_environment();
        let mut editor = ConfigEditor::new(env.source_map);

        // From file1.toml
        assert_eq!(editor.get::<u16>("database.port").unwrap(), Some(5432));
        // Overridden by file2.json
        assert_eq!(
            editor.get::<String>("database.host").unwrap(),
            Some("remotehost".to_string())
        );
        // From file2.json
        assert_eq!(editor.get::<u16>("server.port").unwrap(), Some(8080));
    }

    #[test]
    fn test_config_editor_get_non_existent_key() {
        let env = setup_test_environment();
        let mut editor = ConfigEditor::new(env.source_map);
        assert_eq!(editor.get::<String>("non_existent.key").unwrap(), None);
    }

    #[test]
    fn test_config_editor_get_key_from_non_file_source() {
        let mut source_map = SourceMap::new();
        source_map.insert("env_key".to_string(), SourceMetadata::env("ENV_VAR".to_string(), 0));
        let mut editor = ConfigEditor::new(source_map);
        assert_eq!(editor.get::<String>("env_key").unwrap(), None); // Should return None as it's not a file
    }

    #[test]
    fn test_config_editor_set_existing_key_in_original_source() {
        let env = setup_test_environment();
        let mut editor = ConfigEditor::new(env.source_map.clone());

        // database.host is in config2.json (layer 1)
        editor.set("database.host", "new_remotehost.com").unwrap();
        assert!(editor.is_dirty());
        assert_eq!(editor.dirty_files(), vec![env.file2_path.clone()]);

        editor.save().unwrap();
        assert!(!editor.is_dirty());

        // Verify content in file2.json
        let file2_content: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(&env.file2_path).unwrap()).unwrap();
        assert_eq!(file2_content["database"]["host"], json!("new_remotehost.com"));

        // Verify old content in file1.toml is unchanged
        let file1_content = fs::read_to_string(&env.file1_path).unwrap();
        assert!(file1_content.contains("host = \"localhost\"")); // Original value still there
    }

    #[test]
    fn test_config_editor_set_new_key_with_default_target() {
        let env = setup_test_environment();
        let mut editor = ConfigEditor::new(env.source_map.clone());
        editor.set_default_target(env.file3_path.clone());

        editor.set("new.setting", "new_value").unwrap();
        assert!(editor.is_dirty());
        assert_eq!(editor.dirty_files(), vec![env.file3_path.clone()]);

        editor.save().unwrap();
        assert!(!editor.is_dirty());

        let file3_content: serde_yaml::Value =
            serde_yaml::from_str(&fs::read_to_string(&env.file3_path).unwrap()).unwrap();
        assert_eq!(
            file3_content["new"]["setting"],
            serde_yaml::Value::String("new_value".to_string())
        );
    }

    #[test]
    fn test_config_editor_set_new_key_without_default_target_fails() {
        let env = setup_test_environment();
        let mut editor = ConfigEditor::new(env.source_map.clone());

        let res = editor.set("new.setting", "new_value");
        assert!(res.is_err());
        if let Err(EditorError::KeyNotFound(msg)) = res {
            assert!(msg.contains("Key 'new.setting' not found and no default target set"));
        } else {
            panic!("Expected KeyNotFound error, got {:?}", res);
        }
    }

    #[test]
    fn test_config_editor_set_key_from_non_file_source_fails() {
        let mut source_map = SourceMap::new();
        source_map.insert("env_key".to_string(), SourceMetadata::env("ENV_VAR".to_string(), 0));
        let mut editor = ConfigEditor::new(source_map);
        editor.set_default_target(PathBuf::from("dummy.yaml"));

        let res = editor.set("env_key", "value");
        assert!(res.is_err());
        if let Err(EditorError::InvalidPath(msg)) = res {
            assert!(msg.contains("Source for key 'env_key' is not a file"));
        } else {
            panic!("Expected InvalidPath error, got {:?}", res);
        }
    }

    #[test]
    fn test_config_editor_unset_existing_key_multi_layer() {
        let env = setup_test_environment();
        let mut editor = ConfigEditor::new(env.source_map.clone());

        // database.port is in config1.toml
        editor.unset("database.port").unwrap();
        assert!(editor.is_dirty());
        assert_eq!(editor.dirty_files(), vec![env.file1_path.clone()]);

        editor.save().unwrap();
        assert!(!editor.is_dirty());

        // Verify content in file1.toml
        let file1_content = fs::read_to_string(&env.file1_path).unwrap();
        assert!(!file1_content.contains("port = 5432")); // Should be removed
    }

    #[test]
    fn test_config_editor_unset_key_from_non_file_source_fails() {
        let mut source_map = SourceMap::new();
        source_map.insert("env_key".to_string(), SourceMetadata::env("ENV_VAR".to_string(), 0));
        let mut editor = ConfigEditor::new(source_map);

        let res = editor.unset("env_key");
        assert!(res.is_err());
        if let Err(EditorError::InvalidPath(msg)) = res {
            assert!(msg.contains("Source for key 'env_key' is not a file"));
        } else {
            panic!("Expected InvalidPath error, got {:?}", res);
        }
    }

    #[test]
    fn test_config_editor_unset_non_existent_key_fails() {
        let env = setup_test_environment();
        let mut editor = ConfigEditor::new(env.source_map);

        let res = editor.unset("non_existent.key");
        assert!(res.is_err());
        if let Err(EditorError::KeyNotFound(msg)) = res {
            assert_eq!(msg, "non_existent.key");
        } else {
            panic!("Expected KeyNotFound error, got {:?}", res);
        }
    }

    #[test]
    fn test_config_editor_dirty_tracking_and_files_aggregation() {
        let env = setup_test_environment();
        let mut editor = ConfigEditor::new(env.source_map.clone());
        editor.set_default_target(env.file3_path.clone());

        // Set an existing key in file1 (toml)
        editor.set("database.enabled", false).unwrap();
        // Set an existing key in file2 (json)
        editor.set("server.timeout", 600).unwrap();
        // Set a new key in file3 (yaml)
        editor.set("new_key.sub", "value").unwrap();

        assert!(editor.is_dirty());
        let mut dirty_files = editor.dirty_files();
        dirty_files.sort();
        let mut expected_dirty_files = vec![env.file1_path.clone(), env.file2_path.clone(), env.file3_path.clone()];
        expected_dirty_files.sort();
        assert_eq!(dirty_files, expected_dirty_files);

        // Save only one editor (file1_path) manually - this is not how save() works usually,
        // but for testing specific dirty status it's useful to bypass ConfigEditor::save
        // and test parts of it. In production, ConfigEditor::save() saves all.
        // We need to re-open the editor to make it dirty again, as the
        // underlying LayerEditor would reset its dirty flag.
        editor.editors.get_mut(&env.file1_path).unwrap().save().unwrap();
        // The ConfigEditor's view of dirty state relies on its internal LayerEditor's dirty state.
        // We're manually manipulating a LayerEditor, so ConfigEditor's dirty state might not
        // immediately reflect this without a re-scan or explicit reset/reload of LayerEditor,
        // which ConfigEditor doesn't do until re-opening a file.
        // However, is_dirty() and dirty_files() check the internal state directly.
        // So, after saving file1_path, it should no longer be dirty.

        let _editor_after_partial_save = ConfigEditor::new(env.source_map.clone());
        // To properly test aggregation after partial saves without reloading ConfigEditor,
        // we'd need to mock LayerEditor behavior more deeply.
        // For now, let's test the full save scenario.

        // Revert and test full save
        let mut editor = ConfigEditor::new(env.source_map.clone());
        editor.set_default_target(env.file3_path.clone());
        editor.set("database.enabled", false).unwrap();
        editor.set("server.timeout", 600).unwrap();
        editor.set("new_key.sub", "value").unwrap();
        editor.save().unwrap();
        assert!(!editor.is_dirty());
        assert!(editor.dirty_files().is_empty());
    }

    #[test]
    fn test_config_editor_save_no_dirty_editors() {
        let env = setup_test_environment();
        let mut editor = ConfigEditor::new(env.source_map);
        // No changes made, so no editors are dirty
        editor.save().unwrap(); // Should not panic or return error
        assert!(!editor.is_dirty());
        assert!(editor.dirty_files().is_empty());
    }

    #[test]
    fn test_config_editor_get_or_open_editor_new_file() {
        let env = setup_test_environment();
        let mut editor = ConfigEditor::new(env.source_map.clone());
        let new_file_path = env.file3_path.with_file_name("new_test_file.toml");
        // Ensure the file doesn't exist
        if new_file_path.exists() {
            fs::remove_file(&new_file_path).unwrap();
        }

        // get_or_open_editor should fail on non-existent files
        // For creating files, use set() with a default target
        let editor_ref = editor.get_or_open_editor(&new_file_path);
        assert!(
            editor_ref.is_err(),
            "get_or_open_editor should fail on non-existent file"
        );
    }

    #[test]
    fn test_config_editor_get_or_open_editor_existing_file() {
        let env = setup_test_environment();
        let mut editor = ConfigEditor::new(env.source_map.clone());

        // Open an existing file that's part of the source map
        let editor_ref = editor.get_or_open_editor(&env.file1_path).unwrap();
        if let Editor::Toml(toml_editor) = editor_ref {
            assert!(!toml_editor.is_dirty());
            assert_eq!(toml_editor.get::<u16>("database.port"), Some(5432));
        } else {
            panic!("Expected Toml editor");
        }
        assert!(editor.editors.contains_key(&env.file1_path));
    }

    #[test]
    fn test_config_editor_get_or_open_editor_unrecognized_format() {
        let env = setup_test_environment();
        let mut editor = ConfigEditor::new(env.source_map.clone());
        let bad_path = env.file3_path.with_file_name("no_ext"); // No extension

        let res = editor.get_or_open_editor(&bad_path);
        assert!(res.is_err());
        if let Err(EditorError::FormatMismatch) = res {
            // Expected
        } else {
            panic!("Expected FormatMismatch, got {:?}", res);
        }
    }

    #[test]
    fn test_config_editor_get_or_open_editor_io_error_on_create() {
        let env = setup_test_environment();
        let mut editor = ConfigEditor::new(env.source_map.clone());
        // Attempt to create in a non-existent directory
        let bad_path = PathBuf::from("/nonexistent/dir/file.json");

        let res = editor.get_or_open_editor(&bad_path);
        assert!(res.is_err());
        if let Err(EditorError::IoError(e)) = res {
            assert_eq!(e.kind(), std::io::ErrorKind::NotFound);
        } else {
            panic!("Expected IoError::NotFound, got {:?}", res);
        }
    }

    #[test]
    fn test_config_editor_unset_key_from_nonexistent_file() {
        let mut source_map = SourceMap::new();
        let non_existent_file = PathBuf::from("/nonexistent/dir/config.toml");
        source_map.insert(
            "key_in_nonexistent_file".to_string(),
            SourceMetadata::file(non_existent_file.clone(), Some(SourceScope::ProjectLocal), 0),
        );
        let mut editor = ConfigEditor::new(source_map);

        // Attempt to unset a key whose source file does not exist on disk
        let res = editor.unset("key_in_nonexistent_file");
        assert!(res.is_err());
        if let Err(EditorError::IoError(e)) = res {
            assert_eq!(e.kind(), std::io::ErrorKind::NotFound);
        } else {
            panic!("Expected IoError::NotFound, got {:?}", res);
        }
    }

    #[test]
    fn test_config_editor_set_key_from_nonexistent_file() {
        let mut source_map = SourceMap::new();
        let non_existent_file = PathBuf::from("/nonexistent/dir/config.toml");
        source_map.insert(
            "key_in_nonexistent_file".to_string(),
            SourceMetadata::file(non_existent_file.clone(), Some(SourceScope::ProjectLocal), 0),
        );
        let mut editor = ConfigEditor::new(source_map);

        // Attempt to set a key whose source file does not exist on disk
        let res = editor.set("key_in_nonexistent_file", "new_value");
        assert!(res.is_err());
        if let Err(EditorError::IoError(e)) = res {
            assert_eq!(e.kind(), std::io::ErrorKind::NotFound);
        } else {
            panic!("Expected IoError::NotFound, got {:?}", res);
        }
    }
}