#env-var #variables #environment #serde #struct #load #structured

enveror

A library to load environment variables into structs with serde

9 releases

0.1.8 Aug 5, 2024
0.1.7 Aug 5, 2024
0.1.6 Nov 25, 2023

#636 in Encoding

Download history 28/week @ 2024-07-29 220/week @ 2024-08-05 2/week @ 2024-08-12 6/week @ 2024-09-16 2/week @ 2024-09-23 3/week @ 2024-09-30

438 downloads per month

MIT license

15KB
357 lines

enveror

github crates.io docs.rs

Library for Structured Environment Variables

Feature

  • represent the structure of env-vals using .
  • define the structure of environment variables using serde in enveror
  • each variable (without object) is fully defined within a single line

Sample

config

If you want to define following structure,

{
  "STAGE": "dev",
  "CLOUD": {
    "API_KEY_ID": "hogehoge=hog",
    "API_SECRET_KEY": "fug+;l[l;uw:er-0-63-096z,nxvcafuga",
    "STORAGE": {
      "IMAGES": "myimages"
    }
  },
  "CORS_ORIGINS": ["http://localhost:3000", "", "https://enveror.example.com"],
  "WORKER_COUNT": 4,
  "TIMEOUT_SECONDS": 2.3,
  "EMPTY_STRING": " ",
  "SAMPLE": true,
  "CONFIG": {
    "FLAGS": [true, false, false, true],
    "NUMBERS": [1, 2, 3, 4, 5, 6, 7, 8, 9]
  }
}

You can set variable as following environment variables.

STAGE="dev"
CLOUD.API_KEY_ID="hogehoge=hog"
CLOUD.API_SECRET_KEY="fug+;l[l;uw:er\-0-63-096z,nxvcafuga"
CLOUD.STORAGE.IMAGES="myimages"
CORS_ORIGINS=["http://localhost:3000", "", "https://enveror.example.com"]
WORKER_COUNT=4
TIMEOUT_SECONDS=2.3
EMPTY_STRING=" "
SAMPLE=true
CONFIG.FLAGS=[true, false, false, true]
CONFIG.NUMBERS=[1, 2, 3, 4, 5, 6, 7, 8, 9]

code

The following is testing code for parsing and deserialization.

use std::path::PathBuf;

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
enum Stage {
    Dev,
    Prod,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
struct EnverorConfig {
    stage: Stage,
    cloud: Cloud,
    cors_origins: Vec<String>,
    worker_count: u8,
    timeout_seconds: f32,
    empty_string: String,
    sample: bool,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
struct Cloud {
    api_key_id: String,
    api_secret_key: String,
    storage: CloudStorage,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
struct CloudStorage {
    images: String,
}

extern crate enveror;

#[test]
fn parse_deserialize() -> Result<(), Box<dyn std::error::Error>> {
    enveror::Enveror::new()
        .ignore_default_config()
        .path(PathBuf::from("./tests/case_enveror"))
        .load()?
        .construct::<EnverorConfig>()?;
    Ok(())
}

Dependencies

~0.7–1.6MB
~35K SLoC