rcman 0.2.2

Framework-agnostic settings management with schema, backup/restore, secrets and derive macro support
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
//! YAML Storage Integration Tests
//!
//! Tests for YAML storage backend with various rcman features:
//! - Basic settings management with YAML
//! - Sub-settings (multi-file and single-file modes)
//! - Profiles with YAML storage
//! - Edge cases (null handling, nested structures)

#![cfg(feature = "yaml")]

mod common;

use common::TestSettings;
use rcman::{SettingsConfig, SettingsManager, SubSettingsConfig, YamlStorage};
use serde::{Deserialize, Serialize};
use serde_json::json;
use tempfile::TempDir;

// Conditionally used by profile tests
#[cfg(feature = "profiles")]
use rcman::SettingsSchema;
#[cfg(feature = "profiles")]
use std::collections::HashMap;

// =============================================================================
// Basic YAML Settings Management
// =============================================================================

#[test]
fn test_yaml_basic_save_and_load() {
    let temp_dir = TempDir::new().unwrap();

    let config = SettingsConfig::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_storage::<YamlStorage>()
        .with_schema::<TestSettings>()
        .build();

    let manager = SettingsManager::new(config).unwrap();

    // Save a setting
    manager
        .save_setting("ui", "theme", &json!("light"))
        .unwrap();

    // Verify file is YAML
    let settings_file = temp_dir.path().join("settings.yaml");
    assert!(settings_file.exists(), "Settings file should be .yaml");

    let content = std::fs::read_to_string(&settings_file).unwrap();
    assert!(content.contains("ui:"), "YAML should have ui mapping");
    assert!(
        content.contains("theme: light"),
        "YAML should contain theme: light"
    );
}

#[test]
fn test_yaml_load_settings_struct() {
    let temp_dir = TempDir::new().unwrap();

    let config = SettingsConfig::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_storage::<YamlStorage>()
        .with_schema::<TestSettings>()
        .build();

    let manager = SettingsManager::new(config).unwrap();

    // Save some values
    manager
        .save_setting("ui", "theme", &json!("light"))
        .unwrap();
    manager
        .save_setting("ui", "font_size", &json!(16.0))
        .unwrap();

    // Load as struct
    let settings: TestSettings = manager.get_all().unwrap();
    assert_eq!(settings.ui.theme, "light");
    assert_eq!(settings.ui.font_size, 16.0);
}

#[test]
fn test_yaml_reset_setting() {
    let temp_dir = TempDir::new().unwrap();

    let config = SettingsConfig::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_storage::<YamlStorage>()
        .with_schema::<TestSettings>()
        .build();

    let manager = SettingsManager::new(config).unwrap();

    // Save non-default
    manager
        .save_setting("ui", "theme", &json!("light"))
        .unwrap();

    // Reset
    let default_value = manager.reset_setting("ui", "theme").unwrap();
    assert_eq!(default_value, json!("dark"));

    // Verify it's back to default
    let settings: TestSettings = manager.get_all().unwrap();
    assert_eq!(settings.ui.theme, "dark");
}

// =============================================================================
// YAML Sub-Settings (Multi-File Mode)
// =============================================================================

#[test]
fn test_yaml_sub_settings_multi_file() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_storage::<YamlStorage>()
        .with_sub_settings(SubSettingsConfig::new("remotes"))
        .build()
        .unwrap();

    let remotes = manager.sub_settings("remotes").unwrap();

    // Create entries
    remotes
        .set("gdrive", &json!({"type": "drive", "client_id": "abc123"}))
        .unwrap();
    remotes
        .set("s3", &json!({"type": "s3", "bucket": "my-bucket"}))
        .unwrap();

    // Verify files are YAML
    let remotes_dir = temp_dir.path().join("remotes");
    assert!(remotes_dir.join("gdrive.yaml").exists());
    assert!(remotes_dir.join("s3.yaml").exists());

    // Verify content is valid YAML
    let gdrive_content = std::fs::read_to_string(remotes_dir.join("gdrive.yaml")).unwrap();
    assert!(gdrive_content.contains("type: drive"));
    assert!(gdrive_content.contains("client_id: abc123"));

    // Read back
    let gdrive = remotes.get_value("gdrive").unwrap();
    assert_eq!(gdrive["type"], "drive");
}

#[test]
fn test_yaml_sub_settings_list() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_storage::<YamlStorage>()
        .with_sub_settings(SubSettingsConfig::new("configs"))
        .build()
        .unwrap();

    let configs = manager.sub_settings("configs").unwrap();

    configs.set("alpha", &json!({"name": "Alpha"})).unwrap();
    configs.set("beta", &json!({"name": "Beta"})).unwrap();
    configs.set("gamma", &json!({"name": "Gamma"})).unwrap();

    let mut list = configs.list().unwrap();
    list.sort();

    assert_eq!(list, vec!["alpha", "beta", "gamma"]);
}

#[test]
fn test_yaml_sub_settings_delete() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_storage::<YamlStorage>()
        .with_sub_settings(SubSettingsConfig::new("remotes"))
        .build()
        .unwrap();

    let remotes = manager.sub_settings("remotes").unwrap();

    remotes.set("temp", &json!({"type": "temp"})).unwrap();
    let file_path = temp_dir.path().join("remotes").join("temp.yaml");
    assert!(file_path.exists());

    remotes.delete("temp").unwrap();
    assert!(!file_path.exists());
    assert!(!remotes.exists("temp").unwrap());
}

// =============================================================================
// YAML Sub-Settings (Single-File Mode)
// =============================================================================

#[test]
fn test_yaml_sub_settings_single_file() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_storage::<YamlStorage>()
        .with_sub_settings(SubSettingsConfig::singlefile("backends"))
        .build()
        .unwrap();

    let backends = manager.sub_settings("backends").unwrap();

    backends
        .set("local", &json!({"host": "localhost", "port": 5572}))
        .unwrap();
    backends
        .set("remote", &json!({"host": "192.168.1.1", "port": 5573}))
        .unwrap();

    // Should be a single file
    let backends_file = temp_dir.path().join("backends.yaml");
    assert!(backends_file.exists());
    assert!(backends_file.is_file());

    // Verify content structure
    let content = std::fs::read_to_string(&backends_file).unwrap();
    assert!(content.contains("local:") || content.contains("host: localhost"));
    assert!(content.contains("remote:") || content.contains("host: 192.168.1.1"));

    // Read back both entries
    let local = backends.get_value("local").unwrap();
    assert_eq!(local["host"], "localhost");

    let remote = backends.get_value("remote").unwrap();
    assert_eq!(remote["host"], "192.168.1.1");
}

// =============================================================================
// YAML with Profiles
// =============================================================================

#[cfg(feature = "profiles")]
#[test]
fn test_yaml_profiles_basic() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_storage::<YamlStorage>()
        .with_sub_settings(SubSettingsConfig::new("remotes").with_profiles())
        .build()
        .unwrap();

    let remotes = manager.sub_settings("remotes").unwrap();
    let profiles = remotes.profiles().unwrap();

    // Add data to default profile
    remotes
        .set("personal-drive", &json!({"type": "drive"}))
        .unwrap();

    // Create work profile
    profiles.create("work").unwrap();
    remotes.switch_profile("work").unwrap();

    // Add work-specific data
    remotes
        .set("company-drive", &json!({"type": "sharepoint"}))
        .unwrap();

    // Verify directory structure uses .yaml files
    let remotes_dir = temp_dir.path().join("remotes");
    assert!(
        remotes_dir.join(".profiles.yaml").exists(),
        "Manifest should be .yaml"
    );

    let default_dir = remotes_dir.join("profiles").join("default");
    assert!(default_dir.join("personal-drive.yaml").exists());

    let work_dir = remotes_dir.join("profiles").join("work");
    assert!(work_dir.join("company-drive.yaml").exists());

    // Switch back and verify isolation
    remotes.switch_profile("default").unwrap();
    assert!(remotes.exists("personal-drive").unwrap());
    assert!(!remotes.exists("company-drive").unwrap());
}

#[cfg(feature = "profiles")]
#[test]
fn test_yaml_main_settings_profiles() {
    use rcman::SettingMetadata;

    #[derive(Serialize, Deserialize, Default)]
    struct SimpleSettings {
        #[serde(default)]
        app: AppSection,
    }

    #[derive(Serialize, Deserialize)]
    struct AppSection {
        #[serde(default = "default_mode")]
        mode: String,
    }

    fn default_mode() -> String {
        "normal".to_string()
    }

    impl Default for AppSection {
        fn default() -> Self {
            Self {
                mode: default_mode(),
            }
        }
    }

    impl SettingsSchema for SimpleSettings {
        fn get_metadata() -> HashMap<String, SettingMetadata> {
            let mut map = HashMap::new();
            map.insert(
                "app.mode".to_string(),
                SettingMetadata::text("normal").meta_str("label", "Mode"),
            );
            map
        }
    }

    let temp_dir = TempDir::new().unwrap();

    let config = SettingsConfig::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_storage::<YamlStorage>()
        .with_schema::<SimpleSettings>()
        .with_profiles()
        .build();

    let manager = SettingsManager::new(config).unwrap();

    // Save in default profile
    manager
        .save_setting("app", "mode", &json!("debug"))
        .unwrap();

    // Create and switch to production profile
    manager.create_profile("production").unwrap();
    manager.switch_profile("production").unwrap();

    // Verify production has default value
    let settings: SimpleSettings = manager.get_all().unwrap();
    assert_eq!(settings.app.mode, "normal");

    // Save production-specific
    manager
        .save_setting("app", "mode", &json!("release"))
        .unwrap();

    // Verify manifest is YAML
    assert!(temp_dir.path().join(".profiles.yaml").exists());

    // Verify profile settings are YAML
    let prod_settings = temp_dir
        .path()
        .join("profiles")
        .join("production")
        .join("settings.yaml");
    assert!(prod_settings.exists());
}

// =============================================================================
// YAML Edge Cases
// =============================================================================

#[test]
fn test_yaml_nested_structures() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_storage::<YamlStorage>()
        .with_sub_settings(SubSettingsConfig::new("configs"))
        .build()
        .unwrap();

    let configs = manager.sub_settings("configs").unwrap();

    // Save deeply nested structure
    configs
        .set(
            "complex",
            &json!({
                "server": {
                    "host": "localhost",
                    "port": 8080,
                    "tls": {
                        "enabled": true,
                        "cert_path": "/path/to/cert"
                    }
                },
                "database": {
                    "connection_string": "postgres://localhost/db"
                }
            }),
        )
        .unwrap();

    // Read back and verify structure preserved
    let loaded = configs.get_value("complex").unwrap();
    assert_eq!(loaded["server"]["host"], "localhost");
    assert_eq!(loaded["server"]["tls"]["enabled"], true);
    assert_eq!(
        loaded["database"]["connection_string"],
        "postgres://localhost/db"
    );
}

#[test]
fn test_yaml_arrays() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_storage::<YamlStorage>()
        .with_sub_settings(SubSettingsConfig::new("configs"))
        .build()
        .unwrap();

    let configs = manager.sub_settings("configs").unwrap();

    // Save with arrays
    configs
        .set(
            "with_arrays",
            &json!({
                "tags": ["tag1", "tag2", "tag3"],
                "ports": [80, 443, 8080],
                "enabled_features": ["auth", "logging"]
            }),
        )
        .unwrap();

    let loaded = configs.get_value("with_arrays").unwrap();
    assert_eq!(loaded["tags"].as_array().unwrap().len(), 3);
    assert_eq!(loaded["ports"][0], 80);
}

#[test]
fn test_yaml_special_characters_in_strings() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_storage::<YamlStorage>()
        .with_sub_settings(SubSettingsConfig::new("configs"))
        .build()
        .unwrap();

    let configs = manager.sub_settings("configs").unwrap();

    // Test special characters that might need escaping
    configs
        .set(
            "special",
            &json!({
                "path_with_backslash": "C:\\Users\\test",
                "string_with_quotes": "He said \"hello\"",
                "multiline_like": "line1\nline2\nline3",
                "unicode": "日本語テスト"
            }),
        )
        .unwrap();

    let loaded = configs.get_value("special").unwrap();
    assert_eq!(loaded["path_with_backslash"], "C:\\Users\\test");
    assert_eq!(loaded["string_with_quotes"], "He said \"hello\"");
    assert!(loaded["multiline_like"].as_str().unwrap().contains('\n'));
    assert_eq!(loaded["unicode"], "日本語テスト");
}

#[test]
fn test_yaml_optional_fields() {
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct ConfigWithOptional {
        name: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        description: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        port: Option<u16>,
    }

    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_storage::<YamlStorage>()
        .with_sub_settings(SubSettingsConfig::new("configs"))
        .build()
        .unwrap();

    let configs = manager.sub_settings("configs").unwrap();

    // Save with Some values
    let original_with = ConfigWithOptional {
        name: "test".to_string(),
        description: Some("A test config".to_string()),
        port: Some(8080),
    };
    configs.set("with_optional", &original_with).unwrap();

    // Save without optional fields (simulating None)
    let original_without = ConfigWithOptional {
        name: "minimal".to_string(),
        description: None,
        port: None,
    };
    configs.set("without_optional", &original_without).unwrap();

    // Both should load correctly
    let with_opt: ConfigWithOptional = configs.get("with_optional").unwrap();
    assert_eq!(with_opt, original_with);

    let without_opt: ConfigWithOptional = configs.get("without_optional").unwrap();
    assert_eq!(without_opt, original_without);

    // Verify skip_serializing_if works (keys should be missing in raw JSON)
    let raw_without = configs.get_value("without_optional").unwrap();
    assert!(raw_without.get("description").is_none());
    assert!(raw_without.get("port").is_none());
}

#[test]
fn test_yaml_concurrent_writes() {
    use std::sync::Arc;
    use std::thread;

    let temp_dir = TempDir::new().unwrap();

    let manager = Arc::new(
        SettingsManager::builder("test-app", "1.0.0")
            .with_config_dir(temp_dir.path())
            .with_storage::<YamlStorage>()
            .with_sub_settings(SubSettingsConfig::new("configs"))
            .build()
            .unwrap(),
    );

    let mut handles = vec![];

    for i in 0..5 {
        let manager_clone = Arc::clone(&manager);
        let handle = thread::spawn(move || {
            let configs = manager_clone.sub_settings("configs").unwrap();
            configs
                .set(
                    &format!("config{i}"),
                    &json!({"id": i, "data": format!("data{i}")}),
                )
                .unwrap();
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }

    // Verify all configs exist
    let configs = manager.sub_settings("configs").unwrap();
    let list = configs.list().unwrap();
    assert_eq!(list.len(), 5);

    for i in 0..5 {
        let config = configs.get_value(&format!("config{i}")).unwrap();
        assert_eq!(config["id"], i);
    }
}

// =============================================================================
// YAML Migration
// =============================================================================

#[test]
fn test_yaml_sub_settings_migrator() {
    let temp_dir = TempDir::new().unwrap();

    // Write old format YAML directly
    let configs_dir = temp_dir.path().join("configs");
    std::fs::create_dir_all(&configs_dir).unwrap();
    std::fs::write(
        configs_dir.join("old.yaml"),
        "name: old config\nlegacy_field: should be migrated\n",
    )
    .unwrap();

    // Create manager with migrator
    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_storage::<YamlStorage>()
        .with_sub_settings(
            SubSettingsConfig::new("configs").with_migrator(|mut value| {
                if let Some(obj) = value.as_object_mut() {
                    // Migrate legacy_field to new_field
                    if let Some(legacy) = obj.remove("legacy_field") {
                        obj.insert("migrated_field".into(), legacy);
                    }
                    // Add version
                    if !obj.contains_key("version") {
                        obj.insert("version".into(), json!(2));
                    }
                }
                value
            }),
        )
        .build()
        .unwrap();

    let configs = manager.sub_settings("configs").unwrap();
    let loaded = configs.get_value("old").unwrap();

    // Verify migration happened
    assert!(loaded.get("legacy_field").is_none());
    assert_eq!(loaded["migrated_field"], "should be migrated");
    assert_eq!(loaded["version"], 2);
}