2 releases

0.1.1 Jul 30, 2020
0.1.0 Jul 30, 2020

#761 in Configuration

Apache-2.0

5KB
75 lines

Configurs

Mildly opinionated configuration management for Rust apps. Supports loading config values from environment variables, TOML files, and inline-annotated default values.

Usage

Add this to your [dependencies] section of Cargo.toml:

configurs = "0.1"
configurs_derive = { version = "0.1" }

...or, if you'd like to use the option to load configuration from a TOML file:

configurs = "0.1"
configurs_derive = { version = "0.1", features = ["file_loader"] }

Then, and annotate your struct:

use configurs_derive::Configuration;

#[derive(Configuration)]
struct Configuration {
    #[env="PORT"]
    #[default="8088"]
    port: i16,

    #[env="HOST"]
    #[default="127.0.0.1"]
    host: String,
}

fn main() {
    let config = Configuration::load();
}

Running the program with HOST or PORT environment variables will now override the annotated defaults.

Using the file_loader feature

Additional dependencies:

serde = "1"
serde_derive = "1"
toml = "0.5"

Usage:

use serde_derive::Deserialize;

#[derive(Configuration, Deserialize)]
#[serde(default)]
struct Configuration {
    #[env="PORT"]
    #[default="8088"]
    port: i16,

    #[env="HOST"]
    #[default="127.0.0.1"]
    host: String,
}

fn main() {
    let config = Configuration::load_from_file("./Config.toml");
}

The values will be loaded in the following order or precedence:

  1. env variables
  2. config file
  3. annotated defaults

No runtime deps