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§

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

Enums§

Reason
The reason for the failure to get environment variable

Functions§

get
Fetches the environment variable key from the current process and then tries to parse EString to expected type by annotations.
get_or_set_default
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.
sget
Fetches the environment variable key from the current process and returns value as EString.
sset
Sets the environment variable key to the value value for the currently running process and then returns value as a EString.

Type Aliases§

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