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

Expose parse errors via Dotenv::iter. #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jsirois
Copy link

@jsirois jsirois commented Nov 4, 2023

Refactor dotenv::dotenv::Iter to expose parse errors for those
interested. The parse errors are structured as opaque strings to hide
the parsing implementation details (currently nom and nom's errors are
the internal currency).

This allows a consumer that cares about propagating errors in .env
files to use something like:

let env = dotenv::from_filename(".env")?;
let mut iter = env.iter();
while let Some((key, value)) = iter.try_next()? {
    if std::env::var(key).is_err() {
        std::env::set_var(key, value);
    }
}

Fixes #4

src/errors.rs Outdated
@@ -1,13 +1,15 @@
use std::env;
use std::error;
use std::fmt;
use std::fmt::Display;
use std::io;

#[derive(Debug)]
#[non_exhaustive]
Copy link
Author

@jsirois jsirois Nov 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! I did not know about non_exhaustive until this change and it is what allowed this PR's conservative approach of not breaking existing users / requiring a major semver bump.

Refactor `dotenv::dotenv::Iter` to expose parse errors for those
interested. The parse errors are structured as opaque strings to hide
the parsing implementation details (currently nom and nom's errors are
the internal currency).

This allows a consumer that cares about propagating errors in `.env`
files to use something like:
```
let env = dotenv::from_filename(".env")?;
let mut iter = env.iter();
while let Some((key, value)) = iter.try_next()? {
    if std::env::var(key).is_err() {
        std::env::set_var(key, value);
    }
}
```

Fixes arniu#4
@@ -24,7 +24,6 @@ DOUBLE_AND_SINGLE_QUOTES_INSIDE_BACKTICKS=`double "quotes" and single 'quotes' w
EXPAND_NEWLINES="expand\nnew\nlines"
DONT_EXPAND_UNQUOTED=dontexpand\nnewlines
DONT_EXPAND_SQUOTED='dontexpand\nnewlines'
DONT_EXPAND_SQUOTED='dontexpand\nnewlines'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed this duplicated line was unintentional (it was the only one in all the examples) and it got in the way of fixing tests to assert all expected values were observed. If this was intentional though, I can go back to work on handling this differently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid lines silently stops parsing remaining lines
1 participant