From 82b69a232f6906deaa671176b36de523ca7847fb Mon Sep 17 00:00:00 2001 From: Louis <contact@louiscap.co> Date: Sun, 5 Jan 2025 00:08:40 +0000 Subject: [PATCH] Fmt & Lint --- rustfmt.toml | 4 ++++ src/env_file.rs | 34 +++++++++++++---------------- src/filesystem.rs | 5 ++--- src/lib.rs | 4 ++-- tests/integration.rs | 52 ++++++++++++++++++++++---------------------- 5 files changed, 49 insertions(+), 50 deletions(-) create mode 100644 rustfmt.toml diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..aff2eb2 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,4 @@ +hard_tabs = true +reorder_imports = true +max_width = 140 +merge_derives = true \ No newline at end of file diff --git a/src/env_file.rs b/src/env_file.rs index 6111cb9..b8d44ba 100644 --- a/src/env_file.rs +++ b/src/env_file.rs @@ -1,8 +1,8 @@ +use crate::parser::{file, FileLine}; +use nom_locate::LocatedSpan; use std::env; use std::env::VarError; use std::fmt::Display; -use crate::parser::{file, FileLine}; -use nom_locate::LocatedSpan; use std::str::FromStr; use std::string::ParseError; @@ -114,9 +114,9 @@ pub struct ApplyOptions { impl ApplyOptions { pub fn new(prefix: impl Display, overwrite: bool) -> Self { Self { - prefix: Some(prefix.to_string()), - overwrite, - } + prefix: Some(prefix.to_string()), + overwrite, + } } pub fn with_prefix(prefix: impl Display) -> Self { @@ -124,10 +124,7 @@ impl ApplyOptions { } pub fn with_overwrite(overwrite: bool) -> Self { - Self { - prefix: None, - overwrite, - } + Self { prefix: None, overwrite } } } @@ -141,20 +138,20 @@ impl ApplyEnvironmentFile for EnvironmentFile { } } -impl <E> ApplyEnvironmentFile for Result<EnvironmentFile, E> { +impl<E> ApplyEnvironmentFile for Result<EnvironmentFile, E> { fn apply(&self, options: ApplyOptions) { - if let Ok(file) = self { - file.apply(options); - } - } + if let Ok(file) = self { + file.apply(options); + } + } } impl ApplyEnvironmentFile for Option<EnvironmentFile> { fn apply(&self, options: ApplyOptions) { - if let Some(file) = self { - file.apply(options); - } - } + if let Some(file) = self { + file.apply(options); + } + } } pub struct EnvFileIterator<'a> { @@ -251,7 +248,6 @@ fn set_from_file(file: &EnvironmentFile, options: ApplyOptions) -> Result<(), En Ok(()) } - #[cfg(test)] mod tests { use super::*; diff --git a/src/filesystem.rs b/src/filesystem.rs index 7f17af6..55aded5 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -1,9 +1,9 @@ -use crate::{EnvironmentFile}; +use crate::env_file::{ApplyEnvironmentFile, ApplyOptions}; +use crate::EnvironmentFile; use std::fmt::Display; use std::fs::File; use std::io::Read; use std::path::Path; -use crate::env_file::{ApplyEnvironmentFile, ApplyOptions}; #[derive(Debug, thiserror::Error)] #[allow(clippy::enum_variant_names)] @@ -49,4 +49,3 @@ pub fn dotenv_from(path: impl AsRef<Path>) -> Result<(), EnvFsError> { env_file_from_path(path)?.apply(Default::default()); Ok(()) } - diff --git a/src/lib.rs b/src/lib.rs index 72fc2a5..fa36368 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,8 +5,8 @@ mod env_file; mod filesystem; mod parser; -pub use env_file::{EnvironmentFile, EnvironmentFileError, ApplyEnvironmentFile, ApplyOptions}; +pub use env_file::{ApplyEnvironmentFile, ApplyOptions, EnvironmentFile, EnvironmentFileError}; pub use parser::{FileLine, ValuePart}; #[cfg(feature = "fs")] -pub use filesystem::{dotenv, dotenv_from, dotenv_suffix, dotenv_opts}; +pub use filesystem::{dotenv, dotenv_from, dotenv_opts, dotenv_suffix}; diff --git a/tests/integration.rs b/tests/integration.rs index 4161db4..1d3b9b9 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -2,7 +2,7 @@ use envish::{ApplyEnvironmentFile, ApplyOptions, EnvironmentFile}; #[test] fn it_parses_basic_dotenv_file() { - let file_contents = r#" + let file_contents = r#" # This value won't be set in the test, and this comment will be ignored MY_BEST_VARIABLE=some_value # This variable is also not defined, and it'll still be a string, @@ -10,19 +10,19 @@ fn it_parses_basic_dotenv_file() { SOME_OTHER_VARIABLE=1234 "#; - std::env::var("MY_BEST_VARIABLE").expect_err("MY_BEST_VARIABLE should not be set"); - std::env::var("SOME_OTHER_VARIABLE").expect_err("SOME_OTHER_VARIABLE should not be set"); + std::env::var("MY_BEST_VARIABLE").expect_err("MY_BEST_VARIABLE should not be set"); + std::env::var("SOME_OTHER_VARIABLE").expect_err("SOME_OTHER_VARIABLE should not be set"); - let file = EnvironmentFile::parse(file_contents).expect("Failed to parse environment file"); - file.apply(Default::default()); + let file = EnvironmentFile::parse(file_contents).expect("Failed to parse environment file"); + file.apply(Default::default()); - assert_eq!(std::env::var("MY_BEST_VARIABLE").unwrap(), "some_value"); - assert_eq!(std::env::var("SOME_OTHER_VARIABLE").unwrap(), "1234"); + assert_eq!(std::env::var("MY_BEST_VARIABLE").unwrap(), "some_value"); + assert_eq!(std::env::var("SOME_OTHER_VARIABLE").unwrap(), "1234"); } #[test] fn it_parses_dotenv_file_with_interpolation() { - let file_contents = r#" + let file_contents = r#" # This value won't be set in the test, and this comment will be ignored MY_BEST_VARIABLE=some_value # This variable is also not defined, and it'll still be a string, @@ -32,21 +32,21 @@ fn it_parses_dotenv_file_with_interpolation() { INTERPOLATED_VARIABLE=${SOME_OTHER_VARIABLE}567 "#; - std::env::var("MY_BEST_VARIABLE").expect_err("MY_BEST_VARIABLE should not be set"); - std::env::var("SOME_OTHER_VARIABLE").expect_err("SOME_OTHER_VARIABLE should not be set"); - std::env::var("INTERPOLATED_VARIABLE").expect_err("INTERPOLATED_VARIABLE should not be set"); + std::env::var("MY_BEST_VARIABLE").expect_err("MY_BEST_VARIABLE should not be set"); + std::env::var("SOME_OTHER_VARIABLE").expect_err("SOME_OTHER_VARIABLE should not be set"); + std::env::var("INTERPOLATED_VARIABLE").expect_err("INTERPOLATED_VARIABLE should not be set"); - let file = EnvironmentFile::parse(file_contents).expect("Failed to parse environment file"); - file.apply(Default::default()); + let file = EnvironmentFile::parse(file_contents).expect("Failed to parse environment file"); + file.apply(Default::default()); - assert_eq!(std::env::var("MY_BEST_VARIABLE").unwrap(), "some_value"); - assert_eq!(std::env::var("SOME_OTHER_VARIABLE").unwrap(), "1234"); - assert_eq!(std::env::var("INTERPOLATED_VARIABLE").unwrap(), "1234567"); + assert_eq!(std::env::var("MY_BEST_VARIABLE").unwrap(), "some_value"); + assert_eq!(std::env::var("SOME_OTHER_VARIABLE").unwrap(), "1234"); + assert_eq!(std::env::var("INTERPOLATED_VARIABLE").unwrap(), "1234567"); } #[test] fn it_parses_dotenv_file_with_interpolation_and_prefix_option() { - let file_contents = r#" + let file_contents = r#" # This value won't be set in the test, and this comment will be ignored MY_BEST_VARIABLE=some_value # This variable is also not defined, and it'll still be a string, @@ -56,14 +56,14 @@ fn it_parses_dotenv_file_with_interpolation_and_prefix_option() { INTERPOLATED_VARIABLE=${SOME_OTHER_VARIABLE}567 "#; - std::env::var("MY_BEST_VARIABLE").expect_err("MY_BEST_VARIABLE should not be set"); - std::env::var("SOME_OTHER_VARIABLE").expect_err("SOME_OTHER_VARIABLE should not be set"); - std::env::var("INTERPOLATED_VARIABLE").expect_err("INTERPOLATED_VARIABLE should not be set"); + std::env::var("MY_BEST_VARIABLE").expect_err("MY_BEST_VARIABLE should not be set"); + std::env::var("SOME_OTHER_VARIABLE").expect_err("SOME_OTHER_VARIABLE should not be set"); + std::env::var("INTERPOLATED_VARIABLE").expect_err("INTERPOLATED_VARIABLE should not be set"); - let file = EnvironmentFile::parse(file_contents).expect("Failed to parse environment file"); - file.apply(ApplyOptions::with_prefix("APP_")); + let file = EnvironmentFile::parse(file_contents).expect("Failed to parse environment file"); + file.apply(ApplyOptions::with_prefix("APP_")); - assert_eq!(std::env::var("APP_MY_BEST_VARIABLE").unwrap(), "some_value"); - assert_eq!(std::env::var("APP_SOME_OTHER_VARIABLE").unwrap(), "1234"); - assert_eq!(std::env::var("APP_INTERPOLATED_VARIABLE").unwrap(), "1234567"); -} \ No newline at end of file + assert_eq!(std::env::var("APP_MY_BEST_VARIABLE").unwrap(), "some_value"); + assert_eq!(std::env::var("APP_SOME_OTHER_VARIABLE").unwrap(), "1234"); + assert_eq!(std::env::var("APP_INTERPOLATED_VARIABLE").unwrap(), "1234567"); +} -- GitLab