From aac29186bfd036f878c6e103734b39ec227cc134 Mon Sep 17 00:00:00 2001 From: Ian Rettig Date: Mon, 9 Jun 2025 10:58:24 -0400 Subject: [PATCH] Feat: use anyhow for error reporting. I don't think that the error will properly be reported due to only printing lines that match the pattern. --- src/main.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3ac441b..b962121 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use anyhow::{Context, Result}; use clap::Parser; // Search for a pattern in a file and display the lines that contain it. @@ -7,16 +8,14 @@ struct Cli { path: std::path::PathBuf, } -#[derive(Debug)] -struct CustomError(String); - -fn main() -> Result<(), CustomError> { +fn main() -> Result<()> { let args = Cli::parse(); // let content = std::fs::read_to_string(&args.path).expect("Could not read file."); let content = std::fs::read_to_string(&args.path) - .map_err(|err| CustomError(format!("Error reading `{}`: {}", args.path.display(), err)))?; + .with_context(|| format!("could not read file `{}`.", args.path.display()))?; + for line in content.lines() { if line.contains(&args.pattern) {