3 unstable releases
Uses old Rust 2015
0.1.1 | Jan 20, 2018 |
---|---|
0.1.0 | Jan 20, 2018 |
0.0.1 | Nov 25, 2017 |
#871 in Configuration
703 downloads per month
Used in 5 crates
(3 directly)
28KB
627 lines
Configuration management.
This crate is intended to automatically bridge the gap from
envionmental configuration into your project. By deriving the
Configure
trait for your configuration, you can get an automatic
system for managing your configuration at runtime.
Deriving Configure
This library provides a custom derive which lets you derive the Configure
trait. It requires that your type implement Deserialize
.
We recommend that you implement configuration using these steps:
- Implement
Default
for your type, which provides the default values for each configuration field. - Derive both
Deserialize
andConfigure
for your type. Use the#[serde(default)]
attribute to fall back to the default implementation when the configurable values are not set.
For example:
#[macro_use]
extern crate configure;
extern crate serde;
#[macro_use]
extern crate serde_derive;
use std::net::SocketAddr;
use std::path::PathBuf;
#[derive(Deserialize, Configure)]
#[serde(default)]
pub struct Config {
addr: SocketAddr,
tls_cert: Option<PathBuf>,
}
impl Default for Config {
fn default() -> Config {
Config {
addr: "127.0.0.1:7878".parse().unwrap(),
tls_cert: None,
}
}
}
With this code, you can call Config::generate
to pull you configuration
from the environment, falling back to these default values if the end user
has not set custom configuration for it.
Dependencies
~3MB
~61K SLoC