econf

Loads environment variables into your structs in one shot.

Latest version Documentation License Actions Status

econf allows to override struct fields with environment variables easily. This is useful to build up applications that optionally overrides some configuration with environment variables. Here is the basic usage:

```rust use econf::LoadEnv;

[derive(Debug, LoadEnv)]

struct A { x: bool, y: u64, }

fn main() { let a = A { x: true, y: 42, }; println!("Before: {:?}", a);

let a = econf::load(a, "PREFIX");
println!("After:  {:?}", a);

} ```

```sh $ ./app Before: A { x: true, y: 42 } After: A { x: true, y: 42 }

$ PREFIX_X=false ./app Before: A { x: true, y: 42 } After: A { x: false, y: 42 } ```

In this example,

The environment variables are all upper-case with _ separated.

Why econf?

There are some existing crates that provide similar features but econf is unique in the following ways:

Supported types

Nesting

Nested structs are supported.

```rust

[derive(LoadEnv)]

struct A { v1: usize, v2: B, }

[derive(LoadEnv)]

struct B { v1: usize, v2: usize, }

fn main() { let a = A { v1: 1, v2: B { v1: 2, v2: 3, }, };

let a = econf::load(a, "PREFIX");

} ```

In this example,

Fields in child structs can be specified by chaining the field names with _ as a separator. However, there're cases that names conflict. For example,

```rust

[derive(LoadEnv)]

struct A { v2_v1: usize, v2: B, }

[derive(LoadEnv)]

struct B { v1: usize, v2: usize, }

fn main() { let a = A { v2_v1: 1, v2: B { v1: 2, v2: 3, }, };

let a = econf::load(a, "PREFIX");

} ```

Here PREFIX_V2_V1 corresponds to both a.v2_v1 and a.v2.v1. In this case, econf prints warning through log facade and the value is loaded to both a.v2_v1 and a.v2.v1.

Skipping fields

Fields that do not implement LoadEnv or simply should not be loaded by econf can be skipped by adding the #[econf(skip)] helper attribute:

```rust

[derive(LoadEnv)]

struct A { x: bool, #[econf(skip)] y: u64, // will not be loaded by econf } ```

License: MIT