Compare commits

..
Author SHA1 Message Date
Ian Rettig bbf4da2859 Feat: Add test case for find_matches
Slight find_matches rewrite to use Write trait.
2025-06-09 11:58:43 -04:00
Ian Rettig 7cca8410e1 Feat: extract find_matches into own function. 2025-06-09 11:47:23 -04:00
Ian Rettig ec63f6f022 Feat: Add small test case for practice. 2025-06-09 11:43:17 -04:00
Ian Rettig 0117b99006 Feat: add in logger. 2025-06-09 11:36:59 -04:00
Ian Rettig 6af058de3b Add: env_logger crate. 2025-06-09 11:32:54 -04:00
Ian Rettig 9f094d57b2 Add: log crate. 2025-06-09 11:31:24 -04:00
Ian Rettig aac29186bf Feat: use anyhow for error reporting.
I don't think that the error will properly be reported due to only printing
lines that match the pattern.
2025-06-09 10:58:24 -04:00
Ian Rettig 5c396c8adc Add: anyhow package. 2025-06-09 09:57:33 -04:00
Ian Rettig f9418783d4 Feat: Add custom error message. 2025-06-06 17:50:44 -04:00
Ian Rettig d7d2c74b23 Feat: question mark unwrap. 2025-06-06 17:02:32 -04:00
Ian Rettig d41577ef7d Feat: return error in unwrap. 2025-06-06 17:00:21 -04:00
Ian Rettig 3ff1454fb6 Feat: Use unwrapping for error handling. 2025-06-06 16:55:57 -04:00
Ian Rettig a663f2b4d8 Feat: set up manual error handling. 2025-06-06 14:46:21 -04:00
3 changed files with 183 additions and 12 deletions
Generated
+141
View File
@@ -2,6 +2,15 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]]
name = "anstream"
version = "0.6.19"
@@ -52,6 +61,12 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "anyhow"
version = "1.0.98"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487"
[[package]]
name = "clap"
version = "4.5.39"
@@ -98,11 +113,37 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
[[package]]
name = "env_filter"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "186e05a59d4c50738528153b83b0b0194d3a29507dfec16eccd4b342903397d0"
dependencies = [
"log",
"regex",
]
[[package]]
name = "env_logger"
version = "0.11.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13c863f0904021b108aa8b2f55046443e6b1ebde8fd4a15c399893aae4fa069f"
dependencies = [
"anstream",
"anstyle",
"env_filter",
"jiff",
"log",
]
[[package]]
name = "grrs"
version = "0.1.0"
dependencies = [
"anyhow",
"clap",
"env_logger",
"log",
]
[[package]]
@@ -117,12 +158,63 @@ version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "jiff"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a194df1107f33c79f4f93d02c80798520551949d59dfad22b6157048a88cca93"
dependencies = [
"jiff-static",
"log",
"portable-atomic",
"portable-atomic-util",
"serde",
]
[[package]]
name = "jiff-static"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c6e1db7ed32c6c71b759497fae34bf7933636f75a251b9e736555da426f6442"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "log"
version = "0.4.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94"
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "once_cell_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad"
[[package]]
name = "portable-atomic"
version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f84267b20a16ea918e43c6a88433c2d54fa145c92a811b5b047ccbe153674483"
[[package]]
name = "portable-atomic-util"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8a2f0d8d040d7848a709caf78912debcc3f33ee4b3cac47d73d1e1069e83507"
dependencies = [
"portable-atomic",
]
[[package]]
name = "proc-macro2"
version = "1.0.95"
@@ -141,6 +233,55 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "regex"
version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]]
name = "serde"
version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "strsim"
version = "0.11.1"
+3
View File
@@ -4,4 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1.0.98"
clap = { version = "4.5.39", features = ["derive"] }
env_logger = "0.11.8"
log = "0.4.27"
+39 -12
View File
@@ -1,6 +1,6 @@
use anyhow::{Context, Result};
use clap::Parser;
use std::fs::File;
use std::io::{BufRead, BufReader};
use log::info;
// Search for a pattern in a file and display the lines that contain it.
#[derive(Parser)]
@@ -9,19 +9,46 @@ struct Cli {
path: std::path::PathBuf,
}
fn main() -> std::io::Result<()> {
fn main() -> Result<()> {
env_logger::init();
info!("starting up");
let args = Cli::parse();
let f = File::open(&args.path)?;
let mut reader = BufReader::new(f);
let mut line = String::new();
// let content = std::fs::read_to_string(&args.path).expect("Could not read file.");
while reader.read_line(&mut line)? > 0 {
if line.contains(&args.pattern) {
println!("{}", line)
}
line.clear();
}
let content = std::fs::read_to_string(&args.path)
.with_context(|| format!("could not read file `{}`.", args.path.display()))?;
find_matches(
content.as_str(),
args.pattern.as_str(),
&mut std::io::stdout(),
);
Ok(())
}
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);
}
}
}
#[test]
fn find_a_match() {
let mut result = Vec::new();
find_matches("lorem ipsum\ndolar sit amet", "lorem", &mut result);
assert_eq!(result, b"lorem ipsum\n")
}
#[allow(dead_code)]
fn answer() -> i32 {
return 42;
}
#[test]
fn check_answer_validity() {
assert_eq!(answer(), 42);
}