[][src]Macro itconfig::config

macro_rules! config {
    ($($tokens:tt)*) => { ... };
}

Creates new public mod with function fo get each environment variable of mapping.

All variables are required and program will panic if some variables haven't value, but you can add default value for specific variable.

Starts with v0.6.0 if you don't have an optional variable, the variable is set automatically.

Example usage

config! {
    DATABASE_URL: String,
}

Config with default value

config! {
    DATABASE_URL: String,
    HOST: String => "127.0.0.1",
}

By default itconfig lib creates module with 'cfg' name. But you can use simple meta instruction if you want to rename module. In the example below we renamed module to 'configuration'

env::set_var("DEBUG", "t");

config! {
    #![mod_name = configuration]

    DEBUG: bool,
}

configuration::init();
assert_eq!(configuration::DEBUG(), true);

Namespaces

You can use namespaces for env variables

env::set_var("DEBUG", "t");
env::set_var("DATABASE_USERNAME", "user");
env::set_var("DATABASE_PASSWORD", "pass");
env::set_var("DATABASE_HOST", "localhost");

config! {
    DEBUG: bool,
    DATABASE {
        USERNAME: String,
        PASSWORD: String,
        HOST: String,
    }
}

Meta

If you want to read custom env name for variable you can change it manually.

A variable in the nameespace will lose environment prefix

env::set_var("MY_CUSTOM_NAME", "95");

config! {
    #[env_name = "MY_CUSTOM_NAME"]
    PER_PAGE: i32,

    APP {
        #[env_name = "MY_CUSTOM_NAME"]
        RECIPES_PER_PAGE: i32,
    }
}

cfg::init();
assert_eq!(cfg::PER_PAGE(), 95);
assert_eq!(cfg::APP::RECIPES_PER_PAGE(), 95);

Also you can add custom meta for each variable. For example feature configurations.

config! {
    #[cfg(feature = "postgres")]
    DATABASE_URL: String,

    #[cfg(not(feature = "postgres"))]
    DATABASE_URL: String,
}

Concatenate

Try to concatenate env variable or strings or both to you env variable. It's easy!

env::set_var("POSTGRES_USERNAME", "user");
env::set_var("POSTGRES_PASSWORD", "pass");

config! {
    DATABASE_URL < (
        "postgres://",
        POSTGRES_USERNAME,
        ":",
        POSTGRES_PASSWORD,
        "@",
        POSTGRES_HOST => "localhost:5432",
        "/",
        POSTGRES_DB => "test",
    ),
}

cfg::init();
assert_eq!(cfg::DATABASE_URL(), "postgres://user:pass@localhost:5432/test".to_string())

Concatinated variables can be only strings and support all features like namespaces and meta.

config! {
    CONCATED_NAMESPACE {
        #[env_name = "DATABASE_URL"]
        CONCAT_ENVVAR < (
            "postgres://",
            NOT_DEFINED_PG_USERNAME => "user",
            ":",
            NOT_DEFINED_PG_PASSWORD => "pass",
            "@",
            NOT_DEFINED_PG_HOST => "localhost:5432",
            "/",
            NOT_DEFINED_PG_DB => "test",
        ),
    }
}

cfg::init();

This module will also contain helper method:

pub fn init() {}

Run this at the main function for check all required variables without default value.

Panics

If you miss some required variables your application will panic at startup.

Examples

#[macro_use] extern crate itconfig;
// use dotenv::dotenv;

config! {
    DEBUG: bool => true,
    HOST: String => "127.0.0.1",
}

fn main () {
    // dotenv().ok();
    cfg::init();
    assert_eq!(cfg::HOST(), String::from("127.0.0.1"));
}