Crate configure_me[][src]

This library aims to help with reading configuration of application from files, environment variables and command line arguments, merging it together and validating. It auto-generates most of the code for you based on configuration (heh) file. It creates a struct for you, which contains all the parsed and validated fields, so you can access the information quickly easily and idiomatically.

Important note: since this is generating code, it's intended to be used from build script.


Let's say, your application needs these parametrs to run:

  • Port - this is mandatory
  • IP address to bind to - defaults to
  • Path to TLS certificate - optional, the server will be unsecure if not given

First you create Toml configuration file specifying all the parameters:

name = "port"
type = "u16"
optional = false
name = "bind_addr"
# Yes, this works and  you can use your own T: Deserialize + FromStr as well!
type = "::std::net::Ipv4Addr" 
default = "::std::net::Ipv4Addr::new(0, 0, 0, 0)" # Rust expression that creates the value
name = "tls_cert"
type = "String"
# optional = true is the default, no need to add it here
# If the type is optional, it will be represented as Option<T>
# e.g. Option<String> in this case.

Then, you create a build script like this:

This example is not tested
extern crate configure_me;
fn main() {
    let mut out: std::path::PathBuf = std::env::var_os("OUT_DIR").unwrap().into();
    let config_spec = std::fs::File::open("config.toml").unwrap();
    let config_code = std::fs::File::create(&out).unwrap();
    configure_me::generate_source(config_spec, config_code).unwrap();

Add dependencies to Cargo.toml:

# ...
build = ""
serde = "1"
serde_derive = "1"
toml = "0.4"
configure_me = "0.1"

Create a module src/ for configuration:

This example is not tested
include!(concat!(env!("OUT_DIR"), "/"));

And finally add appropriate incantiations into src/

This example is not tested
extern crate serde;
extern crate serde_derive;
extern crate toml;
mod config;
fn main() {
    // This will read configuration from "/etc/my_awesome_server/server.conf" file and
    // the command-line arguments.
    let server_config = config::Config::gather("/etc/my_awesome_server/server.conf").unwrap();
    // Your code here
    // E.g.:
    let listener = std::net::TcpListener::bind((server_config.bind_addr, server_config.port)).expect("Failed to bind socket");



Error that occured during code generation



Generates the source code for you from provided toml configuration.