matches: store path instead of file name
This commit is contained in:
parent
a1c13d4342
commit
ea6c75a0a8
2 changed files with 10 additions and 14 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
use std::fmt::Display;
|
use std::{fmt::Display, path::PathBuf};
|
||||||
|
|
||||||
pub struct Match {
|
pub struct Match {
|
||||||
pub file_name: String,
|
pub file_path: PathBuf,
|
||||||
pub line_number: usize,
|
pub line_number: usize,
|
||||||
pub text: String,
|
pub text: String,
|
||||||
}
|
}
|
||||||
|
|
@ -23,7 +23,7 @@ impl Matches {
|
||||||
|
|
||||||
impl Display for Match {
|
impl Display for Match {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,12 +46,12 @@ pub fn read_lines(
|
||||||
Ok(reader.lines())
|
Ok(reader.lines())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_file_name_from_path(file_path: &Path) -> String {
|
// fn get_file_name_from_path(file_path: &Path) -> String {
|
||||||
match file_path.file_name() {
|
// match file_path.file_name() {
|
||||||
Some(name) => name.to_string_lossy().to_string(),
|
// Some(name) => name.to_string_lossy().to_string(),
|
||||||
None => String::from("unknown"),
|
// None => String::from("unknown"),
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub fn match_pattern(
|
pub fn match_pattern(
|
||||||
iter: impl Iterator<Item = std::io::Result<String>>,
|
iter: impl Iterator<Item = std::io::Result<String>>,
|
||||||
|
|
@ -65,11 +65,7 @@ pub fn match_pattern(
|
||||||
let line = line?;
|
let line = line?;
|
||||||
|
|
||||||
if regex.is_match(&line) {
|
if regex.is_match(&line) {
|
||||||
let m = Match {
|
let m = Match { line_number: idx + 1, text: line, file_path: PathBuf::from(file_path) };
|
||||||
line_number: idx + 1,
|
|
||||||
text: line,
|
|
||||||
file_name: get_file_name_from_path(file_path),
|
|
||||||
};
|
|
||||||
|
|
||||||
matches.push(m);
|
matches.push(m);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue