Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.16 new API #123

Merged
merged 39 commits into from
Sep 9, 2024
Merged

v0.16 new API #123

merged 39 commits into from
Sep 9, 2024

Conversation

allan2
Copy link
Owner

@allan2 allan2 commented Sep 6, 2024

This is a breaking fast-forward PR.

std::env::set_var is marked unsafe in the Rust 2024 edition. set_var is used in majority of functions in v0.15.

This PR introduces a non-modifying API for loading of environment variables. Modifying of the environment is also supported with improved ergonomics limiting the use of unsafe.

  • introduction of EnvLoader
    • with_path and with_reader entry points with deferred IO
    • supports optional loading and override using EnvSequence
    • non-modifying API with load
    • unsafe modifying API with load_and_modify
    • return type EnvMap is a HashMap newtype with EnvMap::var
  • load attribute macro
    • compatible with tokio
    • ergonomic thread-safe loading before the async runtime starts
  • Error type contains the variable name
    • VarError is now flattened to Error variants containing the called variable name
  • full examples
  • from_filename deprecated to prevent directory traversal attack
    • find example added to replicate the old behaviour

More docs and tests have to be added. Feedback is welcome.

Further reading:

Resolves #14, resolves #25, resolves #39, resolves #68, resolves #105, resolves #106, resolves #48
Progresses #112

`set::env_set_var` is unsafe in the 2024 edition.
All functions calling it are marked as unsafe.
- dotenv moved to dotenvy folder
- dotenvy-macros crate created for runtime macros
- temporary 2024 edition to check for unsafe `set_var` usage
- new API with `EnvLoader` builder, `load`, and `load_and_modify`
- `load` attribute macro that is thread-safe and async-compatible
- many examples added
- some irrelevant tests removed
This naming better reflects the fact that we are configuring a loader
with an optional path, with deferred I/O.
This removes the `EnvVar` variant which wraps `VarError`.
The internals of `VarError` are now variants in `Error`.

This makes it more ergonomic for users who are propogating errors to
know which varaible is truly missing.
This reintroduces `var` as a copy of `std::env::var` with more info.
The signature is different because in this crate, we only handle valid
UTF-8.

Calling `var` now gives the key name in the error.
This change `EnvMap` to a newtype with a `var` function that returns
`Result`.
This mirrors the behaviour of `dotenvy::var`.
This makes the CLI feature aligned with the attribute macro.

The optional example is also updated to be similar to the CLI.
`Path::exists` is no longer used because of a potential race condition.
Error messages are also adjusted.
@allan2 allan2 added this to the v0.16 milestone Sep 6, 2024
@aidenfarley
Copy link

Would adding a rust-toolchain.toml file with the nightly toolchain specified be beneficial to allow tests to compile?

@allan2
Copy link
Owner Author

allan2 commented Sep 6, 2024

No, I will revert the edition. More tests and docs still need to be added.

This creates an internal `ParseBufError` to handle line parsing errors
without the path context.

Loading a file reports the file path error if an attempt was made to
read from it and it is not found.
`EnvLoader::path` is a setter that can be used to specify the path.
If `with_reader` is used, the path can be specified for error context.

If both reader and path are present, load will use the reader as input.
Now that we only use paths and there is also reader input, we don't need
to test with temp file creating and deleting.
The tests are also rewritten for clarity.
Tests are also moved from separate files to the `EnvLoader` module.
The integration tests are no longer being used.
Testing is done at lower levels and will be stored as modules.
Unit tests run in parallel and may clobber each other.
temp-env is now a dev dependency to ensure that unit tests run in
isolated enviornments.

Some tests are added for `load` and `load_and_modify`.
This is to build with clap_builder v4.5.17, which requires 17.4.0.
1.74.0 came out November 2023, so this is less than one year ago.
Copy link

github-actions bot commented Sep 9, 2024

Code Coverage

@allan2 allan2 merged commit 17b438f into master Sep 9, 2024
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants