2026-07-27 08:43:48 +00:00
|
|
|
use std::{
|
|
|
|
|
fs::File,
|
|
|
|
|
io::{BufRead, BufReader},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use anyhow::Context;
|
2026-07-27 09:04:19 +00:00
|
|
|
use regex::Regex;
|
2026-07-27 08:43:48 +00:00
|
|
|
|
2026-07-27 09:53:58 +00:00
|
|
|
use crate::matches::{self, Match, Matches};
|
|
|
|
|
|
2026-07-27 08:43:48 +00:00
|
|
|
pub fn read_lines(
|
|
|
|
|
file_path: &str,
|
|
|
|
|
) -> anyhow::Result<impl Iterator<Item = std::io::Result<String>>, anyhow::Error> {
|
|
|
|
|
if file_path.is_empty() {
|
|
|
|
|
return Err(anyhow::anyhow!("File path cannot be empty"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let file =
|
|
|
|
|
File::open(file_path).with_context(|| format!("failed to open file: {file_path}"))?;
|
|
|
|
|
let reader = BufReader::new(file);
|
|
|
|
|
|
|
|
|
|
Ok(reader.lines())
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-27 09:53:58 +00:00
|
|
|
fn get_file_name_from_path(file_path: &str) -> String {
|
|
|
|
|
match file_path.rsplit('/').next() {
|
|
|
|
|
Some(name) => name.into(),
|
|
|
|
|
None => file_path.into(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-27 09:04:19 +00:00
|
|
|
pub fn match_pattern(
|
|
|
|
|
iter: impl Iterator<Item = std::io::Result<String>>,
|
|
|
|
|
pattern: &str,
|
2026-07-27 09:53:58 +00:00
|
|
|
file_path: &str,
|
|
|
|
|
) -> anyhow::Result<Matches, anyhow::Error> {
|
2026-07-27 09:04:19 +00:00
|
|
|
let reg = Regex::new(pattern).with_context(|| format!("invalid regex: {:?}", pattern))?;
|
2026-07-27 09:53:58 +00:00
|
|
|
let mut matches = Vec::new();
|
|
|
|
|
|
|
|
|
|
for (idx, line) in iter.enumerate() {
|
|
|
|
|
let line = line?;
|
2026-07-27 09:04:19 +00:00
|
|
|
|
2026-07-27 09:53:58 +00:00
|
|
|
if reg.is_match(&line) {
|
|
|
|
|
let m = Match {
|
|
|
|
|
line_number: idx,
|
|
|
|
|
text: line,
|
|
|
|
|
file_name: get_file_name_from_path(file_path),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
matches.push(m);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-27 09:04:19 +00:00
|
|
|
|
2026-07-27 09:53:58 +00:00
|
|
|
Ok(Matches { items: matches, pattern: String::from(pattern) })
|
2026-07-27 08:43:48 +00:00
|
|
|
}
|