8 unstable releases (3 breaking)

0.4.1 Jul 24, 2024
0.4.0 Jul 23, 2024
0.3.2 Jun 22, 2024
0.2.2 Apr 17, 2023
0.1.0 Jul 22, 2022

#251 in #field

Download history 535/week @ 2024-06-21 12/week @ 2024-06-28 12/week @ 2024-07-05 316/week @ 2024-07-19 113/week @ 2024-07-26 10/week @ 2024-08-02 8/week @ 2024-08-09 2/week @ 2024-08-16 4/week @ 2024-08-23 23/week @ 2024-08-30 26/week @ 2024-09-06 25/week @ 2024-09-13 38/week @ 2024-09-20 18/week @ 2024-09-27 2/week @ 2024-10-04

83 downloads per month
Used in 2 crates (via envir)

MIT license

11KB
218 lines

These proc macros help you to implement the envir::Serialize and envir::Deserialize traits.

Attributes

By default, these macro use the uppercase field name as environment variable name.

use envir::Deserialize;

#[derive(envir::Deserialize, Debug)]
struct Config {
    home: String,
}

let config = Config::from_env();
dbg!(config);
$ cargo run
[src/main.rs:12] config = Ok(
    Config {
        home: "/home/sanpi",
    }
)

Container

  • prefix: sets this attributes to add this prefix at the field name.
use envir::Deserialize;

#[derive(envir::Deserialize, Debug)]
#[envir(prefix = "APP_")]
struct Config {
    dir: String,
}

let config = Config::from_env();
dbg!(config);
$ export APP_DIR=~/.config/app
$ cargo run
[src/main.rs:12] config = Ok(
    Config {
        dir: "/home/sanpi/.config/app",
    }
)

Field

  • name: use this name for the environment variable instead of the name of the field. If prefix is defined, it also prepend to this name;
  • export_with: use this function to export this field. The given function must be callable as fn (T) -> HashMap<String, String>;
  • load_with: use this function to load this field. The given function must be callable as fn (Hashmap<String, String>) -> envir::Result<T>;
  • noprefix: doesn’t add the prefix for this field;
  • nested: this field should be de/serialized recursively.
use envir::Deserialize;

#[derive(envir::Deserialize, Debug)]
#[envir(prefix = "APP_")]
struct Config {
    dir: String,
}

let config = Config::from_env();
dbg!(config);
$ export APP_DIR=~/.config/app
$ cargo run
[src/main.rs:12] config = Ok(
    Config {
        dir: "/home/sanpi/.config/app",
    }
)

Dependencies

~0.6–1MB
~23K SLoC