Crates.io | env-loader |
lib.rs | env-loader |
version | 0.3.2 |
source | src |
created_at | 2023-06-05 18:08:29.250726 |
updated_at | 2023-06-08 07:42:43.765114 |
description | Simple storage for env variables with typings |
homepage | |
repository | https://github.com/lemarco/env-loader |
max_upload_size | |
id | 883231 |
size | 20,948 |
ConfigLoader::new(..) returns error if one or more values cannot be read from .env file or any of provided constraints are violated; It is expected behaviour because you don't want to start application without required env values.
If you're trying to get a value with a type that mismatched type in schema you will get an error. Package should not panic in any case.
let env_values = convert! {
PORT: int, // typing is anything possible to lovercase to i32, int, integer: Int,int,INT,Integer,I32,etc..
HOST: str, // same rule for str | string
CRITICAL_FLAG: bool, // same rule for bool | boolean
LONG_VAR: i64 // same rule for i64 | long
};
let store = ConfigLoader::new(env_values, None).unwrap(); // second arg for custom env file
let port: i32 = store.get("PORT").unwrap();
let host: String = store.get("HOST").unwrap();
let flag: bool = store.get("CRITICAL_FLAG").unwrap();
let num: i64 = store.get("LONG_VAR").unwrap();
After name of variable and type devided by ':' you can add constraints devided by "=>"
let env_values = convert! {
PORT: int => min(1000) max(2000),
HOST: str => min(10),
CRITICAL_FLAG: bool => optional,
LONG_VAR: i64 => min(10000),
NOT_EMPTY_STR_VALUE:str => notEmpty
};
Trailing comma is not supported.
let env_values = convert! {
PORT: int,
HOST: str,
};
let store = ConfigLoader::new(env_values, Some(".env.test")).unwrap();
let port: i32 = store.get("PORT").unwrap();
let host: String = store.get("HOST").unwrap();