autorun_config

Crates.ioautorun_config
lib.rsautorun_config
version0.1.1001
sourcesrc
created_at2024-11-14 16:16:16.686535
updated_at2024-11-14 16:22:17.933306
descriptionMacros to easily create configurations that can pull their data from command line arguments or environment variables
homepage
repositoryhttps://github.com/chazfg/autorun_config
max_upload_size
id1447982
size20,168
(chazfg)

documentation

README

Placeholder readme

usage:

use clap::{arg, command, value_parser};
use autorun_config::FromEnv;

#[derive(FromEnv, Debug)]
struct RuntimeConfig {
  uri: String,
  #[env(default = 22)]
  port: i32,
}


fn bootstrap() -> RuntimeConfig {
  let run_args = command!()
    .arg(
      .arg!(--uri <URI>).required(false)
    )
    .arg(
      .arg!(--port <PORT>).required(false).value_parser(value_parser!(i32))
    )
    .get_matches();

  RuntimeConfig::init_from_env(run_args)
}

When you define the bootstrap function and pass in the run args, the macro will derive the look ups to find the variables declared in the struct. Note that the field in the struct are equal to the "--" arguments. The macro will take it's field name and check for <field_name> in the clap arg parser and check for <FIELD_NAME> in the environment. Right now I only handle strings and i32. There is a default field you can specify as well.

Preference order: If found in command line: Return var else: if found in environment: Return var else: if default specificed: Return default else: panic!()

Commit count: 7

cargo fmt