Stupid simple dotenv
A simple dotenv parser for Rust with no dependencies.
Reads key value pairs from an .env or any other file and stores them as easily available environment variables. Since dotenv is no longer maintained, this is an simpler smaller alternative.
Breaking Changes in 0.3.0
to_env() and file_to_env() no longer override existing environment variables.
This aligns with the standard dotenv behavior where shell exports take precedence over .env file values.
If you need the old behavior of overriding existing variables, use the new to_env_override() or file_to_env_override() functions.
// Standard behavior: does NOT override existing env vars
to_env.ok;
// Override behavior: DOES override existing env vars (old behavior)
to_env_override.ok;
Usage
use stupid_simple_dotenv;
API
| Function | Description |
|---|---|
to_env() |
Reads .env file, sets variables that don't exist yet |
to_env_override() |
Reads .env file, overrides all variables |
file_to_env(path) |
Reads custom file, sets variables that don't exist yet |
file_to_env_override(path) |
Reads custom file, overrides all variables |
to_vec() |
Reads .env file to a vector of key-value tuples |
file_to_vec(path) |
Reads custom file to a vector of key-value tuples |
get_or(key, default) |
Gets env var or returns default value |
Valid .env file
.env files are simply configuration files in which a key-value pair separated by a = character is specified per line. The keys are to be specified without quotes. The values can be specified also without, however, are permissible also: ", ' and ` as quotes. Comments are to be introduced with # and must stand in a new line. Comments after values are not recognized and interpreted as values. Spaces before or after keys and values are ignored. If they are intended, quotes must be used. Valid lines of an .env file for this parser are:
myuser = world
other_user = other user name
key0=value
key1= value
key2=value
key3='value'
key4=`value`
key5=` value with spaces `
#comment
key6 = value with spaces inside
key 7= value:with#special&UTF-8 Chars
key-8="value:with#special&UTF-8 Chars_and_quote"
key9="comments now enabled in the same Line" #this is a comment and will be ignored