diff --git a/Cargo.lock b/Cargo.lock index 004def9..0d9233f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,6 +52,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "anyhow" +version = "1.0.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f" + [[package]] name = "clap" version = "4.5.35" @@ -102,6 +108,7 @@ checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" name = "grrs" version = "0.1.0" dependencies = [ + "anyhow", "clap", ] diff --git a/Cargo.toml b/Cargo.toml index e166a20..e60b7c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,5 @@ version = "0.1.0" edition = "2024" [dependencies] +anyhow = "1.0" clap = { version = "4.0", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index ada1e96..26b3c4f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use anyhow::{Context, Result}; use clap::Parser; #[derive(Parser)] @@ -6,13 +7,17 @@ struct Cli { path: std::path::PathBuf, } -#[derive(Debug)] -struct CustomError(String); +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); + } + } -fn main() -> Result<(), CustomError> { - let path = "test.txt"; - let content = std::fs::read_to_string(path) - .map_err(|err| CustomError(format!("Error reading `{}`: {}", path, err)))?; - println!("file content: {}", content); Ok(()) }