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) {