Config and CLI
vld_app::env reads environment variables (or a .env file) into a serde_json::Value, coerces each text value by what the schema accepts and parses it with your vld schema. You get a typed settings struct or a VldEnvError naming every bad variable.
Install
Cargo.toml
[dependencies]
vld = "0.8"
vld-app = { version = "0.8", features = ["env"] }
.env files as well:
Cargo.toml
[dependencies]
vld-app = { version = "0.8", features = ["dotenv"] }
| Feature | Adds to vld_app::env |
|---|---|
env | from_env, from_env_prefixed, from_vars, from_vars_prefixed (std only, no dependency) |
dotenv | env + from_dotenv, read_dotenv (dotenvy 0.15, not re-exported) |
Example
use vld_app::env::prelude::*;
vld::schema! {
#[derive(Debug)]
pub struct Db {
pub url: String => vld::string().min(1),
pub pool_size: i64 => vld::number().int().min(1).with_default(10),
}
}
vld::schema! {
#[derive(Debug)]
pub struct AppSettings {
pub host: String => vld::string().min(1),
pub port: i64 => vld::number().int().min(1).max(65535),
pub debug: bool => vld::boolean().with_default(false),
pub db: Db => vld::nested(Db::parse_value),
}
}
// APP_HOST=0.0.0.0 APP_PORT=8080 APP_DB__URL=postgres://localhost/app
match from_env_prefixed::<AppSettings>("APP_") {
Ok(s) => println!("listening on {}:{}", s.host, s.port),
Err(e) => {
eprintln!("{e}"); // invalid environment:\n APP_PORT: ...
std::process::exit(1);
}
}
A .env file, with the process environment on top:
let vars = vld_app::env::read_dotenv(".env")?
.into_iter()
.chain(std::env::vars());
let settings: AppSettings = vld_app::env::from_vars(vars)?;
API
| Item | Description |
|---|---|
from_env::<T>() | Every process variable, no prefix |
from_env_prefixed::<T>(prefix) | Process variables starting with prefix (stripped) |
from_vars::<T>(pairs) | Any (name, value) pairs (&str or String); testable without set_var |
from_vars_prefixed::<T>(pairs, prefix) | The same with a prefix |
from_dotenv::<T>(path) | A .env file, without touching the process environment (dotenv) |
read_dotenv(path) | The file's (name, value) pairs in file order, to layer with others (dotenv) |
VldEnvError | Validation { error, vars } (vars[i] names the variable of error.issues[i]; var_issues() pairs them), Io (file not readable) or Source (.env syntax, a value/section conflict); implements Error with source() |
prelude | the functions, VldEnvError, vld::prelude::* |
T is any VldParse type: vld::schema!, #[derive(Validate)], or vld_openapi::schemars1' impl_vld_parse!.
Gotchas
- Names: the prefix is matched ASCII case-insensitively; the rest is lower-cased and split on
__into nested objects (APP_DB__POOL_SIZE→db.pool_size), so field names must be lower snake case. Names with an empty part (APP_,APP__X) and non-UTF-8 process variables are skipped. A repeated name keeps the later value. - Coercion: values go through
vld::schema::coerce_text(onlytrue/falseand canonical numbers become booleans / numbers;01234,TRUE,1e3stay text) andvld::schema::parse_coerced:APP_HOST=12345intovld::string()is"12345", and a single value intovld::array(..)is a one-element list. The same rules as web query strings. APP_DBtogether withAPP_DB__URLis aSourceerror: a key cannot be both a value and a section.- Issues are named by the variable read (in its original case), or
PREFIX+ the upper-cased key path when it is missing (APP_DB__URL). An issue inside a list is named by the list's variable. from_envwithout a prefix sees every variable (PATH,HOME, …); preferfrom_env_prefixed.