Skip to content

Commit

Permalink
Better Error reports (#3)
Browse files Browse the repository at this point in the history
* use anyhow for error context

* use error handling for no file path
  • Loading branch information
sananand007 authored Jan 7, 2024
1 parent eb64ad7 commit d88e0a6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ edition = "2021"

[dependencies]
clap = { version = "4.0", features = ["derive"]}
anyhow = { version = "1.0"}
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::Parser;
use std::collections::HashMap;
use std::io::{BufRead, BufReader};
use std::fs::File;
use anyhow::{Context, Result};


/// Search and logically process patterns on the Command line
Expand All @@ -19,16 +20,16 @@ struct Args {
_path: std::path::PathBuf,
}

fn main() -> Result<(), std::io::Error> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();
let f = File::open(&args._path).expect("Unable to open the file!");
let f = File::open(&args._path).map_err(|_| "the correct file path is not provided")?;
let mut br = BufReader::new(f);
let mut line = String::new();
let mut check_pattern = false;
let mut high_freq_wordmap: HashMap<String, i32> = HashMap::new();

loop {
let content = br.read_line(&mut line)?;
let content = br.read_line(&mut line).with_context(|| format!("could not read line"))?;
if content == 0 {
break;
}
Expand Down

0 comments on commit d88e0a6

Please sign in to comment.