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

More idiomatic code #57

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# rust-dotenv
# rust-dotenv

![CI](https://github.com/dotenv-rs/dotenv/workflows/CI/badge.svg)
[![codecov](https://codecov.io/gh/dotenv-rs/dotenv/branch/master/graph/badge.svg)](https://codecov.io/gh/dotenv-rs/dotenv)
Expand All @@ -20,8 +20,7 @@ which setting environment variables is not practical. It loads environment
variables from a `.env` file, if available, and mashes those with the actual
environment variables provided by the operative system.

Usage
----
## Usage

The easiest and most common usage consists on calling `dotenv::dotenv` when the
application starts, which will load environment variables from a file named
Expand All @@ -35,8 +34,7 @@ use the `from_filename` and `from_path` methods provided by the crate.
behaves identically to `env!`, but first tries to load a `.env` file at compile
time.

Examples
----
## Examples

A `.env` file looks like this:

Expand All @@ -58,23 +56,21 @@ use dotenv::dotenv;
use std::env;

fn main() {
dotenv().ok();
let _ = dotenv();

for (key, value) in env::vars() {
println!("{}: {}", key, value);
}
}
```

Variable substitution
----
## Variable substitution

It's possible to reuse variables in the `.env` file using `$VARIABLE` syntax.
The syntax and rules are similar to bash ones, here's the example:


```sh

VAR=one
VAR_2=two

Expand Down Expand Up @@ -107,8 +103,7 @@ RESULT=$PATH #value: the contents of the $PATH environment variable, even though
Dotenv will parse the file, substituting the variables the way it's described in the comments.


Using the `dotenv!` macro
------------------------------------
## Using the `dotenv!` macro

Add `dotenv_codegen` to your dependencies, and add the following to the top of
your crate:
Expand All @@ -122,7 +117,7 @@ Then, in your crate:

```rust
fn main() {
println!("{}", dotenv!("MEANING_OF_LIFE"));
println!("{}", dotenv!("MEANING_OF_LIFE"));
}
```

Expand Down
2 changes: 1 addition & 1 deletion dotenv/examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dotenv::dotenv;
use std::env;

fn main() {
dotenv().ok();
let _ = dotenv();

for (key, value) in env::vars() {
println!("{}: {}", key, value);
Expand Down
12 changes: 6 additions & 6 deletions dotenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static START: Once = Once::new();
/// ```
pub fn var<K: AsRef<OsStr>>(key: K) -> Result<String> {
START.call_once(|| {
dotenv().ok();
let _ = dotenv();
});
env::var(key).map_err(Error::EnvVar)
}
Expand All @@ -61,7 +61,7 @@ pub fn var<K: AsRef<OsStr>>(key: K) -> Result<String> {
/// ```
pub fn vars() -> Vars {
START.call_once(|| {
dotenv().ok();
let _ = dotenv();
});
env::vars()
}
Expand Down Expand Up @@ -109,15 +109,15 @@ pub fn from_path_iter<P: AsRef<Path>>(path: P) -> Result<Iter<File>> {
/// # Examples
/// ```
/// use dotenv;
/// dotenv::from_filename("custom.env").ok();
/// let _ = dotenv::from_filename("custom.env");
/// ```
///
/// It is also possible to do the following, but it is equivalent to using `dotenv::dotenv()`,
/// which is preferred.
///
/// ```
/// use dotenv;
/// dotenv::from_filename(".env").ok();
/// let _ = dotenv::from_filename(".env");
/// ```
pub fn from_filename<P: AsRef<Path>>(filename: P) -> Result<PathBuf> {
let (path, iter) = Finder::new().filename(filename.as_ref()).find()?;
Expand All @@ -130,7 +130,7 @@ pub fn from_filename<P: AsRef<Path>>(filename: P) -> Result<PathBuf> {
/// # Examples
/// ```
/// use dotenv;
/// dotenv::from_filename("custom.env").ok();
/// let _ = dotenv::from_filename("custom.env");
/// ```
///
/// It is also possible to do the following, but it is equivalent to using `dotenv::dotenv()`,
Expand All @@ -156,7 +156,7 @@ pub fn from_filename_iter<P: AsRef<Path>>(filename: P) -> Result<Iter<File>> {
/// # Examples
/// ```
/// use dotenv;
/// dotenv::dotenv().ok();
/// let _ = dotenv::dotenv();
/// ```
pub fn dotenv() -> Result<PathBuf> {
let (path, iter) = Finder::new().find()?;
Expand Down
25 changes: 12 additions & 13 deletions dotenv/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ KEY4='fo ur'
KEY5="fi ve"
KEY6=s\ ix
KEY7=
KEY8=
KEY8=
jplatte marked this conversation as resolved.
Show resolved Hide resolved
KEY9= # foo
KEY10 ="whitespace before ="
KEY11= "whitespace after ="
Expand Down Expand Up @@ -321,7 +321,7 @@ export SHELL_LOVER=1
let mut count = 0;
for (expected, actual) in expected_iter.zip(actual_iter) {
assert!(actual.is_ok());
assert_eq!(expected, actual.ok().unwrap());
assert_eq!(expected, actual.unwrap());
count += 1;
}

Expand All @@ -345,7 +345,7 @@ export SHELL_LOVER=1
// Note 4 spaces after 'invalid' below
let actual_iter = Iter::new(
r#"
invalid
invalid
jplatte marked this conversation as resolved.
Show resolved Hide resolved
very bacon = yes indeed
=value"#
.as_bytes(),
Expand Down Expand Up @@ -425,7 +425,7 @@ mod variable_substitution_tests {
let mut count = 0;
for (expected, actual) in expected_iter.zip(actual_iter) {
assert!(actual.is_ok());
assert_eq!(expected, actual.ok().unwrap());
assert_eq!(expected, actual.unwrap());
count += 1;
}

Expand Down Expand Up @@ -588,17 +588,16 @@ mod error_tests {

assert_eq!(parsed_values.len(), 2);

if let Ok(first_line) = &parsed_values[0] {
assert_eq!(first_line, &(String::from("KEY"), String::from("VALUE")))
} else {
assert!(false, "Expected the first value to be parsed")
}
let first_line = parsed_values[0]
.as_ref()
.expect("Expected the first value to be parsed");
assert_eq!(first_line, &(String::from("KEY"), String::from("VALUE")));

if let Err(LineParse(second_value, index)) = &parsed_values[1] {
assert_eq!(second_value, wrong_value);
assert_eq!(*index, wrong_value.len() - 1)
} else {
assert!(false, "Expected the second value not to be parsed")
panic!("Expected the second value not to be parsed")
}
}

Expand All @@ -614,7 +613,7 @@ mod error_tests {
assert_eq!(second_value, wrong_key_value);
assert_eq!(*index, 0)
} else {
assert!(false, "Expected the second value not to be parsed")
panic!("Expected the second value not to be parsed")
}
}

Expand All @@ -629,7 +628,7 @@ mod error_tests {
assert_eq!(wrong_value, wrong_format);
assert_eq!(*index, 0)
} else {
assert!(false, "Expected the second value not to be parsed")
panic!("Expected the second value not to be parsed")
}
}

Expand All @@ -645,7 +644,7 @@ mod error_tests {
assert_eq!(wrong_value, wrong_escape);
assert_eq!(*index, wrong_escape.find("\\").unwrap() + 1)
} else {
assert!(false, "Expected the second value not to be parsed")
panic!("Expected the second value not to be parsed")
}
}
}
2 changes: 1 addition & 1 deletion dotenv/tests/test-child-dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn test_child_dir() {

env::set_current_dir("child").unwrap();

dotenv().ok();
let _ = dotenv();
assert_eq!(env::var("TESTKEY").unwrap(), "test_val");

env::set_current_dir(dir.path().parent().unwrap()).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion dotenv/tests/test-default-location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::common::*;
fn test_default_location() {
let dir = make_test_dotenv().unwrap();

dotenv().ok();
let _ = dotenv();
assert_eq!(env::var("TESTKEY").unwrap(), "test_val");

env::set_current_dir(dir.path().parent().unwrap()).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion dotenv/tests/test-dotenv-iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn test_dotenv_iter() {

assert!(env::var("TESTKEY").is_err());

iter.load().ok();
let _ = iter.load();

assert_eq!(env::var("TESTKEY").unwrap(), "test_val");

Expand Down
2 changes: 1 addition & 1 deletion dotenv/tests/test-from-filename-iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn test_from_filename_iter() {

assert!(env::var("TESTKEY").is_err());

iter.load().ok();
let _ = iter.load();

assert_eq!(env::var("TESTKEY").unwrap(), "test_val");

Expand Down
2 changes: 1 addition & 1 deletion dotenv/tests/test-from-filename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::common::*;
fn test_from_filename() {
let dir = make_test_dotenv().unwrap();

from_filename(".env").ok();
let _ = from_filename(".env");

assert_eq!(env::var("TESTKEY").unwrap(), "test_val");

Expand Down
2 changes: 1 addition & 1 deletion dotenv/tests/test-from-path-iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn test_from_path_iter() {

assert!(env::var("TESTKEY").is_err());

iter.load().ok();
let _ = iter.load();

assert_eq!(env::var("TESTKEY").unwrap(), "test_val");

Expand Down
2 changes: 1 addition & 1 deletion dotenv/tests/test-from-path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn test_from_path() {
let mut path = env::current_dir().unwrap();
path.push(".env");

from_path(&path).ok();
let _ = from_path(&path);

assert_eq!(env::var("TESTKEY").unwrap(), "test_val");

Expand Down