smolgrep/src/matches.rs

35 lines
751 B
Rust
Raw Normal View History

use std::fmt::Display;
pub struct Match {
pub file_name: String,
pub line_number: usize,
pub text: String,
}
pub struct Matches {
pub items: Vec<Match>,
pub pattern: String,
}
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)
}
}
impl Display for Matches {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Matches for pattern {}:", self.pattern)?;
for (idx, item) in self.items.iter().enumerate() {
if idx > 0 {
writeln!(f)?;
}
write!(f, "{}", item)?;
}
2026-07-27 10:05:19 +00:00
Ok(())
}
}