use anyhow::{Error, Result};
pub const PREFIX: &str = "found - ";
pub fn find_matches(
content: &str,
pattern: &str,
mut writer: impl std::io::Write,
) -> Result<(), Error> {
for line in content.lines() {
if line.contains(pattern) {
writeln!(writer, "{}{}", PREFIX, line)?;
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn find_match() {
let mut res = Vec::new();
let ret = find_matches("hi\nlol and ok\nlulz", "lol", &mut res);
let mut to_be_compared: String = String::new();
let shoud_find_string = "lol and ok\n";
to_be_compared.push_str(PREFIX);
to_be_compared.push_str(&shoud_find_string);
println!("to be comp: {}", to_be_compared);
assert_eq!(ret.unwrap(), ());
assert_eq!(res, to_be_compared.as_bytes());
}
}