From 22b3c1ed18e24bab9a508d4bf550964de86c26c5 Mon Sep 17 00:00:00 2001 From: SangWon Date: Fri, 11 Oct 2024 01:32:57 +0900 Subject: [PATCH] #1 feature/dotenv_values feat: add dotenv_values method This function is similar to the existing dotenv function, but instead of setting environment variables, it returns the contents of the .env file as a HashMap. This allows users to handle the contents of the .env file more flexibly. --- dotenv/src/lib.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/dotenv/src/lib.rs b/dotenv/src/lib.rs index f2b1b0a..6d58314 100644 --- a/dotenv/src/lib.rs +++ b/dotenv/src/lib.rs @@ -179,3 +179,24 @@ pub fn dotenv_iter() -> Result> { let (_, iter) = Finder::new().find()?; Ok(iter) } + + +/// Like `dotenv`, but returns an HashMap over variables instead of loading into environment. +/// Unlike 'dotenv().ok()', this function only retrieves variables from the .env file, without loading them into the environment. +/// # Examples +/// ```no_run +/// use dotenv; +/// +/// let dotenv_values: std::collections::HashMap = dotenv::dotenv_values().unwrap(); +/// dotenv_values.iter() +/// .for_each(|item| println!("{:?}", item)); +/// +/// ``` +pub fn dotenv_values() -> Result> { + let vars = dotenv_iter()? + .filter_map(|item| item.ok()) + .collect::>(); + + Ok(vars) +} +