envconf 0.1.1

Boilerplate free initialization of structs from environment variables
Documentation
  • Coverage
  • 71.43%
    5 out of 7 items documented1 out of 4 items with examples
  • Size
  • Source code size: 9.29 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.38 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 11s Average build duration of successful builds.
  • all releases: 11s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • c0dearm/envconf
    3 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • c0dearm

envconf

Rust Crates Docs License

Boilerplate free initialization of structs from environment variables

Usage

It is usual that programs rely on environment variables to define their behavior, especially for cloud and microservies applications. Imagine you need to setup a database connection by reading the following environment variables:

  • DB_HOST
  • DB_PORT
  • DB_USER
  • DB_PASSWORD
  • DB_NAME

With this library crate, it is as easy as this:

use envconf::{Setting, Error};

#[derive(Setting)]
struct DBSettings {
    #[conf(env = "DB_HOST", default = "localhost")]
    host: String,
    #[conf(env = "DB_PORT", default = 5432)]
    port: usize,
    #[conf(default = "myuser")]  // hardcoded setting
    user: String,
    #[conf(env = "DB_PASSWORD")] // env variable required
    password: String,
    #[conf(env = "DB_NAME", default = "mydb")]
    name: String,
}

fn main() -> Result<(), Error<'static>> {
    // This env is mandatory, so it needs to be set!
    std::env::set_var("DB_PASSWORD", "secret");

    // Initialize config from environment variables
    // Read the crate docs to check the possible Error variants
    let db_settings = DBSettings::init()?;

    assert_eq!(db_settings.host, "localhost");
    assert_eq!(db_settings.port, 5432);
    assert_eq!(db_settings.user, "myuser");
    assert_eq!(db_settings.password, "secret");
    assert_eq!(db_settings.name, "mydb");

    Ok(())
}

Contributing

If you find a vulnerability, bug or would like a new feature, open a new issue.

To introduce your changes into the codebase, submit a Pull Request.

Many thanks!

License

envconf is distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE and LICENSE-MIT, and COPYRIGHT for details.