Crates.io | envfmt |
lib.rs | envfmt |
version | 0.1.0 |
created_at | 2025-09-14 07:56:33.430366+00 |
updated_at | 2025-09-14 07:56:33.430366+00 |
description | Formats strings by expanding environment variables |
homepage | https://github.com/pyk/envfmt |
repository | https://github.com/pyk/envfmt |
max_upload_size | |
id | 1838455 |
size | 20,487 |
A lightweight Rust crate for expanding environment-style variables in strings, similar to shell expansion.
This crate provides a simple and efficient way to substitute variables in a
string, using either the process environment or a custom context like a
HashMap
.
envfmt
is built with only the Rust standard library
(plus thiserror
for convenience).$VAR
${VAR}
${VAR:-default}
$$
to get a literal $
envfmt::format()
for environment
variables, or envfmt::format_with()
with a HashMap
for easy testing and
custom data sources.HashMap<String, _>
and HashMap<&str, _>
keys out
of the box.Add envfmt
to your project's dependencies:
cargo add envfmt
This is the most common use case. First, ensure an environment variable is set in your shell:
export ETHERSCAN_API_KEY="key"
Then, use envfmt::format()
to expand it:
let key = envfmt::format("${ETHERSCAN_API_KEY}").unwrap();
assert_eq!(key, "key");
If a variable might be missing, you can provide a fallback default value:
let log_config = "${LOG_LEVEL:-INFO}";
let log_level = envfmt::format(log_config).unwrap();
assert_eq!(log_level, "INFO");
For advance usage, or if your variables come from a source other than the
environment, use envfmt::format_with()
. This is fast, safe, and doesn't
require modifying the global environment.
let mut context = HashMap::new();
context.insert("user", "Alice");
let template = "Hello $user!";
let message = envfmt::format_with(template, &context)?;
assert_eq!(message, "Hello Alice!");
This project is licensed under the MIT License.