summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2023-03-01 17:59:20 +0400
committerJoursoir <chat@joursoir.net>2023-03-01 17:59:20 +0400
commit8e40b9ae042b715785004678b1bb3d3a4fca9838 (patch)
treec63b12a27bbfa771c066efad3ef769f533d918bd /src
downloadtrgrep-8e40b9ae042b715785004678b1bb3d3a4fca9838.tar.gz
trgrep-8e40b9ae042b715785004678b1bb3d3a4fca9838.tar.bz2
trgrep-8e40b9ae042b715785004678b1bb3d3a4fca9838.zip
initial commit
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs6
-rw-r--r--src/main.rs33
2 files changed, 39 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..c01823d
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,6 @@
+pub fn search<'a>(pattern: &str, contents: &'a str) -> Vec<&'a str> {
+ contents
+ .lines()
+ .filter(|line| line.contains(pattern))
+ .collect()
+}
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..76b2543
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,33 @@
+use std::error::Error;
+use std::process;
+use std::fs;
+
+use clap::Parser;
+
+#[derive(Parser)]
+#[command(author, version, about, long_about = None)] // Read from `Cargo.toml`
+struct Config {
+ /// The pattern to look for
+ pattern: String,
+ /// The path to the file to read
+ file: String,
+}
+
+fn main() {
+ let config = Config::parse();
+
+ if let Err(e) = run(config) {
+ eprintln!("Error: {e}");
+ process::exit(1);
+ }
+}
+
+fn run(config: Config) -> Result<(), Box<dyn Error>> {
+ let contents = fs::read_to_string(config.file)?;
+
+ for line in trgrep::search(&config.pattern, &contents) {
+ println!("{line}");
+ }
+
+ Ok(())
+}