diff options
Diffstat (limited to 'rust-cli/grrs/src')
| -rw-r--r-- | rust-cli/grrs/src/lib.rs | 13 | ||||
| -rw-r--r-- | rust-cli/grrs/src/main.rs | 23 |
2 files changed, 28 insertions, 8 deletions
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); } } |
