renvar 0.1.0

Library to deserialize environment variables in to Rust datatypes
Documentation
  • Coverage
  • 100%
    24 out of 24 items documented17 out of 19 items with examples
  • Size
  • Source code size: 70.17 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 5.96 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • westernwontons

Renvar

renvar is a library to deserialize environment variables into Rust data structures.

Huge thanks to softprops for envy, as this library is inspired by it and borrows much of the code from there.

Installation

Add it to your Cargo.toml

[dependencies]
renvar = "0.1"

Usage

use renvar::{from_env, from_iter, from_str};
use serde::Deserialize;
use std::env;

let env_content = r#"
name=renvar
type=Library
dependencies=serde
"#;

#[derive(Debug, Deserialize, PartialEq, Eq)]
enum CrateType {
    Library,
    Binary,
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
struct Renvar {
    name: String,
    #[serde(rename = "type")]
    typ: CrateType,
    dependencies: Vec<String>,
}

let actual = Renvar {
    name: "renvar".to_owned(),
    typ: CrateType::Library,
    dependencies: vec!["serde".to_owned()],
};

// we can read from strings

let value = from_str::<Renvar>(env_content).unwrap();

assert_eq!(value, actual);

// directly from the environment

let envs = vec![
    ("name".to_owned(), "renvar".to_owned()),
    ("type".to_owned(), "Library".to_owned()),
    ("dependencies".to_owned(), "serde".to_owned()),
];

for (key, value) in envs.clone().into_iter() {
    env::set_var(key, value);
}

let value = from_env::<Renvar>().unwrap();

assert_eq!(value, actual);

// or from iterables

let value = from_iter::<Renvar, _>(envs).unwrap();

assert_eq!(value, actual);

Feature flags

Renvar has the following feature flags:

prefixed

prefixed gives you the prefixed function, that accepts a prefix. The prefixes will be stripped away before deserialization.

postfixed

postfix is exactly the same as prefix, just with postfixes

case_insensitive_prefixed

Case insensitive variant of prefixed

case_insensitive_postfixed

Case insensitive variant of postfixed

with_trimmer

Finally, the with_trimmer feature flag gives you *_with_trimmer variants for all of the above, where you can strip extraneous characters off of the beginning and end of envrironment variables by passing a closure.

Supported datatypes

  • Strings and strs
  • enums
  • sequences
  • Unit structs

Development

Tests

If you have just, you run just test, otherwise cargo test --all-features