allan2 / dotenvy

A well-maintained fork of the dotenv crate
MIT License
708 stars 45 forks source link

fix(docs): Handle errors, rather than using `unwrap`, in examples #52

Closed LeoniePhiline closed 1 year ago

LeoniePhiline commented 1 year ago

This PR

Fixes #49

Notes

A note regarding the from_path* example changes

I simplified these examples, as they did not plainly show off usage and were somewhat nonsensical:

A note regarding dotenv_override example changes

github-actions[bot] commented 1 year ago

Code Coverage

allan2 commented 1 year ago

For most of these, I prefer the original. See this comparison:

Original

dotenvy::from_filename("custom.env").unwrap();

Suggested

fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenvy::from_filename("custom.env")?;
    Ok(())
}

The original is more legible for illustrating the usage of from_filename().

I can see ? being preferable to unwrap() in lengthier examples that are often copied and pasted, but that isn't usually the case here.

LeoniePhiline commented 1 year ago

@allan2 You're missing here that rustdoc renders this as:

dotenvy::from_filename("custom.env")?;

The entire code block is being run as a test. Lines prefixed with # are removed from user-facing documentation.

This is common practice, as well as best practice in Rust projects.

I would recommend you run cargo doc --open and have a look at the generated documentation.


I can see ? being preferable to unwrap() in lengthier examples that are often copied and pasted, but that isn't usually the case here.

This isn't at all related to lengthiness, but about proliferating proper error handling.

I am very sure the examples shown in the dotenvy documentation are copy-pasted very, very often.

allan2 commented 1 year ago

My apologies, I did miss that.

This PR looks good to me!