-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
grss
implementation to errors chapter
At the end of the chapter, provide a running code example that applies the nicer error reporting to the `grrs` tool.
- Loading branch information
Showing
3 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use anyhow::{Context, Result}; | ||
use clap::Parser; | ||
|
||
/// Search for a pattern in a file and display the lines that contain it. | ||
#[derive(Parser)] | ||
struct Cli { | ||
/// The pattern to look for | ||
pattern: String, | ||
/// The path to the file to read | ||
path: std::path::PathBuf, | ||
} | ||
|
||
fn main() -> Result<()> { | ||
let args = Cli::parse(); | ||
|
||
let content = std::fs::read_to_string(&args.path) | ||
.with_context(|| format!("could not read file `{}`", args.path.display()))?; | ||
|
||
for line in content.lines() { | ||
if line.contains(&args.pattern) { | ||
println!("{}", line); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters