Skip to main content

basic/
basic.rs

1use app_json_settings::ConfigManager;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Deserialize, Serialize)]
5struct AppSettings {
6    theme: String,
7    window_width: u32,
8    window_height: u32,
9    show_welcome_tip: bool,
10}
11
12impl Default for AppSettings {
13    fn default() -> Self {
14        Self {
15            theme: "system".to_string(),
16            window_width: 1024,
17            window_height: 768,
18            show_welcome_tip: true,
19        }
20    }
21}
22
23fn main() -> app_json_settings::Result<()> {
24    // The production desktop form is usually:
25    //
26    // let manager = ConfigManager::<AppSettings>::for_app("my-gui-app")?;
27    //
28    // This example uses a temporary root so running it does not write to your
29    // real app settings directory.
30    let root = std::env::temp_dir().join("app-json-settings-basic-example");
31    let manager = ConfigManager::<AppSettings>::new()
32        .with_root_dir(root)
33        .try_with_filename("settings.json")?;
34
35    let settings = manager.load_or_default()?;
36
37    println!("settings path: {}", manager.path().display());
38    println!("theme: {}", settings.theme);
39    println!(
40        "window: {}x{}",
41        settings.window_width, settings.window_height
42    );
43
44    Ok(())
45}