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:
+4
-5
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user