Loading & saving
dotcfg separates “there is no config yet” from “the config is broken.” You decide whether a missing file is normal, an error, or a reason to create defaults.
Optional: load()
Use this when your application can run without a saved config. A missing file becomes Ok(None); I/O and parse failures still return errors.
Rust
let config: Option<AppConfig> = cfg.load()?;
if let Some(config) = config {
println!("Loaded {}", config.theme);
}Required: load_or_error()
Use this when the application must be configured before it can continue.
Rust
let config: AppConfig = cfg.load_or_error()?;Create defaults: load_or_default()
This requires your type to implement Default. If the file is missing, dotcfg constructs T::default(), saves it, and returns that value.
Rust
#[derive(Serialize, Deserialize, Default)]
struct AppConfig {
theme: String,
}
let config: AppConfig = cfg.load_or_default()?;Save a complete config
save() serializes your struct in the selected format. The config directory is created automatically when it does not exist.
Rust
let config = AppConfig {
theme: "dark".into(),
};
cfg.save(&config)?;