Crates.io | confique |
lib.rs | confique |
version | 0.3.0 |
source | src |
created_at | 2021-07-28 10:41:37.390471 |
updated_at | 2024-10-18 14:58:35.103673 |
description | Type-safe, layered, light-weight, `serde`-based configuration library |
homepage | |
repository | https://github.com/LukasKalbertodt/confique/ |
max_upload_size | |
id | 428305 |
size | 168,166 |
Confique is a rather light-weight library that helps with configuration management in a type-safe and DRY (don't repeat yourself) fashion.
Features:
Type safe: the code using the config values does not need to parse strings or unwrap
any Option
s.
All values already have the correct type.
Layered configuration: you can load from and then merge multiple sources of configuration.
Load config values from:
Based on serde
: less code in confique
(more light-weight) and access to a huge ecosystem of high quality parsers.
Easily generate configuration "templates": describe all available config values to your users without repeating yourself.
Simple validation: validity checks can easily be added via attributes.
use std::{net::IpAddr, path::PathBuf};
use confique::Config;
#[derive(Config)]
struct Conf {
/// Port to listen on.
#[config(env = "PORT", default = 8080)]
port: u16,
/// Bind address.
#[config(default = "127.0.0.1")]
address: IpAddr,
#[config(nested)]
log: LogConf,
}
#[derive(Config)]
struct LogConf {
#[config(default = true)]
stdout: bool,
#[config(validate(file.is_absolute(), "log file requires absolute path"))]
file: Option<PathBuf>,
#[config(default = ["debug"])]
ignored_modules: Vec<String>,
}
let config = Conf::builder()
.env()
.file("example-app.toml")
.file("/etc/example-app/config.toml")
.load()?;
See the documentation for more information.
With the above example, you can automatically generate a configuration template: a file in a chosen format that lists all values with their description, default values, and env values.
toml::template::<Conf>() |
yaml::template::<Conf>() |
json5::template::<Conf>() |
|
|
|
(Note: The "environment variable" sentence is on a single line; I just split it into two lines for readability in this README.)
config
"http.port"
) and deserialize at "use site".figment
serde
and also uses your own structs as data store, thus type safeserde
?Serde is not a configuration, but a deserialization library.
But you can get surprisingly far with just serde and it might actually be sufficient for your project.
However, once you want to load from multiple sources, you either have make all your fields Option
or repeat code/docs.
With confique
you also get some other handy helpers.
There is still some design space to explore and there are certainly still many features one could add. However, the core interface (the derive macro and the core traits) probably won't change a lot anymore. Confique is used by a web project (that's already used in production) which I'm developing alongside of confique.
Licensed under either of Apache License, Version 2.0 or MIT license at your option. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.