1 stable release
Uses old Rust 2015
1.0.0 | Feb 17, 2020 |
---|
#78 in #configuration-management
Used in projects
10KB
87 lines
confy
Zero-boilerplate configuration management.
Focus on storing the right data, instead of worrying about how to store it.
#[derive(Serialize, Deserialize)]
struct MyConfig {
version: u8,
api_key: String,
}
/// `MyConfig` implements `Default`
impl ::std::ops::Default for MyConfig {
fn default() -> Self { Self { version: 0, api_key: "".into() } }
}
fn main() -> Result<(), ::std::io::Error> {
let cfg = confy::load("my-app-name")?;
}
lib.rs
:
Zero-boilerplate configuration management
Why?
There are a lot of different requirements when selecting, loading and writing a config, depending on the operating system and other environment factors.
In many applications this burden is left to you, the developer of an application, to figure out where to place the configuration files.
This is where confy
comes in.
Idea
confy
takes care of figuring out operating system
specific and environment paths before reading and
writing a configuration.
It gives you easy access to a configuration file
which is mirrored into a Rust struct
via serde.
This way you only need to worry about the layout of
your configuration, not where and how to store it.
confy
uses the Default
trait in Rust to automatically
create a new configuration, if none is available to read
from yet.
This means that you can simply assume your application
to have a configuration, which will be created with
default values of your choosing, without requiring
any special logic to handle creation.
#[derive(Serialize, Deserialize)]
struct MyConfig {
version: u8,
api_key: String,
}
/// `MyConfig` implements `Default`
impl ::std::default::Default for MyConfig {
fn default() -> Self { Self { version: 0, api_key: "".into() } }
}
fn main() -> Result<(), ::std::io::Error> {
let cfg = confy::load("my-app-name")?;
}
Updating the configuration is then done via the store
function.
Dependencies
~375–790KB
~13K SLoC