#configuration #apps #loader #derive #macro #configurs #file-loader

macro configurs_derive

Macro implementations for configurs, the configuration loader for Rust apps

1 unstable release

0.1.0 Jul 30, 2020

#122 in #loader

Apache-2.0

7KB
114 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

Dependencies

~1.5MB
~37K SLoC