diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..e3387b1 --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,23 @@ +use clap::Parser; +use std::path::PathBuf; + +#[derive(Parser)] +pub struct Cli { + pub(crate) pattern: String, + + #[arg(value_name = "FILES", num_args = 1..)] + pub(crate) file_paths: Vec, + + #[arg(short, long)] + pub(crate) ignore_case: bool, +} + +pub struct CliOptions { + pub ignore_case: bool, +} + +impl Cli { + pub fn get_options(&self) -> CliOptions { + CliOptions { ignore_case: self.ignore_case } + } +} diff --git a/src/main.rs b/src/main.rs index 2efcb61..0983088 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,11 @@ -use std::{path::PathBuf, process::ExitCode}; +use std::process::ExitCode; -use clap::Parser; - -use crate::{matches::Matches, parser::process_files}; +use crate::{cli::Cli, matches::Matches, parser::process_files}; +mod cli; mod matches; mod parser; -#[derive(Parser)] -struct Cli { - pattern: String, - - #[arg(value_name = "FILES", num_args = 1..)] - file_paths: Vec, - - #[arg(short, long)] - ignore_case: bool, -} +use clap::Parser; fn main() -> ExitCode { match run() { @@ -36,7 +26,7 @@ fn run() -> anyhow::Result { // println!("{:?} {}", cli.file_paths, cli.pattern); - let matches = process_files(&cli.file_paths, &cli.pattern, &cli)?; + let matches = process_files(&cli.file_paths, &cli.pattern, &cli.get_options())?; Ok(matches) } diff --git a/src/matches.rs b/src/matches.rs index f161e89..9d6093c 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -16,7 +16,7 @@ impl Matches { self.items.is_empty() } - pub fn append(&mut self, matches: &mut Self) -> () { + pub fn append(&mut self, matches: &mut Self) { self.items.append(&mut matches.items); } } diff --git a/src/parser.rs b/src/parser.rs index 83cd964..38ae372 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -8,17 +8,17 @@ use anyhow::Context; use regex::{Regex, RegexBuilder}; use crate::{ + cli::CliOptions, matches::{Match, Matches}, - Cli, }; pub fn process_files( file_paths: &Vec, pattern: &str, - cli: &Cli, + cli_options: &CliOptions, ) -> anyhow::Result { let reg = RegexBuilder::new(pattern) - .case_insensitive(cli.ignore_case) + .case_insensitive(cli_options.ignore_case) .build() .with_context(|| format!("invalid regex: {:?}", pattern))?; @@ -26,7 +26,7 @@ pub fn process_files( for file_path in file_paths { let lines_iter = read_lines(file_path)?; - let mut _matches = match_pattern(lines_iter, pattern, file_path, ®, cli)?; + let mut _matches = match_pattern(lines_iter, pattern, file_path, ®)?; matches.append(&mut _matches); } @@ -66,7 +66,6 @@ pub fn match_pattern( pattern: &str, file_path: &Path, regex: &Regex, - cli: &Cli, ) -> anyhow::Result { let mut matches = Vec::new(); @@ -107,8 +106,10 @@ mod tests { let input: Vec> = vec![Ok("hola".into()), Ok("no".into()), Ok("hola again".into())]; + let reg = Regex::new("hola").unwrap(); + let result = - match_pattern(input.into_iter(), r"^hola", Path::new("/tmp/test.txt")).unwrap(); + match_pattern(input.into_iter(), r"^hola", Path::new("/tmp/test.txt"), ®).unwrap(); assert_eq!(result.items.len(), 2);