Closed acgetchell closed 11 months ago
This works:
#![allow(unused)]
use anyhow::Context;
use clap::Parser;
use std::error::Error;
pub type Result<T, E = dyn Error> = std::result::Result<T, E>;
/// 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 find_matches(content: &str, pattern: &str, mut writer: impl std::io::Write) {
for line in content.lines() {
if line.contains(pattern) {
writeln!(writer, "{}", line);
}
}
}
fn main() -> Result<(), Box<dyn Error>> {
let args = Cli::parse();
let content = std::fs::read_to_string(&args.path)
.with_context(|| format!("could not read file `{}`", args.path.display()))?;
find_matches(&content, &args.pattern, &mut std::io::stdout());
Ok(())
}
#[test]
fn find_a_match() {
let mut result = Vec::new();
find_matches("lorem ipsum\ndolor sit amet", "lorem", &mut result);
assert_eq!(result, b"lorem ipsum\n");
}
The file is at https://github.com/rust-cli/book/blob/master/src/tutorial/testing/src/main.rs and is tested.
The important bit is:
Here’s an example of a main function that builds on what we’ve seen in the previous chapters and uses our extracted find_matches function
For what its referring to, see this main.
This snippet:
Produces the following: