10 unstable releases (3 breaking)

new 0.4.2 Jul 26, 2024
0.4.1 Jul 24, 2024
0.3.2 Jun 22, 2024
0.2.2 Apr 17, 2023
0.1.0 Jul 22, 2022

#482 in Encoding

Download history 14/week @ 2024-04-04 5/week @ 2024-04-18 55/week @ 2024-04-25 2/week @ 2024-05-16 2/week @ 2024-05-23 2/week @ 2024-05-30 3/week @ 2024-06-06 10/week @ 2024-06-13 555/week @ 2024-06-20 31/week @ 2024-06-27 26/week @ 2024-07-04 276/week @ 2024-07-18

346 downloads per month
Used in 2 crates

MIT license

17KB
383 lines

envir

Github actions Gitlab CI

A toolbox to deal with your environment.

Basics

Without feature, this crate provides simple functions to retreive the value of an environment variable:

  • get to retreive as string;
  • parse to directly parse the value as a desired type.

The try_ version of these functions return None if the variable doens’t exist when get and parse return the Error::Missing error.

In addition this crate provide a set function, like std::env::set_var but works for all types implement ToString.

Finally, a collect function to retreive all environment variables in a easy to print collection.

dotenv

The dotenv feature adds an eponyme function to load .env file.

logger

The logger feature adds logger configured via environment variables.

serde

The serde feature adds macro to deserialize struct from env:

use envir::Deserialize;

#[derive(envir::Deserialize)]
struct Config {
}

fn main() -> envir::Result {
    let config: Config = envir::from_env()?;
    // or
    let config = Config::from_env()?;

    Ok(())
}

And serialize to env:

use envir::Serialize;

#[derive(envir::Serialize, Default)]
struct Config {
}

let config = Config::default();
config.export();

The extrapolation feature allows environment variables replacement in the default macro attribute:

#[derive(envir::Deserialize)]
struct Config {
    #[envir(default = "/home/${USER}")]
    home: String,
}

You can read the envir_derive crate documentation for more informations.

Dependencies

~0–1.3MB
~20K SLoC