Crate envconfig

source ·
Expand description

Envconfig is a Rust library that helps to initialize configuration structure from environment variables. It makes use of custom derive macros to reduce boilerplate.

Example

use std::env;
use envconfig::Envconfig;

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

    #[envconfig(from = "DB_PORT")]
    pub db_port: Option<u16>,

    #[envconfig(from = "HTTP_PORT", default = "8080")]
    pub http_port: u16,
}

// We assume that those environment variables are set somewhere outside
env::set_var("DB_HOST", "localhost");
env::set_var("DB_PORT", "5432");

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

assert_eq!(config.db_host, "localhost");
assert_eq!(config.db_port, Some(5432));
assert_eq!(config.http_port, 8080);

The library uses std::str::FromStr trait to convert environment variables into custom data type. So, if your data type does not implement std::str::FromStr the program will not compile.

Enums§

  • Represents an error, that may be returned by fn init_from_env() of trait Envconfig.

Traits§

  • Indicates that structure can be initialize from environment variables.

Functions§

  • Tries to load an environment variable by name and parse it into type T. If the environment variable is not present, it returns None.
  • Load an environment variable by name and parse it into type T.
  • Tries to load an environment variable by name and parse it into type T. If the environment variable is not present, it returns a default value.

Derive Macros§

  • Custom derive for trait [envconfig::Envconfig]