#env-var #deserialize #derive-deserialize #traits #environment #variables #envy

jealousy

A wrapper arount the envy crate, providing an easily implementable trait

2 releases

0.1.5 Sep 26, 2023
0.1.3 Aug 28, 2023
0.1.2 Aug 28, 2023
0.1.1 Aug 28, 2023
0.1.0 Aug 28, 2023

#1432 in Rust patterns

31 downloads per month
Used in openmood

MIT license

7KB

Jealousy

Jealousy is a simple wrapper trait around the envy crate. The envy crate allows for easy deserialization of any structure from the environment. This crate adds the following features on top of that:

  • FromEnv trait that can be implemented on any deserializable struct
  • Logging surrounding the envy crate and errors, for easier debugging
  • derive macro (behind derive feature) to not have to write the boilerplate

Installation

You can either use cargo add jealousy or add jealousy = "0.1.3" to the dependencies in your Cargo.toml.

features

  • derive Allows for using the derive macro instead of having to implement the trait

Usage

The default impl of from_env should be enough for most use cases. The following will deserialize the struct's fields from the following environment variables:

  • CONFIG_VAR1
  • CONFIG_VAR2

Both methods, the trait AND the derive macro, use the default impl of the from_env method.

Trait

use serde::Deserialize;
use jealousy::FromEnv;

#[derive(Deserialize)]
struct Config {
    var1: String,
    var2: u16,
}

impl FromEnv for Config {
    const PREFIX: &'static str = "CONFIG";
}

Derive (requires feature: derive)

The derive macro mainly prevents the boilerplate you see above. It is functionally identical in every other way.

use serde::Deserialize;
use jealousy::FromEnv;

#[derive(Deserialize, FromEnv)]
#[from_env(prefix = "CONFIG")]
struct Config {
    var1: String,
    var2: u16,
}

Dependencies

~0.7–1.4MB
~30K SLoC