wrapping up

This commit is contained in:
Fabio Montefuscolo 2025-04-02 22:36:05 +02:00
parent cfc7b1266a
commit f438c88533
Signed by: fabiomontefuscolo
GPG key ID: 7598676DAE19B4EF
3 changed files with 20 additions and 7 deletions

7
Cargo.lock generated
View file

@ -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",
]

View file

@ -4,4 +4,5 @@ version = "0.1.0"
edition = "2024"
[dependencies]
anyhow = "1.0"
clap = { version = "4.0", features = ["derive"] }

View file

@ -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(())
}