Files
envkit/README.md
T
2026-06-08 12:09:43 -04:00

1.7 KiB

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
  • Vectors with any of the supported types

Boolean parsing accepts:

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

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