Feat: Add erroor handling to find_matches.

This commit is contained in:
Ian Rettig
2025-06-09 13:16:29 -04:00
parent bbf4da2859
commit bcb4cba5e1
+8 -6
View File
@@ -23,24 +23,26 @@ fn main() -> Result<()> {
content.as_str(),
args.pattern.as_str(),
&mut std::io::stdout(),
);
)?;
Ok(())
}
fn find_matches(content: &str, pattern: &str, mut writer: impl std::io::Write) {
fn find_matches(content: &str, pattern: &str, mut writer: impl std::io::Write) -> Result<()> {
for line in content.lines() {
if line.contains(pattern) {
writeln!(writer, "{}", line);
writeln!(writer, "{}", line)?;
}
}
Ok(())
}
#[test]
fn find_a_match() {
fn find_a_match() -> Result<(), Box<dyn std::error::Error>> {
let mut result = Vec::new();
find_matches("lorem ipsum\ndolar sit amet", "lorem", &mut result);
assert_eq!(result, b"lorem ipsum\n")
find_matches("lorem ipsum\ndolar sit amet", "lorem", &mut result)?;
assert_eq!(result, b"lorem ipsum\n");
Ok(())
}
#[allow(dead_code)]