envconfgen

Crates.ioenvconfgen
lib.rsenvconfgen
version1.0.2
sourcesrc
created_at2024-07-02 02:04:10.660697
updated_at2024-07-02 22:13:54.545205
descriptionConfig struct generator.
homepage
repositoryhttps://github.com/h4-h/envconf
max_upload_size
id1289159
size9,193
hash (h4-h)

documentation

README

EnvConf

Simple and small crate for configuration structs.

Crate generates impl with:

  1. new public method that fetches values from environment
  2. #field_name public getters for values

Why this instead of X-config?

My crate panics if it can't found variable in environment and default is not specified and idk... it contains only 76 LOC.

Anyway, there is no big reason why to use my crate instead of cool and big X-config.

Install

  1. Using cli cargo add envconfgen
  2. Manually adding to Cargo.toml
# Cargo.toml

[dependencies]
envconfgen = "1.0.2"

Example

#[derive(envconfgen::EnvConfig)]
struct Config {
  // `var` specifies the environment variable name to fetch the value from.
  // If not provided, the uppercase field name (`TEST_VAR`) will be used.
  #[var("MY_TEST_VAR")]
  test_var: String,

  // `default` provides a fallback value if the environment variable is not set.
  // If not provided and the variable is missing, it will panic.
  //
  // This field does not use `var`, so it defaults to searching for `DATABASE_URL`.
  #[default("nothing")]
  database_url: String,
}

fn main() {
  let config = Config::new(); // Generated factory method

  // Crate also generates a public getters for fields
  // I don't think it's a good idea to use public fields for config struct
  println!("test_var: {}", config.test_var());
  println!("database_url: {}", config.database_url());
}

License - MIT

Contribution

Feel free to open issue or send PR.

Commit count: 7

cargo fmt