smolgrep/src/matches.rs
2026-07-27 21:40:32 +03:00

41 lines
886 B
Rust

use std::{fmt::Display, path::PathBuf};
pub struct Match {
pub file_path: PathBuf,
pub line_number: usize,
pub text: String,
}
pub struct Matches {
pub items: Vec<Match>,
}
impl Matches {
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
pub fn append(&mut self, matches: &mut Self) {
self.items.append(&mut matches.items);
}
}
impl Display for Match {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: line {}: {}", self.file_path.display(), self.line_number, self.text)
}
}
impl Display for Matches {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (idx, item) in self.items.iter().enumerate() {
if idx > 0 {
writeln!(f)?;
}
write!(f, "{}", item)?;
}
Ok(())
}
}