From de79955778b0e94b335653271cf245857953e9ae Mon Sep 17 00:00:00 2001 From: Mohammad Kanaan Date: Mon, 27 Jul 2026 21:40:32 +0300 Subject: [PATCH] refactor: remove pattern from matches as it's not needed on each instance --- src/main.rs | 3 +++ src/matches.rs | 3 --- src/parser.rs | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0983088..ce468cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,9 @@ fn run() -> anyhow::Result { // println!("{:?} {}", cli.file_paths, cli.pattern); let matches = process_files(&cli.file_paths, &cli.pattern, &cli.get_options())?; + if !matches.is_empty() { + println!("Matches for pattern {}:", cli.pattern); + } Ok(matches) } diff --git a/src/matches.rs b/src/matches.rs index c1614ca..60b38ee 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -8,7 +8,6 @@ pub struct Match { pub struct Matches { pub items: Vec, - pub pattern: String, } impl Matches { @@ -29,8 +28,6 @@ impl Display for Match { impl Display for Matches { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - writeln!(f, "Matches for pattern {}:", self.pattern)?; - for (idx, item) in self.items.iter().enumerate() { if idx > 0 { writeln!(f)?; diff --git a/src/parser.rs b/src/parser.rs index 21af148..da55434 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -22,8 +22,9 @@ pub fn process_files( .build() .with_context(|| format!("invalid regex: {:?}", pattern))?; - let mut matches = Matches { items: vec![], pattern: String::from(pattern) }; + let mut matches = Matches { items: vec![] }; + // fails and stops everything when one file fails for file_path in file_paths { let lines_iter = read_lines(file_path)?; let mut _matches = match_pattern(lines_iter, pattern, file_path, ®)?; @@ -71,7 +72,7 @@ pub fn match_pattern( } } - Ok(Matches { items: matches, pattern: String::from(pattern) }) + Ok(Matches { items: matches }) } #[cfg(test)]