10 releases (2 stable)

Uses new Rust 2024

new 1.2.0 Nov 14, 2025
1.0.0 Dec 7, 2024
0.4.1 Jul 24, 2024
0.3.2 Jun 22, 2024
0.1.0 Jul 22, 2022

#37 in #field-name

Download history 18/week @ 2025-07-18 3/week @ 2025-08-15 17/week @ 2025-08-22 8/week @ 2025-08-29 11/week @ 2025-09-05 6/week @ 2025-09-12 6/week @ 2025-09-26 2/week @ 2025-10-03 24/week @ 2025-10-10 17/week @ 2025-10-17 20/week @ 2025-10-24 1/week @ 2025-10-31

62 downloads per month
Used in 3 crates (via envir)

MIT license

14KB
267 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;
  • skip: skip this field, don’t load or export it;
  • skip_load: don’t load this field;
  • skip_export: don’t export this field;
  • skip_export_if: call a function to determine whether to export this field or not. The given function must be callable as fn(&T) -> bool.
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

~3.5MB
~77K SLoC