All Projects → greyblake → envconfig-rs

greyblake / envconfig-rs

Licence: MIT License
Build a config structure from environment variables in Rust without boilerplate

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to envconfig-rs

Config
12 factor configuration as a typesafe struct in as little as two function calls
Stars: ✭ 251 (+85.93%)
Mutual labels:  environment, configuration
environment
🌳 Environment variable configuration for Node.js made easy.
Stars: ✭ 12 (-91.11%)
Mutual labels:  environment, configuration
superconfig
Access environment variables. Also includes presence validation, type coercion and default values.
Stars: ✭ 33 (-75.56%)
Mutual labels:  configuration, 12-factor
Next Runtime Dotenv
Expose environment variables to the runtime config of Next.js
Stars: ✭ 136 (+0.74%)
Mutual labels:  environment, configuration
laravel-conditional-providers
THIS PACKAGE HAS BEEN DEPRECATED — Load Laravel service providers and facades based on the current environment.
Stars: ✭ 26 (-80.74%)
Mutual labels:  environment, configuration
Fig
A minimalist Go configuration library
Stars: ✭ 142 (+5.19%)
Mutual labels:  environment, configuration
cfg-rs
A Configuration Library for Rust Applications
Stars: ✭ 18 (-86.67%)
Mutual labels:  environment, configuration
Anura Server
the Anura configuration manger
Stars: ✭ 16 (-88.15%)
Mutual labels:  environment, configuration
ngx-env
Easily inject environment variables into your Angular applications
Stars: ✭ 73 (-45.93%)
Mutual labels:  environment, configuration
salak.rs
A multi layered configuration loader and zero-boilerplate configuration parser.
Stars: ✭ 27 (-80%)
Mutual labels:  environment, configuration
Conf
Go package for loading program configuration from multiple sources.
Stars: ✭ 70 (-48.15%)
Mutual labels:  environment, configuration
ts-dotenv
Strongly-typed environment variables for Node.js
Stars: ✭ 18 (-86.67%)
Mutual labels:  environment, 12-factor
Common Env
🔑 The only configuration library you will ever need
Stars: ✭ 67 (-50.37%)
Mutual labels:  environment, configuration
Environ Config
Python Application Configuration With Environment Variables
Stars: ✭ 210 (+55.56%)
Mutual labels:  environment, configuration
Erl Env
Make retrieving configuration parameters super fast(7x faster than application:get_env)and stable.
Stars: ✭ 32 (-76.3%)
Mutual labels:  environment, configuration
dart environment config
Environment specific config generator for Dart and Flutter applications during CI/CD builds
Stars: ✭ 87 (-35.56%)
Mutual labels:  environment, configuration
Phpdotenv
Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
Stars: ✭ 11,648 (+8528.15%)
Mutual labels:  environment, configuration
Env
Simple lib to parse environment variables to structs
Stars: ✭ 2,164 (+1502.96%)
Mutual labels:  environment, configuration
configuro
An opinionated configuration loading framework for Containerized and Cloud-Native applications.
Stars: ✭ 81 (-40%)
Mutual labels:  configuration, 12-factor
dotfiles
My personal app/env configs and dotfiles.
Stars: ✭ 27 (-80%)
Mutual labels:  environment, configuration

Envconfig logo

Build Status License Documentation

Initialize config structure from environment variables in Rust without boilerplate.

Install

[dependencies]
envconfig = "0.10.0"

Usage

Let's say you application relies on the following environment variables:

  • DB_HOST
  • DB_PORT

And you want to initialize Config structure like this one:

struct Config {
    host: String,
    port: u16,
}

You can achieve this with the following code without boilerplate:

use envconfig::Envconfig;

#[derive(Envconfig)]
pub struct Config {
    #[envconfig(from = "DB_HOST")]
    pub db_host: String,

    #[envconfig(from = "DB_PORT", default = "5432")]
    pub db_port: u16,
}

fn main() {
    // Assuming the following environment variables are set
    std::env::set_var("DB_HOST", "127.0.0.1");

    // Initialize config from environment variables or terminate the process.
    let config = Config::init_from_env().unwrap();

    assert_eq!(config.db_host, "127.0.0.1");
    assert_eq!(config.db_port, 5432);
}

Testing

When writing tests you should avoid using environment variables. Cargo runs Rust tests in parallel by default which means you can end up with race conditions in your tests if two or more are fighting over an environment variable.

To solve this you can initialise your struct from a HashMap<String, String> in your tests. The HashMap should match what you expect the real environment variables to be; for example DB_HOST environment variable becomes a DB_HOST key in your HashMap.

use envconfig::Envconfig;

#[derive(Envconfig)]
pub struct Config {
    #[envconfig(from = "DB_HOST")]
    pub db_host: String,

    #[envconfig(from = "DB_PORT", default = "5432")]
    pub db_port: u16,
}

#[test]
fn test_config_can_be_loaded_from_hashmap() {
    // Create a HashMap that looks like your environment 
    let mut hashmap = HashMap::new();
    hashmap.insert("DB_HOST".to_string(), "127.0.0.1".to_string());

    // Initialize config from a HashMap to avoid test race conditions
    let config = Config::init_from_hashmap(&hashmap).unwrap();

    assert_eq!(config.db_host, "127.0.0.1");
    assert_eq!(config.db_port, 5432);
}

Contributing

Running tests

Tests do some manipulation with environment variables, so to prevent flaky tests they have to be executed in a single thread:

cargo test -- --test-threads=1

License

MIT © Sergey Potapov

Contributors

  • greyblake Potapov Sergey - creator, maintainer.
  • allevo Tommaso Allevi - support nested structures
  • hobofan Maximilian Goisser - update dependencies
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].