(
path: &Path,
regex: ®ex::Regex,
matches: &mut Vec<MatchResult>,
limit: usize,
)
| 210 | } |
| 211 | |
| 212 | fn search_file( |
| 213 | path: &Path, |
| 214 | regex: ®ex::Regex, |
| 215 | matches: &mut Vec<MatchResult>, |
| 216 | limit: usize, |
| 217 | ) -> Result<(), io::Error> { |
| 218 | let file = File::open(path)?; |
| 219 | let reader = BufReader::new(file); |
| 220 | let path_str = path.to_string_lossy().to_string(); |
| 221 | let mut offset = 0; |
| 222 | |
| 223 | for (line_num, line_result) in reader.lines().enumerate() { |
| 224 | if matches.len() >= limit { |
| 225 | break; |
| 226 | } |
| 227 | |
| 228 | let line = line_result?; |
| 229 | let line_len = line.len() + 1; |
| 230 | |
| 231 | if regex.is_match(&line) { |
| 232 | let mut submatches = Vec::new(); |
| 233 | for cap in regex.find_iter(&line) { |
| 234 | submatches.push(SubMatch { |
| 235 | text: cap.as_str().to_string(), |
| 236 | start: cap.start(), |
| 237 | end: cap.end(), |
| 238 | }); |
| 239 | } |
| 240 | |
| 241 | matches.push(MatchResult { |
| 242 | path: path_str.clone(), |
| 243 | line_number: line_num + 1, |
| 244 | lines: line, |
| 245 | absolute_offset: offset, |
| 246 | submatches, |
| 247 | }); |
| 248 | } |
| 249 | |
| 250 | offset += line_len; |
| 251 | } |
| 252 | |
| 253 | Ok(()) |
| 254 | } |
| 255 | |
| 256 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 257 | struct TreeNode { |
no test coverage detected