use std::io::Write;
use std::fmt::Debug;
use anyhow::{anyhow, Context, Result, Error};
pub fn find_matches(content: &str, pattern: &str, mut writer: impl Write + Debug) -> Result<(), Error> {
if pattern.is_empty() {
return Err(anyhow!("Empty pattern is not allowed"));
}
let mut matches_found = false;
for line in content.lines() {
if line.contains(pattern) {
writeln!(writer, "{}", line)
.with_context(|| format!("Could not write line to {:?}", writer))?;
matches_found = true;
}
}
if matches_found {
Ok(())
} else {
Err(anyhow!("No matches found for pattern: {}", pattern))
}
}