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;
|
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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user