Skip to main content

custom_root/
custom_root.rs

1use app_json_settings::ConfigManager;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Deserialize, Serialize)]
5struct PortableSettings {
6    recent_files: Vec<String>,
7    sidebar_visible: bool,
8}
9
10impl Default for PortableSettings {
11    fn default() -> Self {
12        Self {
13            recent_files: Vec::new(),
14            sidebar_visible: true,
15        }
16    }
17}
18
19fn main() -> app_json_settings::Result<()> {
20    // Caller-provided roots are useful for portable apps, tests, sandboxed
21    // hosts, and UWP-style apps where the host resolves its own local folder.
22    let root = std::env::temp_dir().join("app-json-settings-custom-root-example");
23
24    let manager = ConfigManager::<PortableSettings>::new()
25        .with_root_dir(&root)
26        .try_with_filename("preferences.json")?;
27
28    let mut settings = manager.load_or_default()?;
29
30    if settings.recent_files.is_empty() {
31        settings
32            .recent_files
33            .push("/example/documents/readme.md".to_string());
34        manager.save(&settings)?;
35    }
36
37    println!("custom root: {}", root.display());
38    println!("settings file: {}", manager.path().display());
39    println!("recent files: {:?}", settings.recent_files);
40
41    Ok(())
42}