Crate enve

source ·
Expand description

enve

enve helps you work with environment variables and convert it to any type using only type annotations.

All standard environment variable types are included, but enve under the hood uses estring, so you can easily create your own type.

Usage

Basic

fn main() -> Result<(), enve::Error> {
    enve::sset("E", "10");

    let res: f32 = enve::get("E")?;

    println!("result: {}", res);

    Ok(())
}

You can use predefined structs like SepVec if you enable structs feature.

Note: You can use custom types as annotations! Just implement ParseFragment.

use enve::SepVec;

type PlusVec<T> = SepVec<T, '+'>;
type MulVec<T> = SepVec<T, '*'>;

fn main() -> Result<(), enve::Error> {
    enve::sset("E", "10+5*2+3");

    let res = enve::get::<PlusVec<MulVec<f32>>>("E")?
        .iter()
        .map(|m| m.iter().product::<f32>())
        .sum::<f32>();

    assert_eq!(res, 23.0);

    Ok(())
}

You can also use predefined aggregators if you enable aggs feature.

use enve::{SepVec, Product, Sum, estring::Aggregate};

type PlusVec<T> = SepVec<T, '+'>;
type MulVec<T> = SepVec<T, '*'>;

fn main() -> Result<(), enve::Error> {
    enve::sset("E", "10+5*2+3");

    let res = enve::get::<Sum<PlusVec<Product<MulVec<f32>>>>>("E")?.agg();

    assert_eq!(res, 23.0);

    Ok(())
}

Look at the examples to see the power!

Re-exports

pub use estring;

Structs

Wrapper under String type.
The error type for operations interacting with environment variables
Wrapper for pair (A, B) tuple to split string by a separator (S1).
Aggregate struct, that can multiply inner aggregatable items if Aggregatable::Item implements std::iter::Product
Wrapper for Vec to split string by a separator (SEP).
Aggregate struct, that can sum inner aggregatable items if Aggregatable::Item implements std::iter::Sum
Wrapper that allow to trim substring before continue
Wrapper for trio (A, B, C) tuple to split string by separators (S1 and S2).

Enums

The reason for the failure to get environment variable

Functions

Fetches the environment variable key from the current process and then tries to parse EString to expected type by annotations.
Fetches the environment variable key from the current process. It set value default if environment variable key ins’n set. Then this function tries to parse EString to expected type by annotations.
Fetches the environment variable key from the current process and returns value as EString.
Sets the environment variable key to the value value for the currently running process and then returns value as a EString.

Type Definitions

Splits substring by comma character and returns SepVec
Splits substring by semicolon character and returns SepVec