Crate configure_me

source ·
Expand description

This crate 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.

This is currently only a facade for the dependencies, the core of the crate is in configure_me_codegen crate.

Important: In order to use this crate, you need to create a build script using configure_me_codegen to generate the code that will use this crate! See the example.

Example

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

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

First create config_spec.toml configuration file specifying all the parameters:

[[param]]
name = "port"
type = "u16"
optional = false
doc = "Port to listen on."
 
[[param]]
name = "bind_addr"
type = "::std::net::Ipv4Addr" 
default = "::std::net::Ipv4Addr::new(0, 0, 0, 0)" # Rust expression that creates the value
doc = "IP address to bind to."
 
[[param]]
name = "tls_cert"
type = "::std::path::PathBuf"
doc = "Path to the TLS certificate. The connections will be unsecure if it isn't provided."

Then, create a simple build script:

extern crate configure_me;
 
fn main() {
    configure_me::build_script("config_spec.toml").unwrap();
}

Add dependencies to Cargo.toml:

[packge]
# ...
build = "build.rs"
 
[dependencies]
configure_me = "0.3"
serde = "1"
 
[build-dependencies]
configure_me_codegen = "0.3"

And finally add appropriate incantiations into src/main.rs:

#[macro_use]
extern crate configure_me;
 
include_config!();
 
fn main() {
    // This will read configuration from "/etc/my_awesome_server/server.conf" file and
    // the command-line arguments.
    let (server_config, _remaining_args) = Config::including_optional_config_files(&["/etc/my_awesome_server/server.conf]").unwrap_or_exit();
 
    // Your code here
    // E.g.:
    let listener = std::net::TcpListener::bind((server_config.bind_addr, server_config.port)).expect("Failed to bind socket");
}

Re-exports

pub extern crate parse_arg;
pub extern crate serde;
pub extern crate toml;

Macros