summaryrefslogtreecommitdiffstats
path: root/rust-cli
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2022-12-21 15:56:24 +0100
committerivarlovlie <git@ivarlovlie.no>2022-12-21 15:56:24 +0100
commit0e1bf83811c4a9e4e2869190cfc94bf32476b9ee (patch)
treefd518a91cc92f756dc9b8959762f7553533ce60e /rust-cli
parent87ee550abea331430fc2db0f1d96d6acbfa6c8f2 (diff)
downloadlearning-rust-0e1bf83811c4a9e4e2869190cfc94bf32476b9ee.tar.xz
learning-rust-0e1bf83811c4a9e4e2869190cfc94bf32476b9ee.zip
12/21/22 15:56:24
Diffstat (limited to 'rust-cli')
-rw-r--r--rust-cli/grrs/Cargo.toml6
-rw-r--r--rust-cli/grrs/src/lib.rs13
-rw-r--r--rust-cli/grrs/src/main.rs23
-rw-r--r--rust-cli/grrs/tests/finds_correct_matches.rs4
-rw-r--r--rust-cli/grrs/tests/finds_matches.rs12
-rw-r--r--rust-cli/grrs/tests/test_utils.rs20
6 files changed, 69 insertions, 9 deletions
diff --git a/rust-cli/grrs/Cargo.toml b/rust-cli/grrs/Cargo.toml
index 08077b2..fc86a5f 100644
--- a/rust-cli/grrs/Cargo.toml
+++ b/rust-cli/grrs/Cargo.toml
@@ -6,4 +6,8 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
-clap = { version = "4.0", features = ["derive"] } \ No newline at end of file
+clap = { version = "4.0", features = ["derive"] }
+
+[dev-dependencies]
+assert_cmd = "2.0.7"
+predicates = "2.1.4" \ No newline at end of file
diff --git a/rust-cli/grrs/src/lib.rs b/rust-cli/grrs/src/lib.rs
new file mode 100644
index 0000000..f91064f
--- /dev/null
+++ b/rust-cli/grrs/src/lib.rs
@@ -0,0 +1,13 @@
+use std::{io::{BufReader, BufRead, Error}, fs::File};
+
+pub fn find_matches(file: &File, pattern: &str) -> Result<Vec<String>, Error> {
+ let reader = BufReader::new(file);
+ let mut result = Vec::<String>::new();
+ for line in reader.lines() {
+ let line_val = line.unwrap();
+ if line_val.contains(&pattern) {
+ result.push(line_val);
+ }
+ }
+ return Ok(result);
+} \ No newline at end of file
diff --git a/rust-cli/grrs/src/main.rs b/rust-cli/grrs/src/main.rs
index 09e2d3a..e5ed13b 100644
--- a/rust-cli/grrs/src/main.rs
+++ b/rust-cli/grrs/src/main.rs
@@ -1,5 +1,4 @@
-use std::io::{BufReader, BufRead};
-use std::fs::File;
+use std::fs::{File};
use::clap::Parser;
#[derive(Parser)]
@@ -8,14 +7,22 @@ struct Cli {
path: std::path::PathBuf,
}
-
fn main() {
let args = Cli::parse();
- //println!("pattern: {}, path: {}",args.pattern, args.path.display());
+ if !args.path.exists() {
+ eprintln!("The file at '{}' does not exist", &args.path.display());
+ return;
+ }
+ if args.pattern.is_empty() {
+ eprintln!("No pattern was supplied, see --help");
+ return;
+ }
let file = File::open(args.path).expect("could not read file");
- let mut reader = BufReader::new(file);
- for line in reader.lines() {
- if line.unwrap_or_default().contains(&args.pattern) {
- }
+ let matches = grrs::find_matches(&file, &args.pattern).unwrap();
+ if matches.len() < 1 {
+ return;
+ }
+ for line in matches {
+ println!("{}", line);
}
}
diff --git a/rust-cli/grrs/tests/finds_correct_matches.rs b/rust-cli/grrs/tests/finds_correct_matches.rs
new file mode 100644
index 0000000..db5228a
--- /dev/null
+++ b/rust-cli/grrs/tests/finds_correct_matches.rs
@@ -0,0 +1,4 @@
+#[test]
+fn finds_correct_matches() {
+
+} \ No newline at end of file
diff --git a/rust-cli/grrs/tests/finds_matches.rs b/rust-cli/grrs/tests/finds_matches.rs
new file mode 100644
index 0000000..5f301fc
--- /dev/null
+++ b/rust-cli/grrs/tests/finds_matches.rs
@@ -0,0 +1,12 @@
+use std::fs::File;
+
+#[test]
+fn finds_matches() {
+ let mut test_file = File::create("finds_matches.txt").unwrap();
+ std::io::Write::write_all(&mut test_file, b"asdf\ntesting").unwrap();
+ std::io::Write::flush(&mut test_file).unwrap();
+ let file_to_test = File::open("finds_matches.txt").expect("could not read test file");
+ let matches = grrs::find_matches(&file_to_test, "asdf").unwrap();
+ assert_eq!(matches.len(), 1);
+ std::fs::remove_file("finds_matches.txt").unwrap();
+} \ No newline at end of file
diff --git a/rust-cli/grrs/tests/test_utils.rs b/rust-cli/grrs/tests/test_utils.rs
new file mode 100644
index 0000000..d408e87
--- /dev/null
+++ b/rust-cli/grrs/tests/test_utils.rs
@@ -0,0 +1,20 @@
+use std::{fs::{File}, path::Path};
+
+pub fn get_test_file_with_contents(name: &str, contents:&str) -> &File {
+ let path = get_unused_filename();
+ return File::open(path);
+}
+
+fn get_unused_filename() -> String {
+ let files = std::fs::read_dir(Path::new(".")).unwrap();
+ let file_suffix = "-testing.txt";
+ let mut file_no = 1;
+ for file in files {
+ let dir = file.unwrap();
+ }
+
+ return file_no
+ .to_string()
+ .to_owned()
+ .push_str(&file_suffix);
+} \ No newline at end of file