41 lines
829 B
Rust
41 lines
829 B
Rust
use anyhow::{Context, Result};
|
|
use clap::Parser;
|
|
use log::info;
|
|
|
|
// Search for a pattern in a file and display the lines that contain it.
|
|
#[derive(Parser)]
|
|
struct Cli {
|
|
pattern: String,
|
|
path: std::path::PathBuf,
|
|
}
|
|
|
|
fn main() -> Result<()> {
|
|
env_logger::init();
|
|
info!("starting up");
|
|
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)
|
|
.with_context(|| format!("could not read file `{}`.", args.path.display()))?;
|
|
|
|
|
|
for line in content.lines() {
|
|
if line.contains(&args.pattern) {
|
|
println!("{}", line)
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
fn answer() -> i32 {
|
|
return 42;
|
|
}
|
|
|
|
#[test]
|
|
fn check_answer_validity() {
|
|
assert_eq!(answer(), 42);
|
|
}
|