fix read_lines to better handle errors

This commit is contained in:
Mohammad Kanaan 2026-07-27 21:29:57 +03:00
parent 0f57d037e0
commit a1c13d4342

View file

@ -39,16 +39,8 @@ pub fn process_files(
pub fn read_lines(
file_path: &Path,
) -> anyhow::Result<impl Iterator<Item = std::io::Result<String>>, anyhow::Error> {
match file_path.try_exists() {
Ok(true) => {}
Ok(false) => return Err(anyhow::anyhow!("File path needs to exist")),
Err(e) => eprintln!("Error checking path: {e}"),
}
let path_string = file_path.to_string_lossy().to_string();
let file =
File::open(file_path).with_context(|| format!("failed to open file: {path_string}"))?;
let file = File::open(file_path)
.with_context(|| format!("failed to open file: {}", file_path.display()))?;
let reader = BufReader::new(file);
Ok(reader.lines())