Skip to main content

Module env

Module env 

Source
Expand description

Typed environment variable readers — required / with_default / optional / list / duration_secs / duration_millis. Typed readers for environment variables.

Each reader parses std::env::var into the type you ask for and returns a Result, so a missing or bad value fails at startup instead of quietly defaulting inside a handler.

§Quick start

ⓘ
use rustango::env;
use std::time::Duration;

struct AppConfig {
    database_url: String,
    port: u16,
    debug: bool,
    allowed_hosts: Vec<String>,
    session_ttl: Duration,
}

fn load_config() -> Result<AppConfig, env::EnvError> {
    Ok(AppConfig {
        database_url:  env::required::<String>("DATABASE_URL")?,
        port:          env::with_default("PORT", 8080)?,
        debug:         env::with_default("DEBUG", false)?,
        allowed_hosts: env::list("ALLOWED_HOSTS").unwrap_or_default(),
        session_ttl:   env::duration_secs("SESSION_TTL_SECS").unwrap_or(Duration::from_secs(3600)),
    })
}

Structs§

DatabaseUrlBuilder
Builds a database URL from separate fields.
EnvRequirement
One declared environment-variable requirement.
Validator
Collects the env vars an app needs and reports every missing one at once.

Enums§

DbDriver
Backends recognized by DatabaseUrlBuilder and database_url_from_env.
EnvError

Functions§

database_url_from_env
Resolve a database URL from environment variables.
duration_millis
Read an env var as a Duration, reading the value as milliseconds.
duration_secs
Read an env var as a Duration, reading the value as seconds.
list
Read a comma-separated env var into a Vec<T>. Empty entries, such as the one a trailing comma makes, are dropped. Gives None when the variable is unset.
optional
Read an optional env var. Returns None if unset, Some(parsed) if set.
required
Read a required env var and parse it into T.
with_default
Read an env var, returning default when unset.