Skip to main content

update/
update.rs

1use app_json_settings::ConfigManager;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Deserialize, Serialize)]
5struct AppSettings {
6    launch_count: u32,
7    theme: String,
8}
9
10impl Default for AppSettings {
11    fn default() -> Self {
12        Self {
13            launch_count: 0,
14            theme: "system".to_string(),
15        }
16    }
17}
18
19fn main() -> app_json_settings::Result<()> {
20    let root = std::env::temp_dir().join("app-json-settings-update-example");
21    let manager = ConfigManager::<AppSettings>::new()
22        .with_root_dir(root)
23        .try_with_filename("settings.json")?;
24
25    let updated = manager.update(|settings| {
26        settings.launch_count += 1;
27        if settings.theme.is_empty() {
28            settings.theme = "system".to_string();
29        }
30    })?;
31
32    println!("settings path: {}", manager.path().display());
33    println!("launch count: {}", updated.launch_count);
34    println!("theme: {}", updated.theme);
35
36    Ok(())
37}