All Projects → softprops → Envy

softprops / Envy

Licence: mit
deserialize env vars into typesafe structs with rust

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to Envy

Pyhocon
HOCON parser for Python
Stars: ✭ 355 (-24.15%)
Mutual labels:  configuration
Conform
Easy, powerful, and extendable configuration tooling for releases.
Stars: ✭ 384 (-17.95%)
Mutual labels:  configuration
Emacs4developers
A document to help developers to use Emacs as a developer
Stars: ✭ 430 (-8.12%)
Mutual labels:  configuration
Icingaweb2 Module Director
The Director aims to be your new favourite Icinga config deployment tool. Director is designed for those who want to automate their configuration deployment and those who want to grant their “point & click” users easy access to the configuration.
Stars: ✭ 359 (-23.29%)
Mutual labels:  configuration
Centraldogma
Highly-available version-controlled service configuration repository based on Git, ZooKeeper and HTTP/2
Stars: ✭ 378 (-19.23%)
Mutual labels:  configuration
Wechat Mini Program Wiki
Anyone can make a Wechat mini-program with the first and only English Wiki made to decrypt the Mini-program framework.
Stars: ✭ 388 (-17.09%)
Mutual labels:  configuration
Config
⚙ Config.Net - the easiest configuration framework for .NET developers
Stars: ✭ 349 (-25.43%)
Mutual labels:  configuration
Koanf
Light weight, extensible configuration management library for Go. Built in support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.
Stars: ✭ 450 (-3.85%)
Mutual labels:  configuration
Confy
🛋 Zero-boilerplate configuration management in Rust
Stars: ✭ 379 (-19.02%)
Mutual labels:  configuration
Serde
Serialization framework for Rust
Stars: ✭ 4,901 (+947.22%)
Mutual labels:  serde
Serde Yaml
Strongly typed YAML library for Rust
Stars: ✭ 364 (-22.22%)
Mutual labels:  serde
Hyperjson
A hyper-fast Python module for reading/writing JSON data using Rust's serde-json.
Stars: ✭ 374 (-20.09%)
Mutual labels:  serde
Agileconfig
基于.NET Core开发的轻量级分布式配置中心 / .NET Core light configuration server
Stars: ✭ 403 (-13.89%)
Mutual labels:  configuration
Isopod
An expressive DSL and framework for Kubernetes configuration without YAML
Stars: ✭ 358 (-23.5%)
Mutual labels:  configuration
Neovim Init.vim
🏮 The perfect Neovim configuration for productive people who wants to level up their Vim experience with a clean, minimal-looking aesthetic, as well as a highly extensible, easily customizable set of popular tools and shortcuts to boost productivity. 🏮
Stars: ✭ 440 (-5.98%)
Mutual labels:  configuration
Craco
Create React App Configuration Override, an easy and comprehensible configuration layer for create-react-app
Stars: ✭ 5,285 (+1029.27%)
Mutual labels:  configuration
Augeas
A configuration editing tool and API
Stars: ✭ 384 (-17.95%)
Mutual labels:  configuration
Vapor
Runtime configuration system for Elixir
Stars: ✭ 463 (-1.07%)
Mutual labels:  configuration
Spacemacs
A community-driven Emacs distribution - The best editor is neither Emacs nor Vim, it's Emacs *and* Vim!
Stars: ✭ 21,906 (+4580.77%)
Mutual labels:  configuration
Anyway config
Configuration library for Ruby gems and applications
Stars: ✭ 409 (-12.61%)
Mutual labels:  configuration

envy Github Actions Coverage Status Software License crates.io Latest API docs

deserialize environment variables into typesafe structs

📦 install

Add the following to your Cargo.toml file.

[dependencies]
envy = "0.4"

🤸 usage

A typical envy usage looks like the following. Assuming your rust program looks something like this...

💡 These examples use Serde's derive feature

use serde::Deserialize;

#[derive(Deserialize, Debug)]
struct Config {
  foo: u16,
  bar: bool,
  baz: String,
  boom: Option<u64>
}

fn main() {
    match envy::from_env::<Config>() {
       Ok(config) => println!("{:#?}", config),
       Err(error) => panic!("{:#?}", error)
    }
}

... export some environment variables

$ FOO=8080 BAR=true BAZ=hello yourapp

You should be able to access a completely typesafe config struct deserialized from env vars.

Envy assumes an env var exists for each struct field with a matching name in all uppercase letters. i.e. A struct field foo_bar would map to an env var named FOO_BAR.

Structs with Option type fields will successfully be deserialized when their associated env var is absent.

Envy also supports deserializing Vecs from comma separated env var values.

Because envy is built on top of serde, you can use all of serde's attributes to your advantage.

For instance let's say your app requires a field but would like a sensible default when one is not provided.

/// provides default value for zoom if ZOOM env var is not set
fn default_zoom() -> {
  32
}

#[derive(Deserialize, Debug)]
struct Config {
  foo: u16,
  bar: bool,
  baz: String,
  boom: Option<u64>,
  #[serde(default="default_zoom")]
  zoom: u16
}

The following will yield an application configured with a zoom of 32

$ FOO=8080 BAR=true BAZ=hello yourapp

The following will yield an application configured with a zoom of 10

$ FOO=8080 BAR=true BAZ=hello ZOOM=10 yourapp

The common pattern for prefixing env var names for a specific app is supported using the envy::prefixed(prefix) interface. Asumming your env vars are prefixed with APP_ the above example may instead look like

use serde::Deserialize;

#[derive(Deserialize, Debug)]
struct Config {
  foo: u16,
  bar: bool,
  baz: String,
  boom: Option<u64>
}

fn main() {
    match envy::prefixed("APP_").from_env::<Config>() {
       Ok(config) => println!("{:#?}", config),
       Err(error) => panic!("{:#?}", error)
    }
}

the expectation would then be to export the same environment variables prefixed with APP_

$ APP_FOO=8080 APP_BAR=true APP_BAZ=hello yourapp

👭 Consider this crate a cousin of envy-store, a crate for deserializing AWS parameter store values into typesafe structs and recap, a crate for deserializing named regex capture groups into typesafe structs.

Doug Tangren (softprops) 2016-2019

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].