From 8c2ba38270e6862be47301e02bb09517604f4af9 Mon Sep 17 00:00:00 2001 From: Zoey Riordan Date: Fri, 26 Jun 2020 09:53:21 -0700 Subject: [PATCH] fix linting errors --- dotenv/src/bin/dotenv.rs | 22 +-- dotenv/src/errors.rs | 31 +++- dotenv/src/find.rs | 15 +- dotenv/src/iter.rs | 2 +- dotenv/src/lib.rs | 10 +- dotenv/src/parse.rs | 178 ++++++++++++--------- dotenv/tests/common/mod.rs | 5 +- 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 +- dotenv/tests/test-variable-substitution.rs | 69 ++++---- dotenv/tests/test-vars.rs | 2 +- dotenv_codegen/src/lib.rs | 2 +- dotenv_codegen_implementation/src/lib.rs | 10 +- 18 files changed, 207 insertions(+), 153 deletions(-) diff --git a/dotenv/src/bin/dotenv.rs b/dotenv/src/bin/dotenv.rs index 5af9a6b..dce09c6 100644 --- a/dotenv/src/bin/dotenv.rs +++ b/dotenv/src/bin/dotenv.rs @@ -3,7 +3,7 @@ extern crate dotenv; use clap::{App, AppSettings, Arg}; use std::os::unix::process::CommandExt; -use std::process::{Command, exit}; +use std::process::{exit, Command}; macro_rules! die { ($fmt:expr) => ({ @@ -33,26 +33,30 @@ fn main() { .setting(AppSettings::AllowExternalSubcommands) .setting(AppSettings::ArgRequiredElseHelp) .setting(AppSettings::UnifiedHelpMessage) - .arg(Arg::with_name("FILE") - .short("f") - .long("file") - .takes_value(true) - .help("Use a specific .env file (defaults to .env)")) + .arg( + Arg::with_name("FILE") + .short("f") + .long("file") + .takes_value(true) + .help("Use a specific .env file (defaults to .env)"), + ) .get_matches(); match matches.value_of("FILE") { None => dotenv::dotenv(), Some(file) => dotenv::from_filename(file), - }.unwrap_or_else(|e| die!("error: failed to load environment: {}", e)); + } + .unwrap_or_else(|e| die!("error: failed to load environment: {}", e)); let mut command = match matches.subcommand() { (name, Some(matches)) => { - let args = matches.values_of("") + let args = matches + .values_of("") .map(|v| v.collect()) .unwrap_or(Vec::new()); make_command(name, args) - }, + } _ => die!("error: missing required argument "), }; diff --git a/dotenv/src/errors.rs b/dotenv/src/errors.rs index bcce24b..a8df844 100644 --- a/dotenv/src/errors.rs +++ b/dotenv/src/errors.rs @@ -1,6 +1,6 @@ -use std::io; -use std::fmt; use std::error; +use std::fmt; +use std::io; pub type Result = std::result::Result; @@ -10,7 +10,7 @@ pub enum Error { Io(io::Error), EnvVar(std::env::VarError), #[doc(hidden)] - __Nonexhaustive + __Nonexhaustive, } impl Error { @@ -37,7 +37,11 @@ impl fmt::Display for Error { match self { Error::Io(err) => write!(fmt, "{}", err), Error::EnvVar(err) => write!(fmt, "{}", err), - Error::LineParse(line, error_index) => write!(fmt, "Error parsing line: '{}', error at line index: {}", line, error_index), + Error::LineParse(line, error_index) => write!( + fmt, + "Error parsing line: '{}', error at line index: {}", + line, error_index + ), _ => unreachable!(), } } @@ -52,14 +56,22 @@ mod test { #[test] fn test_io_error_source() { let err = Error::Io(std::io::ErrorKind::PermissionDenied.into()); - let io_err = err.source().unwrap().downcast_ref::().unwrap(); + let io_err = err + .source() + .unwrap() + .downcast_ref::() + .unwrap(); assert_eq!(std::io::ErrorKind::PermissionDenied, io_err.kind()); } #[test] fn test_envvar_error_source() { let err = Error::EnvVar(std::env::VarError::NotPresent); - let var_err = err.source().unwrap().downcast_ref::().unwrap(); + let var_err = err + .source() + .unwrap() + .downcast_ref::() + .unwrap(); assert_eq!(&std::env::VarError::NotPresent, var_err); } @@ -105,6 +117,9 @@ mod test { fn test_lineparse_error_display() { let err = Error::LineParse("test line".to_string(), 2); let err_desc = format!("{}", err); - assert_eq!("Error parsing line: 'test line', error at line index: 2", err_desc); + assert_eq!( + "Error parsing line: 'test line', error at line index: 2", + err_desc + ); } -} \ No newline at end of file +} diff --git a/dotenv/src/find.rs b/dotenv/src/find.rs index ab44e82..dd60728 100644 --- a/dotenv/src/find.rs +++ b/dotenv/src/find.rs @@ -6,7 +6,7 @@ use crate::errors::*; use crate::iter::Iter; pub struct Finder<'a> { - filename: &'a Path, + filename: &'a Path, } impl<'a> Finder<'a> { @@ -34,9 +34,11 @@ pub fn find(directory: &Path, filename: &Path) -> Result { let candidate = directory.join(filename); match fs::metadata(&candidate) { - Ok(metadata) => if metadata.is_file() { - return Ok(candidate); - }, + Ok(metadata) => { + if metadata.is_file() { + return Ok(candidate); + } + } Err(error) => { if error.kind() != io::ErrorKind::NotFound { return Err(Error::Io(error)); @@ -47,6 +49,9 @@ pub fn find(directory: &Path, filename: &Path) -> Result { if let Some(parent) = directory.parent() { find(parent, filename) } else { - Err(Error::Io(io::Error::new(io::ErrorKind::NotFound, "path not found"))) + Err(Error::Io(io::Error::new( + io::ErrorKind::NotFound, + "path not found", + ))) } } diff --git a/dotenv/src/iter.rs b/dotenv/src/iter.rs index f38b447..40506e2 100644 --- a/dotenv/src/iter.rs +++ b/dotenv/src/iter.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::env; -use std::io::{BufReader, Lines}; use std::io::prelude::*; +use std::io::{BufReader, Lines}; use crate::errors::*; use crate::parse; diff --git a/dotenv/src/lib.rs b/dotenv/src/lib.rs index e39e2de..f2b1b0a 100644 --- a/dotenv/src/lib.rs +++ b/dotenv/src/lib.rs @@ -5,20 +5,20 @@ //! file, if available, and mashes those with the actual environment variables //! provided by the operating system. -mod parse; mod errors; -mod iter; mod find; +mod iter; +mod parse; use std::env::{self, Vars}; use std::ffi::OsStr; use std::fs::File; use std::path::{Path, PathBuf}; -use std::sync::{Once}; +use std::sync::Once; pub use crate::errors::*; -use crate::iter::Iter; use crate::find::Finder; +use crate::iter::Iter; static START: Once = Once::new(); @@ -49,7 +49,7 @@ pub fn var>(key: K) -> Result { /// The returned iterator contains a snapshot of the process's environment variables at the /// time of this invocation, modifications to environment variables afterwards will not be /// reflected in the returned iterator. -/// +/// /// Examples: /// /// ```no_run diff --git a/dotenv/src/parse.rs b/dotenv/src/parse.rs index 5fa3deb..e68f84d 100644 --- a/dotenv/src/parse.rs +++ b/dotenv/src/parse.rs @@ -5,7 +5,10 @@ use crate::errors::*; // for readability's sake pub type ParsedLine = Result>; -pub fn parse_line(line: &str, substitution_data: &mut HashMap>) -> ParsedLine { +pub fn parse_line( + line: &str, + substitution_data: &mut HashMap>, +) -> ParsedLine { let mut parser = LineParser::new(line, substitution_data); parser.parse_line() } @@ -31,7 +34,7 @@ impl<'a> LineParser<'a> { } fn err(&self) -> Error { - return Error::LineParse(self.original_line.into(), self.pos); + Error::LineParse(self.original_line.into(), self.pos) } fn parse_line(&mut self) -> ParsedLine { @@ -66,7 +69,7 @@ impl<'a> LineParser<'a> { self.substitution_data .insert(key.clone(), Some(parsed_value.clone())); - return Ok(Some((key, parsed_value))); + Ok(Some((key, parsed_value))) } fn parse_key(&mut self) -> Result { @@ -90,7 +93,7 @@ impl<'a> LineParser<'a> { } fn expect_equal(&mut self) -> Result<()> { - if !self.line.starts_with("=") { + if !self.line.starts_with('=') { return Err(self.err()); } self.line = &self.line[1..]; @@ -116,7 +119,10 @@ enum SubstitutionMode { EscapedBlock, } -fn parse_value(input: &str, substitution_data: &mut HashMap>) -> Result { +fn parse_value( + input: &str, + substitution_data: &mut HashMap>, +) -> Result { let mut strong_quote = false; // ' let mut weak_quote = false; // " let mut escaped = false; @@ -148,7 +154,7 @@ fn parse_value(input: &str, substitution_data: &mut HashMap output.push(c), - 'n' => output.push('\n'), // handle \n case + 'n' => output.push('\n'), // handle \n case _ => { return Err(Error::LineParse(input.to_owned(), index)); } @@ -171,7 +177,11 @@ fn parse_value(input: &str, substitution_data: &mut HashMap(), &mut output); + apply_substitution( + substitution_data, + &substitution_name.drain(..).collect::(), + &mut output, + ); if c == '$' { substitution_mode = if !strong_quote && !escaped { SubstitutionMode::Block @@ -187,7 +197,11 @@ fn parse_value(input: &str, substitution_data: &mut HashMap { if c == '}' { substitution_mode = SubstitutionMode::None; - apply_substitution(substitution_data, &substitution_name.drain(..).collect::(), &mut output); + apply_substitution( + substitution_data, + &substitution_name.drain(..).collect::(), + &mut output, + ); } else { substitution_name.push(c); } @@ -224,18 +238,36 @@ fn parse_value(input: &str, substitution_data: &mut HashMap(), &mut output); + apply_substitution( + substitution_data, + &substitution_name.drain(..).collect::(), + &mut output, + ); Ok(output) } } -fn apply_substitution(substitution_data: &mut HashMap>, substitution_name: &str, output: &mut String) { +fn apply_substitution( + substitution_data: &mut HashMap>, + substitution_name: &str, + output: &mut String, +) { if let Ok(environment_value) = std::env::var(substitution_name) { output.push_str(&environment_value); } else { - let stored_value = substitution_data.get(substitution_name).unwrap_or(&None).to_owned(); + let stored_value = substitution_data + .get(substitution_name) + .unwrap_or(&None) + .to_owned(); output.push_str(&stored_value.unwrap_or_else(String::new)); }; } @@ -249,7 +281,8 @@ mod test { #[test] fn test_parse_line_env() { // Note 5 spaces after 'KEY8=' below - let actual_iter = Iter::new(r#" + let actual_iter = Iter::new( + r#" KEY=1 KEY2="2" KEY3='3' @@ -263,7 +296,9 @@ KEY10 ="whitespace before =" KEY11= "whitespace after =" export="export as key" export SHELL_LOVER=1 -"#.as_bytes()); +"# + .as_bytes(), + ); let expected_iter = vec![ ("KEY", "1"), @@ -279,8 +314,9 @@ export SHELL_LOVER=1 ("KEY11", "whitespace after ="), ("export", "export as key"), ("SHELL_LOVER", "1"), - ].into_iter() - .map(|(key, value)| (key.to_string(), value.to_string())); + ] + .into_iter() + .map(|(key, value)| (key.to_string(), value.to_string())); let mut count = 0; for (expected, actual) in expected_iter.zip(actual_iter) { @@ -294,19 +330,26 @@ export SHELL_LOVER=1 #[test] fn test_parse_line_comment() { - let result: Result> = Iter::new(r#" + let result: Result> = Iter::new( + r#" # foo=bar -# "#.as_bytes()).collect(); +# "# + .as_bytes(), + ) + .collect(); assert!(result.unwrap().is_empty()); } #[test] fn test_parse_line_invalid() { // Note 4 spaces after 'invalid' below - let actual_iter = Iter::new(r#" + let actual_iter = Iter::new( + r#" invalid very bacon = yes indeed -=value"#.as_bytes()); +=value"# + .as_bytes(), + ); let mut count = 0; for actual in actual_iter { @@ -318,7 +361,8 @@ very bacon = yes indeed #[test] fn test_parse_value_escapes() { - let actual_iter = Iter::new(r#" + let actual_iter = Iter::new( + r#" KEY=my\ cool\ value KEY2=\$sweet KEY3="awesome stuff \"mang\"" @@ -326,7 +370,9 @@ KEY4='sweet $\fgs'\''fds' KEY5="'\"yay\\"\ "stuff" KEY6="lol" #well you see when I say lol wh KEY7="line 1\nline 2" -"#.as_bytes()); +"# + .as_bytes(), + ); let expected_iter = vec![ ("KEY", r#"my cool value"#), @@ -336,8 +382,9 @@ KEY7="line 1\nline 2" ("KEY5", r#"'"yay\ stuff"#), ("KEY6", "lol"), ("KEY7", "line 1\nline 2"), - ].into_iter() - .map(|(key, value)| (key.to_string(), value.to_string())); + ] + .into_iter() + .map(|(key, value)| (key.to_string(), value.to_string())); for (expected, actual) in expected_iter.zip(actual_iter) { assert!(actual.is_ok()); @@ -347,12 +394,15 @@ KEY7="line 1\nline 2" #[test] fn test_parse_value_escapes_invalid() { - let actual_iter = Iter::new(r#" + let actual_iter = Iter::new( + r#" KEY=my uncool value KEY2="why KEY3='please stop'' KEY4=h\8u -"#.as_bytes()); +"# + .as_bytes(), + ); for actual in actual_iter { assert!(actual.is_err()); @@ -368,7 +418,8 @@ mod variable_substitution_tests { let actual_iter = Iter::new(input_string.as_bytes()); let expected_count = &expected_parse_result.len(); - let expected_iter = expected_parse_result.into_iter() + let expected_iter = expected_parse_result + .into_iter() .map(|(key, value)| (key.to_string(), value.to_string())); let mut count = 0; @@ -388,30 +439,20 @@ mod variable_substitution_tests { KEY=test KEY1="${KEY}" "#, - vec![ - ("KEY", "test"), - ("KEY1", "test"), - ], + vec![("KEY", "test"), ("KEY1", "test")], ); } #[test] fn substitute_undefined_variables_to_empty_string() { - assert_parsed_string( - r#"KEY=">$KEY1<>${KEY2}<""#, - vec![ - ("KEY", "><><"), - ], - ); + assert_parsed_string(r#"KEY=">$KEY1<>${KEY2}<""#, vec![("KEY", "><><")]); } #[test] fn do_not_substitute_variables_with_dollar_escaped() { assert_parsed_string( "KEY=>\\$KEY1<>\\${KEY2}<", - vec![ - ("KEY", ">$KEY1<>${KEY2}<"), - ], + vec![("KEY", ">$KEY1<>${KEY2}<")], ); } @@ -419,20 +460,13 @@ mod variable_substitution_tests { fn do_not_substitute_variables_in_weak_quotes_with_dollar_escaped() { assert_parsed_string( r#"KEY=">\$KEY1<>\${KEY2}<""#, - vec![ - ("KEY", ">$KEY1<>${KEY2}<"), - ], + vec![("KEY", ">$KEY1<>${KEY2}<")], ); } #[test] fn do_not_substitute_variables_in_strong_quotes() { - assert_parsed_string( - "KEY='>${KEY1}<>$KEY2<'", - vec![ - ("KEY", ">${KEY1}<>$KEY2<"), - ], - ); + assert_parsed_string("KEY='>${KEY1}<>$KEY2<'", vec![("KEY", ">${KEY1}<>$KEY2<")]); } #[test] @@ -442,10 +476,7 @@ mod variable_substitution_tests { KEY=VALUE KEY1=$KEY$KEY "#, - vec![ - ("KEY", "VALUE"), - ("KEY1", "VALUEVALUE"), - ], + vec![("KEY", "VALUE"), ("KEY1", "VALUEVALUE")], ); } @@ -455,13 +486,10 @@ mod variable_substitution_tests { r#" KEY.Value=VALUE "#, - vec![ - ("KEY.Value", "VALUE"), - ], + vec![("KEY.Value", "VALUE")], ); } - #[test] fn recursive_substitution() { assert_parsed_string( @@ -469,10 +497,7 @@ mod variable_substitution_tests { KEY=${KEY1}+KEY_VALUE KEY1=${KEY}+KEY1_VALUE "#, - vec![ - ("KEY", "+KEY_VALUE"), - ("KEY1", "+KEY_VALUE+KEY1_VALUE"), - ], + vec![("KEY", "+KEY_VALUE"), ("KEY1", "+KEY_VALUE+KEY1_VALUE")], ); } @@ -496,12 +521,7 @@ mod variable_substitution_tests { fn substitute_variable_from_env_variable() { std::env::set_var("KEY11", "test_user_env"); - assert_parsed_string( - r#"KEY=">${KEY11}<""#, - vec![ - ("KEY", ">test_user_env<"), - ], - ); + assert_parsed_string(r#"KEY=">${KEY11}<""#, vec![("KEY", ">test_user_env<")]); } #[test] @@ -513,10 +533,7 @@ mod variable_substitution_tests { KEY11=test_user KEY=">${KEY11}<" "#, - vec![ - ("KEY11", "test_user"), - ("KEY", ">test_user_env<"), - ], + vec![("KEY11", "test_user"), ("KEY", ">test_user_env<")], ); } @@ -543,10 +560,7 @@ mod variable_substitution_tests { KEY2=$KEY1_2 KEY=>${KEY1}<>${KEY2}< "#, - vec![ - ("KEY2", "_2"), - ("KEY", "><>_2<"), - ], + vec![("KEY2", "_2"), ("KEY", "><>_2<")], ); } } @@ -560,10 +574,17 @@ mod error_tests { fn should_not_parse_unfinished_substitutions() { let wrong_value = ">${KEY{<"; - let parsed_values: Vec<_> = Iter::new(format!(r#" + let parsed_values: Vec<_> = Iter::new( + format!( + r#" KEY=VALUE KEY1={} - "#, wrong_value).as_bytes()).collect(); + "#, + wrong_value + ) + .as_bytes(), + ) + .collect(); assert_eq!(parsed_values.len(), 2); @@ -615,7 +636,8 @@ mod error_tests { #[test] fn should_not_parse_illegal_escape() { let wrong_escape = r">\f<"; - let parsed_values: Vec<_> = Iter::new(format!("VALUE={}", wrong_escape).as_bytes()).collect(); + let parsed_values: Vec<_> = + Iter::new(format!("VALUE={}", wrong_escape).as_bytes()).collect(); assert_eq!(parsed_values.len(), 1); diff --git a/dotenv/tests/common/mod.rs b/dotenv/tests/common/mod.rs index 6bdbb8e..26457b5 100644 --- a/dotenv/tests/common/mod.rs +++ b/dotenv/tests/common/mod.rs @@ -1,6 +1,6 @@ -use std::{env, io}; use std::fs::File; use std::io::prelude::*; +use std::{env, io}; use tempfile::{tempdir, TempDir}; pub fn tempdir_with_dotenv(dotenv_text: &str) -> io::Result { @@ -14,6 +14,5 @@ pub fn tempdir_with_dotenv(dotenv_text: &str) -> io::Result { } pub fn make_test_dotenv() -> io::Result { - tempdir_with_dotenv("TESTKEY=test_val") + tempdir_with_dotenv("TESTKEY=test_val") } - diff --git a/dotenv/tests/test-child-dir.rs b/dotenv/tests/test-child-dir.rs index ca1cf06..7c2b61e 100644 --- a/dotenv/tests/test-child-dir.rs +++ b/dotenv/tests/test-child-dir.rs @@ -1,7 +1,7 @@ mod common; -use std::{env, fs}; use dotenv::*; +use std::{env, fs}; use crate::common::*; diff --git a/dotenv/tests/test-default-location.rs b/dotenv/tests/test-default-location.rs index 147b012..effd973 100644 --- a/dotenv/tests/test-default-location.rs +++ b/dotenv/tests/test-default-location.rs @@ -1,7 +1,7 @@ mod common; -use std::env; use dotenv::*; +use std::env; use crate::common::*; diff --git a/dotenv/tests/test-dotenv-iter.rs b/dotenv/tests/test-dotenv-iter.rs index 2892303..ec8c120 100644 --- a/dotenv/tests/test-dotenv-iter.rs +++ b/dotenv/tests/test-dotenv-iter.rs @@ -1,7 +1,7 @@ mod common; -use std::env; use dotenv::*; +use std::env; use crate::common::*; diff --git a/dotenv/tests/test-from-filename-iter.rs b/dotenv/tests/test-from-filename-iter.rs index 97f8c25..1d3ccbb 100644 --- a/dotenv/tests/test-from-filename-iter.rs +++ b/dotenv/tests/test-from-filename-iter.rs @@ -1,7 +1,7 @@ mod common; -use std::env; use dotenv::*; +use std::env; use crate::common::*; diff --git a/dotenv/tests/test-from-filename.rs b/dotenv/tests/test-from-filename.rs index bdcf49e..87dd74b 100644 --- a/dotenv/tests/test-from-filename.rs +++ b/dotenv/tests/test-from-filename.rs @@ -1,7 +1,7 @@ mod common; -use std::env; use dotenv::*; +use std::env; use crate::common::*; diff --git a/dotenv/tests/test-from-path-iter.rs b/dotenv/tests/test-from-path-iter.rs index bdcb042..2c4b763 100644 --- a/dotenv/tests/test-from-path-iter.rs +++ b/dotenv/tests/test-from-path-iter.rs @@ -1,7 +1,7 @@ mod common; -use std::env; use dotenv::*; +use std::env; use crate::common::*; diff --git a/dotenv/tests/test-from-path.rs b/dotenv/tests/test-from-path.rs index e481f02..34aa80a 100644 --- a/dotenv/tests/test-from-path.rs +++ b/dotenv/tests/test-from-path.rs @@ -1,7 +1,7 @@ mod common; -use std::env; use dotenv::*; +use std::env; use crate::common::*; diff --git a/dotenv/tests/test-variable-substitution.rs b/dotenv/tests/test-variable-substitution.rs index 7860b97..a6f2e09 100644 --- a/dotenv/tests/test-variable-substitution.rs +++ b/dotenv/tests/test-variable-substitution.rs @@ -1,7 +1,7 @@ mod common; -use std::env; use dotenv::*; +use std::env; use crate::common::*; @@ -11,48 +11,57 @@ fn test_variable_substitutions() { std::env::set_var("KEY1", "value1"); let substitutions_to_test = [ - "$ZZZ", - "$KEY", - "$KEY1", - "${KEY}1", - "$KEY_U", - "${KEY_U}", - "\\$KEY" + "$ZZZ", "$KEY", "$KEY1", "${KEY}1", "$KEY_U", "${KEY_U}", "\\$KEY", ]; let common_string = substitutions_to_test.join(">>"); - let dir = tempdir_with_dotenv(&format!(r#" + let dir = tempdir_with_dotenv(&format!( + r#" KEY1=new_value1 KEY_U=$KEY+valueU SUBSTITUTION_FOR_STRONG_QUOTES='{}' SUBSTITUTION_FOR_WEAK_QUOTES="{}" SUBSTITUTION_WITHOUT_QUOTES={} -"#, common_string, common_string, common_string)).unwrap(); +"#, + common_string, common_string, common_string + )) + .unwrap(); assert_eq!(var("KEY").unwrap(), "value"); assert_eq!(var("KEY1").unwrap(), "value1"); assert_eq!(var("KEY_U").unwrap(), "value+valueU"); - assert_eq!(var("SUBSTITUTION_FOR_STRONG_QUOTES").unwrap(), common_string); - assert_eq!(var("SUBSTITUTION_FOR_WEAK_QUOTES").unwrap(), [ - "", - "value", - "value1", - "value1", - "value_U", - "value+valueU", - "$KEY" - ].join(">>")); - assert_eq!(var("SUBSTITUTION_WITHOUT_QUOTES").unwrap(), [ - "", - "value", - "value1", - "value1", - "value_U", - "value+valueU", - "$KEY" - ].join(">>")); + assert_eq!( + var("SUBSTITUTION_FOR_STRONG_QUOTES").unwrap(), + common_string + ); + assert_eq!( + var("SUBSTITUTION_FOR_WEAK_QUOTES").unwrap(), + [ + "", + "value", + "value1", + "value1", + "value_U", + "value+valueU", + "$KEY" + ] + .join(">>") + ); + assert_eq!( + var("SUBSTITUTION_WITHOUT_QUOTES").unwrap(), + [ + "", + "value", + "value1", + "value1", + "value_U", + "value+valueU", + "$KEY" + ] + .join(">>") + ); env::set_current_dir(dir.path().parent().unwrap()).unwrap(); dir.close().unwrap(); -} \ No newline at end of file +} diff --git a/dotenv/tests/test-vars.rs b/dotenv/tests/test-vars.rs index d7b23a3..a80995c 100644 --- a/dotenv/tests/test-vars.rs +++ b/dotenv/tests/test-vars.rs @@ -12,7 +12,7 @@ fn test_vars() { let dir = make_test_dotenv().unwrap(); let vars: HashMap = vars().collect(); - + assert_eq!(vars["TESTKEY"], "test_val"); env::set_current_dir(dir.path().parent().unwrap()).unwrap(); diff --git a/dotenv_codegen/src/lib.rs b/dotenv_codegen/src/lib.rs index 0041413..c7588d4 100644 --- a/dotenv_codegen/src/lib.rs +++ b/dotenv_codegen/src/lib.rs @@ -1,4 +1,4 @@ use proc_macro_hack::proc_macro_hack; #[proc_macro_hack] -pub use dotenv_codegen_implementation::dotenv; \ No newline at end of file +pub use dotenv_codegen_implementation::dotenv; diff --git a/dotenv_codegen_implementation/src/lib.rs b/dotenv_codegen_implementation/src/lib.rs index f044b4a..50a6cc2 100644 --- a/dotenv_codegen_implementation/src/lib.rs +++ b/dotenv_codegen_implementation/src/lib.rs @@ -1,6 +1,6 @@ extern crate proc_macro; -use std::env; +use std::env::{self, VarError}; use proc_macro::TokenStream; use proc_macro_hack::proc_macro_hack; @@ -20,8 +20,8 @@ pub fn dotenv(input: TokenStream) -> TokenStream { } fn expand_env(input_raw: TokenStream) -> TokenStream { - - let args = >::parse_terminated.parse(input_raw) + let args = >::parse_terminated + .parse(input_raw) .expect("expected macro to be called with a comma-separated list of string literals"); let mut iter = args.iter(); @@ -33,7 +33,7 @@ fn expand_env(input_raw: TokenStream) -> TokenStream { let err_msg = match iter.next() { Some(lit) => lit.value(), - None => format!("environment variable `{}` not defined", var_name).into(), + None => format!("environment variable `{}` not defined", var_name), }; if iter.next().is_some() { @@ -42,6 +42,6 @@ fn expand_env(input_raw: TokenStream) -> TokenStream { match env::var(var_name) { Ok(val) => quote!(#val).into(), - Err(_) => panic!("{}", err_msg), + Err(VarError::NotPresent) | Err(VarError::NotUnicode(_)) => panic!("{}", err_msg), } }