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.
This commit is contained in:
Ian Rettig
2025-06-09 10:58:24 -04:00
parent 5c396c8adc
commit aac29186bf
+4 -5
View File
@@ -1,3 +1,4 @@
use anyhow::{Context, Result};
use clap::Parser; use clap::Parser;
// Search for a pattern in a file and display the lines that contain it. // Search for a pattern in a file and display the lines that contain it.
@@ -7,16 +8,14 @@ struct Cli {
path: std::path::PathBuf, path: std::path::PathBuf,
} }
#[derive(Debug)] fn main() -> Result<()> {
struct CustomError(String);
fn main() -> Result<(), CustomError> {
let args = Cli::parse(); 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).expect("Could not read file.");
let content = std::fs::read_to_string(&args.path) 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() { for line in content.lines() {
if line.contains(&args.pattern) { if line.contains(&args.pattern) {