From 456b832088e2b06c1d9beca72a63bf7a1dbc40bc Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 7 Jul 2020 23:22:12 +0200 Subject: [PATCH 1/4] Replace all uses of .ok() None of them really use it to convert Result to Option --- README.md | 2 +- dotenv/examples/simple.rs | 2 +- dotenv/src/lib.rs | 12 ++++++------ dotenv/src/parse.rs | 8 ++++---- dotenv/tests/test-child-dir.rs | 2 +- dotenv/tests/test-default-location.rs | 2 +- dotenv/tests/test-dotenv-iter.rs | 2 +- dotenv/tests/test-from-filename-iter.rs | 2 +- dotenv/tests/test-from-filename.rs | 2 +- dotenv/tests/test-from-path-iter.rs | 2 +- dotenv/tests/test-from-path.rs | 2 +- 11 files changed, 19 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 1f91ab3..2a3b3bf 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ use dotenv::dotenv; use std::env; fn main() { - dotenv().ok(); + let _ = dotenv(); for (key, value) in env::vars() { println!("{}: {}", key, value); diff --git a/dotenv/examples/simple.rs b/dotenv/examples/simple.rs index e2cba32..fbadab7 100644 --- a/dotenv/examples/simple.rs +++ b/dotenv/examples/simple.rs @@ -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); diff --git a/dotenv/src/lib.rs b/dotenv/src/lib.rs index f2b1b0a..3b4db38 100644 --- a/dotenv/src/lib.rs +++ b/dotenv/src/lib.rs @@ -38,7 +38,7 @@ static START: Once = Once::new(); /// ``` pub fn var>(key: K) -> Result { START.call_once(|| { - dotenv().ok(); + let _ = dotenv(); }); env::var(key).map_err(Error::EnvVar) } @@ -61,7 +61,7 @@ pub fn var>(key: K) -> Result { /// ``` pub fn vars() -> Vars { START.call_once(|| { - dotenv().ok(); + let _ = dotenv(); }); env::vars() } @@ -109,7 +109,7 @@ pub fn from_path_iter>(path: P) -> Result> { /// # 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()`, @@ -117,7 +117,7 @@ pub fn from_path_iter>(path: P) -> Result> { /// /// ``` /// use dotenv; -/// dotenv::from_filename(".env").ok(); +/// let _ = dotenv::from_filename(".env"); /// ``` pub fn from_filename>(filename: P) -> Result { let (path, iter) = Finder::new().filename(filename.as_ref()).find()?; @@ -130,7 +130,7 @@ pub fn from_filename>(filename: P) -> Result { /// # 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()`, @@ -156,7 +156,7 @@ pub fn from_filename_iter>(filename: P) -> Result> { /// # Examples /// ``` /// use dotenv; -/// dotenv::dotenv().ok(); +/// let _ = dotenv::dotenv(); /// ``` pub fn dotenv() -> Result { let (path, iter) = Finder::new().find()?; diff --git a/dotenv/src/parse.rs b/dotenv/src/parse.rs index e68f84d..5a8c963 100644 --- a/dotenv/src/parse.rs +++ b/dotenv/src/parse.rs @@ -290,7 +290,7 @@ KEY4='fo ur' KEY5="fi ve" KEY6=s\ ix KEY7= -KEY8= +KEY8= KEY9= # foo KEY10 ="whitespace before =" KEY11= "whitespace after =" @@ -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; } @@ -345,7 +345,7 @@ export SHELL_LOVER=1 // Note 4 spaces after 'invalid' below let actual_iter = Iter::new( r#" - invalid + invalid very bacon = yes indeed =value"# .as_bytes(), @@ -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; } diff --git a/dotenv/tests/test-child-dir.rs b/dotenv/tests/test-child-dir.rs index 7c2b61e..359aac8 100644 --- a/dotenv/tests/test-child-dir.rs +++ b/dotenv/tests/test-child-dir.rs @@ -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(); diff --git a/dotenv/tests/test-default-location.rs b/dotenv/tests/test-default-location.rs index effd973..2afff7d 100644 --- a/dotenv/tests/test-default-location.rs +++ b/dotenv/tests/test-default-location.rs @@ -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(); diff --git a/dotenv/tests/test-dotenv-iter.rs b/dotenv/tests/test-dotenv-iter.rs index ec8c120..e0a9b66 100644 --- a/dotenv/tests/test-dotenv-iter.rs +++ b/dotenv/tests/test-dotenv-iter.rs @@ -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"); diff --git a/dotenv/tests/test-from-filename-iter.rs b/dotenv/tests/test-from-filename-iter.rs index 1d3ccbb..9adfc2e 100644 --- a/dotenv/tests/test-from-filename-iter.rs +++ b/dotenv/tests/test-from-filename-iter.rs @@ -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"); diff --git a/dotenv/tests/test-from-filename.rs b/dotenv/tests/test-from-filename.rs index 87dd74b..b30ef75 100644 --- a/dotenv/tests/test-from-filename.rs +++ b/dotenv/tests/test-from-filename.rs @@ -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"); diff --git a/dotenv/tests/test-from-path-iter.rs b/dotenv/tests/test-from-path-iter.rs index 2c4b763..ac9a4f5 100644 --- a/dotenv/tests/test-from-path-iter.rs +++ b/dotenv/tests/test-from-path-iter.rs @@ -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"); diff --git a/dotenv/tests/test-from-path.rs b/dotenv/tests/test-from-path.rs index 34aa80a..1e2eeb7 100644 --- a/dotenv/tests/test-from-path.rs +++ b/dotenv/tests/test-from-path.rs @@ -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"); From a09f751f65015ad42325f02af17c75106d80229b Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 13 Jul 2020 23:10:05 +0200 Subject: [PATCH 2/4] =?UTF-8?q?Replace=20all=20uses=20of=20`assert!(false,?= =?UTF-8?q?=20=E2=80=A6)`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dotenv/src/parse.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/dotenv/src/parse.rs b/dotenv/src/parse.rs index 5a8c963..a0fa5b9 100644 --- a/dotenv/src/parse.rs +++ b/dotenv/src/parse.rs @@ -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") } } @@ -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") } } @@ -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") } } @@ -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") } } } From 7d8c85496735b81bac7d3060372f6ab26083e38d Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 7 Jul 2020 23:26:54 +0200 Subject: [PATCH 3/4] Update README.md formatting --- README.md | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 2a3b3bf..4e99cfa 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 @@ -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: @@ -66,15 +64,13 @@ fn main() { } ``` -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 @@ -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: @@ -122,7 +117,7 @@ Then, in your crate: ```rust fn main() { - println!("{}", dotenv!("MEANING_OF_LIFE")); + println!("{}", dotenv!("MEANING_OF_LIFE")); } ``` From a07748478e491c161429951893c9e410fbc556a9 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sun, 11 Oct 2020 02:16:12 +0200 Subject: [PATCH 4/4] Undo accidental whitespace changes --- dotenv/src/parse.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dotenv/src/parse.rs b/dotenv/src/parse.rs index a0fa5b9..c7ec193 100644 --- a/dotenv/src/parse.rs +++ b/dotenv/src/parse.rs @@ -290,7 +290,7 @@ KEY4='fo ur' KEY5="fi ve" KEY6=s\ ix KEY7= -KEY8= +KEY8= KEY9= # foo KEY10 ="whitespace before =" KEY11= "whitespace after =" @@ -345,7 +345,7 @@ export SHELL_LOVER=1 // Note 4 spaces after 'invalid' below let actual_iter = Iter::new( r#" - invalid + invalid very bacon = yes indeed =value"# .as_bytes(),