Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
54 changes: 54 additions & 0 deletions src/env_validator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use regex::Regex;
use std::collections::HashSet;
use std::collections::HashMap;
use std::fs;
use std::io;



pub fn extract_env_variables(source: &str) -> Vec<String> {
Expand Down Expand Up @@ -29,4 +33,54 @@ pub fn extract_env_variables(source: &str) -> Vec<String> {
let mut result: Vec<String> = vars.into_iter().collect();
result.sort();
result
}

pub fn parse_env_file(path: &str) -> io::Result<HashMap<String, String>> {
let content = fs::read_to_string(path)?;
let mut vars = HashMap::new();

for line in content.lines() {
let line = line.trim();

if line.is_empty() || line.starts_with('#') {
continue;
}

if let Some((key, value)) = line.split_once('=') {
vars.insert(
key.trim().to_string(),
value.trim().to_string(),
);
}
Comment on lines +42 to +54

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Reject malformed env records instead of skipping them.

Any non-empty, non-comment line without = is silently dropped here, and =value is accepted with an empty key. That turns env-file typos into missing configuration while still returning Ok(...).

Suggested fix
-    for line in content.lines() {
+    for (line_no, line) in content.lines().enumerate() {
         let line = line.trim();

         if line.is_empty() || line.starts_with('#') {
             continue;
         }

-        if let Some((key, value)) = line.split_once('=') {
-            vars.insert(
-                key.trim().to_string(),
-                value.trim().to_string(),
-            );
-        }
+        let (key, value) = line.split_once('=').ok_or_else(|| {
+            io::Error::new(
+                io::ErrorKind::InvalidData,
+                format!("invalid env entry on line {}: missing '='", line_no + 1),
+            )
+        })?;
+
+        let key = key.trim();
+        if key.is_empty() {
+            return Err(io::Error::new(
+                io::ErrorKind::InvalidData,
+                format!("invalid env entry on line {}: empty key", line_no + 1),
+            ));
+        }
+
+        vars.insert(key.to_string(), value.trim().to_string());
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
for line in content.lines() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}
if let Some((key, value)) = line.split_once('=') {
vars.insert(
key.trim().to_string(),
value.trim().to_string(),
);
}
for (line_no, line) in content.lines().enumerate() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}
let (key, value) = line.split_once('=').ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("invalid env entry on line {}: missing '='", line_no + 1),
)
})?;
let key = key.trim();
if key.is_empty() {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("invalid env entry on line {}: empty key", line_no + 1),
));
}
vars.insert(key.to_string(), value.trim().to_string());
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/env_validator.rs` around lines 41 - 53, In env_validator.rs, update the
line parsing logic in the env-file reader so malformed records are rejected
instead of ignored: the current loop in the env parsing function silently skips
non-empty, non-comment lines without an '=' and accepts entries with an empty
key. Add validation around the existing split_once('=') handling so only
non-empty keys are inserted, and return an error for any malformed line rather
than continuing with Ok(...). Use the existing env-file parsing path and the
vars insert logic as the place to enforce this.

}

Ok(vars)
}

pub fn validate_env(
extracted: &[String],
env: &HashMap<String, String>,
) {
println!("Validation Report");

println!("\nMissing Variables:");
for var in extracted {
if !env.contains_key(var) {
println!("- {}", var);
}
}

println!("\nUnused Variables:");
for key in env.keys() {
if !extracted.contains(key) {
println!("- {}", key);
}
}

println!("\nEmpty Variables:");
for (key, value) in env {
if value.trim().is_empty() {
println!("- {}", key);
}
Comment on lines +73 to +84

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Sort report entries before printing.

Lines 74-83 iterate HashMap directly, so the "Unused Variables" and "Empty Variables" sections come out in nondeterministic order. That makes the CLI output noisy and brittle to assert in tests.

Suggested fix
     println!("\nUnused Variables:");
-    for key in env.keys() {
-        if !extracted.contains(key) {
-            println!("- {}", key);
-        }
-    }
+    let mut unused: Vec<_> = env
+        .keys()
+        .filter(|key| !extracted.contains(*key))
+        .cloned()
+        .collect();
+    unused.sort();
+    for key in unused {
+        println!("- {}", key);
+    }

     println!("\nEmpty Variables:");
-    for (key, value) in env {
-        if value.trim().is_empty() {
-            println!("- {}", key);
-        }
-    }
+    let mut empty: Vec<_> = env
+        .iter()
+        .filter(|(_, value)| value.trim().is_empty())
+        .map(|(key, _)| key.clone())
+        .collect();
+    empty.sort();
+    for key in empty {
+        println!("- {}", key);
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
println!("\nUnused Variables:");
for key in env.keys() {
if !extracted.contains(key) {
println!("- {}", key);
}
}
println!("\nEmpty Variables:");
for (key, value) in env {
if value.trim().is_empty() {
println!("- {}", key);
}
println!("\nUnused Variables:");
let mut unused: Vec<_> = env
.keys()
.filter(|key| !extracted.contains(*key))
.cloned()
.collect();
unused.sort();
for key in unused {
println!("- {}", key);
}
println!("\nEmpty Variables:");
let mut empty: Vec<_> = env
.iter()
.filter(|(_, value)| value.trim().is_empty())
.map(|(key, _)| key.clone())
.collect();
empty.sort();
for key in empty {
println!("- {}", key);
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/env_validator.rs` around lines 73 - 84, The report output in
`env_validator.rs` is currently iterating `HashMap` entries directly in the
“Unused Variables” and “Empty Variables” sections, which makes the printed order
nondeterministic. Update the report generation logic around the existing `env`
iteration to collect the matching keys first, sort them, and then print them in
a stable order before emitting each section so `println!` output is
deterministic and test-friendly.

}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod env_validator;

fn main() {
println!("issue 4!!!");
println!("issue 4/5/6 solution");
}