feat: implement basic pattern matching
This commit is contained in:
parent
452483b1b3
commit
d58ae9ffbe
4 changed files with 60 additions and 6 deletions
45
Cargo.lock
generated
45
Cargo.lock
generated
|
|
@ -2,6 +2,15 @@
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aho-corasick"
|
||||||
|
version = "1.1.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anstream"
|
name = "anstream"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
|
|
@ -116,6 +125,12 @@ version = "1.70.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.8.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "once_cell_polyfill"
|
name = "once_cell_polyfill"
|
||||||
version = "1.70.2"
|
version = "1.70.2"
|
||||||
|
|
@ -140,12 +155,42 @@ dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex"
|
||||||
|
version = "1.13.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"memchr",
|
||||||
|
"regex-automata",
|
||||||
|
"regex-syntax",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-automata"
|
||||||
|
version = "0.4.16"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8fcfdb36bda0c880c5931cdc7a2bcdc8ba4556847b9d912bca70bc94708711ad"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"memchr",
|
||||||
|
"regex-syntax",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-syntax"
|
||||||
|
version = "0.8.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "smolgrep"
|
name = "smolgrep"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
|
"regex",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
||||||
|
|
@ -6,3 +6,4 @@ edition = "2024"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.104"
|
anyhow = "1.0.104"
|
||||||
clap = { version = "4.6.4", features = ["derive"] }
|
clap = { version = "4.6.4", features = ["derive"] }
|
||||||
|
regex = "1.13.1"
|
||||||
|
|
|
||||||
|
|
@ -11,10 +11,12 @@ fn main() -> anyhow::Result<()> {
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
|
|
||||||
let lines_iter = parser::read_lines(&cli.file_path)?;
|
let lines_iter = parser::read_lines(&cli.file_path)?;
|
||||||
parser::consume_lines(lines_iter);
|
let matches = parser::match_pattern(lines_iter, &cli.pattern);
|
||||||
|
|
||||||
println!("Pattern: {}", cli.pattern);
|
println!("Pattern: {}", cli.pattern);
|
||||||
println!("File path: {}", cli.file_path);
|
println!("File path: {}", cli.file_path);
|
||||||
|
println!("---");
|
||||||
|
println!("{:?}", matches);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
pub fn read_lines(
|
pub fn read_lines(
|
||||||
file_path: &str,
|
file_path: &str,
|
||||||
|
|
@ -19,9 +20,14 @@ pub fn read_lines(
|
||||||
Ok(reader.lines())
|
Ok(reader.lines())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn consume_lines(iter: impl Iterator<Item = std::io::Result<String>>) {
|
pub fn match_pattern(
|
||||||
for line in iter {
|
iter: impl Iterator<Item = std::io::Result<String>>,
|
||||||
let line = line.unwrap_or("".into());
|
pattern: &str,
|
||||||
println!("{line}");
|
) -> anyhow::Result<Vec<String>, anyhow::Error> {
|
||||||
}
|
let reg = Regex::new(pattern).with_context(|| format!("invalid regex: {:?}", pattern))?;
|
||||||
|
let lines = iter.collect::<std::io::Result<Vec<_>>>()?;
|
||||||
|
|
||||||
|
let matches = lines.into_iter().filter(|line| reg.is_match(line)).collect::<Vec<_>>();
|
||||||
|
|
||||||
|
Ok(matches)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue