Zero-boilerplate configuration management.
Focus on storing the right data, instead of worrying about how or where to store it.
use serde_derive::{Serialize, Deserialize};
#[derive(Default, Debug, Serialize, Deserialize)]
struct MyConfig {
version: u8,
api_key: String,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let cfg: MyConfig = confy::load("my-app-name", None)?;
dbg!(cfg);
Ok(())
}confy can be used with either TOML, YAML, or RON files.
TOML is the default language used with confy but any of the other languages can be used by enabling them with feature flags as shown below.
Note: you can only use one of these features at a time, so in order to use either of the optional features you have to disable default features.
To use YAML files with confy you have to make sure you have enabled the yaml_conf feature and disabled both toml_conf and ron_conf.
Enable the feature in Cargo.toml:
[dependencies.confy]
features = ["yaml_conf"]
default-features = falseFor using RON files with confy you have to make sure you have enabled the ron_conf feature and disabled both toml_conf and yaml_conf.
Enable the feature in Cargo.toml:
[dependencies.confy]
features = ["ron_conf"]
default-features = falseInformation about adding context to error messages can be found at Providing Context
confy uses the etcetera crate to store your configuration files, the common locations for those depend on the stratgey usd App Strategy or Native Strategy, below are the common OS paths, you can change the strategy using the built-in change_config_strategy function.
This is the default strategy in version 2.0.0+. It uses the XDG format on both Linux and MacOS systems.
| Linux | macOS | Windows |
|---|---|---|
$XDG_CONFIG_HOME/<project_path> or $HOME/.config/<project_path> |
$HOME/.config/<project_path> |
{FOLDERID_RoamingAppData}/<project_path>/config |
Where the <project_path> will be $MY_APP_NAME.
You can change to this strategy which is provided for both backwards capability and for GUI applications on macOS which is more traditional and expects this format.
| Linux | macOS | Windows |
|---|---|---|
$XDG_CONFIG_HOME/<project_path> or $HOME/.config/<project_path> |
$HOME/Library/Application Support/<project_path> |
{FOLDERID_RoamingAppData}/<project_path>/config |
Where the <project_path> will be rs.$MY_APP_NAME on macOS and just $MY_APP_NAME elsewhere.
In version 2.0.0 we moved from the ProjectDirs crate to etcetera allowing for both uses of XDG or the OS's Native paths.
- From now on the default will be using
XDGdirectories on macOS.
In this version we bumped several dependencies which have had changes with some of the default (de)serialization process:
serde_yamlv0.8 -> v0.9: v0.9 release notes. There were several breaking changes tov0.9.0and are listed in this release tag. Especially cases where previously numbers were parsed and now returnString. See the release notes for more details.tomlv0.5 -> v0.8: v0.8 CHANGELOG. Breaking change to how tuple variants work intoml, from the notes: "Serialization and deserialization of tuple variants has changed from being an array to being a table with the key being the variant name and the value being the array".
- The base functions
loadandstorehave been added an optional parameter in the event multiples configurations are needed, or ones with different filename. - The default configuration file is now named "default-config" instead of using the application's name. Put the second argument of
loadandstoreto be the same of the first one to keep the previous configuration file. - It is now possible to save the configuration as
tomlor asYAML. The configuration's file name's extension depends on the format used.
Starting with version 0.4.0 the configuration file are stored in the expected place for your system. See the directories crates for more information.
Before version 0.4.0, the configuration file was written in the current directory.
This work is triple-licensed under MIT, MIT/X11, or the Apache 2.0 (or any later version). You may choose any one of these three licenses if you use this work.
SPDX-License-Identifier: MIT OR X11 OR Apache-2.0+