2026-06-08 01:17:44 -04:00
2026-06-08 01:17:44 -04:00
2026-06-08 01:12:46 -04:00
2026-06-08 01:12:46 -04:00
2026-06-08 01:12:46 -04:00
2026-06-08 01:12:46 -04:00
2026-06-08 01:12:46 -04:00
2026-06-08 01:17:44 -04:00

envkit

envkit is a small Rust crate for reading environment variables and converting them into typed values.

It keeps the API minimal:

  • EnvLoader::get reads a required variable.
  • EnvLoader::get_or reads a variable with a fallback.
  • EnvLoader::get_opt returns Option<T>.
  • with_prefix lets you build loaders for namespaced variables like APP_PORT or DB_HOST.

Supported types

envkit currently supports:

  • String
  • bool
  • numeric types: i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64

Boolean parsing accepts:

  • true / false
  • 1 / 0
  • t / f

Quick start

use envkit::EnvLoader;

fn main() -> Result<(), envkit::error::EnvError> {
    let loader = EnvLoader;

    let port: u16 = loader.get("PORT")?;
    let debug: bool = loader.get_or("DEBUG", false);
    let app_name: String = loader.get_opt("APP_NAME").unwrap_or_else(|| "envkit".to_string());

    println!("port={port}, debug={debug}, app_name={app_name}");
    Ok(())
}

Prefixed variables

use envkit::EnvLoader;

fn main() -> Result<(), envkit::error::EnvError> {
    let loader = EnvLoader;
    let app = loader.with_prefix("APP_");

    let host: String = app.get("HOST")?;
    let port: u16 = app.get_or("PORT", 8080);

    println!("host={host}, port={port}");
    Ok(())
}

If you use the prefixed loader above, it reads APP_HOST and APP_PORT.

Errors

envkit returns EnvError for two cases:

  • MissingVar when the environment variable is not present
  • Invalid when parsing fails

Examples

See the examples/ folder for runnable usage samples.

Development

Run the test suite with:

cargo test
S
Description
A dead-simple env loader
Readme 45 KiB
v0.2.1 Latest
2026-06-08 11:38:41 -05:00
Languages
Rust 100%