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(), content.as_str(),
args.pattern.as_str(), args.pattern.as_str(),
&mut std::io::stdout(), &mut std::io::stdout(),
); )?;
Ok(()) 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() { for line in content.lines() {
if line.contains(pattern) { if line.contains(pattern) {
writeln!(writer, "{}", line); writeln!(writer, "{}", line)?;
} }
} }
Ok(())
} }
#[test] #[test]
fn find_a_match() { fn find_a_match() -> Result<(), Box<dyn std::error::Error>> {
let mut result = Vec::new(); let mut result = Vec::new();
find_matches("lorem ipsum\ndolar sit amet", "lorem", &mut result); find_matches("lorem ipsum\ndolar sit amet", "lorem", &mut result)?;
assert_eq!(result, b"lorem ipsum\n") assert_eq!(result, b"lorem ipsum\n");
Ok(())
} }
#[allow(dead_code)] #[allow(dead_code)]