Crates.io | envsafe |
lib.rs | envsafe |
version | 0.1.0 |
source | src |
created_at | 2024-07-29 13:43:26.4328 |
updated_at | 2024-07-29 13:43:26.4328 |
description | Rust library to safely combine dotenv files with existing environment |
homepage | |
repository | https://github.com/sonro/envsafe |
max_upload_size | |
id | 1318895 |
size | 13,740 |
Load .env
files in Rust, without using unsafe!
# .env
SOME_ENV_VAR='Hello envsafe'
// main.rs
use std::error::Error as StdError;
fn main() -> Result<(), Box<dyn StdError>> {
let safe = envsafe::load()?;
if let Some(var) = safe.get("SOME_ENV_VAR") {
println!("SOME_ENV_VAR: {var}");
}
}
envsafe
approaches the environment as readonly - combining existing
variables with dotenv files in a single map. It is therefore thread-safe.
.env
file if it exists
.env
has syntax errorsCustomize how the EnvSafe
is loaded using [EnvSafeConfig
]. You can specify:
.env
filesThe following example always tries to load common.env
.
If the variable APP_ENV
is set to DEV
, it will first load the environment,
then dev.env
. It will fail if the file does not exist or has syntax errors
and will override all existing variables.
If APP_ENV
is set to PROD
, it will load prod.env
AND THEN load the
environment. All errors will be ignored.
EnvSafeConfig::<MAX_ENV_FILES, MAX_APP_ENVS>::new()
.add_envfile("common.env")
.app_env_config(
AppEnvConfig::new("APP_ENV")
.add_app_env(
"DEV",
EnvConfig::new()
.add_envfile_override("dev.env")
.sequence(EnvSequence::EnvThenFiles)
.errors(ErrorReturn::All),
)
.add_app_env(
"PROD",
EnvConfig::new()
.add_envfile("prod.env")
.sequence(EnvSequence::FilesThenEnv)
.errors(ErrorReturn::None),
),
)
.load()
.unwrap();
Only the EnvSafe
is allocated on the heap. All the configurations are kept
on the stack and are dropped when the EnvSafe
is loaded. This is why we
specify MAX_ENV_FILES
and MAX_APP_ENVS
in the example above.
dotenvy
currently modifies the
environment, which is unsafe in a multithreaded program on Unix-based systems.
This safety is trivial to enforce, but if you do not want to use unsafe
:
envsafe
may be a better choice. See
this issue for more details.
Thank you for considering contributing to envsafe!
We welcome any form of contribution:
Note: Before you take the time to open a pull request, please open an issue first.
envsafe
is distributed under the MIT License.