1pub mod colors;
41pub mod crawler;
42pub mod highlighter;
43pub mod result;
44pub mod search;
45pub mod search_xtreme;
46
47use crate::colors::Color;
48use crate::crawler::get_files;
49use crate::result::{print_result, print_xtreme_stats};
50use crate::search::search_files;
51use crate::search_xtreme::search_files_xtreme;
52use std::path::PathBuf;
53use std::time::Instant;
54
55pub fn run(dir: &PathBuf, pattern: &str, color: &Color, show_stats: bool) {
56 let start_time = Instant::now();
57 let files = get_files(dir);
58 let rx = search_files(&files, pattern, color, show_stats);
59
60 print_result(rx, show_stats, start_time);
61}
62
63pub fn run_xtreme(dir: &PathBuf, pattern: &str, color: &Color, show_stats: bool) {
64 let start_time = Instant::now();
65 let files = get_files(dir);
66 let (files_processed, lines, matches, skipped) =
67 search_files_xtreme(&files, pattern, color, show_stats);
68
69 if show_stats {
70 print_xtreme_stats(files_processed, lines, matches, skipped, start_time);
71 }
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77 use std::fs::File;
78 use std::io::Write;
79 use tempdir::TempDir;
80
81 #[test]
82 fn test_run_integration() {
83 let temp_dir = TempDir::new("lib_test").unwrap();
85 let test_file = temp_dir.path().join("test.txt");
86
87 let mut file = File::create(&test_file).unwrap();
88 writeln!(file, "Hello world").unwrap();
89 writeln!(file, "This is a test").unwrap();
90
91 let pattern = "Hello";
92 let color = Color::Red;
93
94 run(&temp_dir.path().to_path_buf(), pattern, &color, false);
97 }
98
99 #[test]
100 fn test_run_with_single_file() {
101 let temp_dir = TempDir::new("lib_single_test").unwrap();
103 let test_file = temp_dir.path().join("single.txt");
104
105 let mut file = File::create(&test_file).unwrap();
106 writeln!(file, "Pattern match here").unwrap();
107
108 let pattern = "Pattern";
109 let color = Color::Blue;
110
111 run(&test_file, pattern, &color, false);
113 }
114
115 #[test]
116 fn test_run_with_no_matches() {
117 let temp_dir = TempDir::new("lib_no_match_test").unwrap();
119 let test_file = temp_dir.path().join("nomatch.txt");
120
121 let mut file = File::create(&test_file).unwrap();
122 writeln!(file, "This file has no pattern").unwrap();
123
124 let pattern = "NonExistentPattern";
125 let color = Color::Green;
126
127 run(&temp_dir.path().to_path_buf(), pattern, &color, false);
129 }
130
131 #[test]
132 fn test_run_different_colors() {
133 let temp_dir = TempDir::new("lib_colors_test").unwrap();
135 let test_file = temp_dir.path().join("colors.txt");
136
137 let mut file = File::create(&test_file).unwrap();
138 writeln!(file, "Test pattern").unwrap();
139
140 let pattern = "pattern";
141
142 run(&temp_dir.path().to_path_buf(), pattern, &Color::Red, false);
144 run(
145 &temp_dir.path().to_path_buf(),
146 pattern,
147 &Color::Green,
148 false,
149 );
150 run(&temp_dir.path().to_path_buf(), pattern, &Color::Blue, false);
151 run(&temp_dir.path().to_path_buf(), pattern, &Color::Bold, false);
152 }
153}