matches: store path instead of file name

This commit is contained in:
Mohammad Kanaan 2026-07-27 21:32:25 +03:00
parent a1c13d4342
commit ea6c75a0a8
2 changed files with 10 additions and 14 deletions

View file

@ -1,7 +1,7 @@
use std::fmt::Display;
use std::{fmt::Display, path::PathBuf};
pub struct Match {
pub file_name: String,
pub file_path: PathBuf,
pub line_number: usize,
pub text: String,
}
@ -23,7 +23,7 @@ impl Matches {
impl Display for Match {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: line {}: {}", self.file_name, self.line_number, self.text)
write!(f, "{}: line {}: {}", self.file_path.display(), self.line_number, self.text)
}
}

View file

@ -46,12 +46,12 @@ pub fn read_lines(
Ok(reader.lines())
}
fn get_file_name_from_path(file_path: &Path) -> String {
match file_path.file_name() {
Some(name) => name.to_string_lossy().to_string(),
None => String::from("unknown"),
}
}
// fn get_file_name_from_path(file_path: &Path) -> String {
// match file_path.file_name() {
// Some(name) => name.to_string_lossy().to_string(),
// None => String::from("unknown"),
// }
// }
pub fn match_pattern(
iter: impl Iterator<Item = std::io::Result<String>>,
@ -65,11 +65,7 @@ pub fn match_pattern(
let line = line?;
if regex.is_match(&line) {
let m = Match {
line_number: idx + 1,
text: line,
file_name: get_file_name_from_path(file_path),
};
let m = Match { line_number: idx + 1, text: line, file_path: PathBuf::from(file_path) };
matches.push(m);
}