Feat: Add erroor handling to find_matches.
This commit is contained in:
+8
-6
@@ -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)]
|
||||||
|
|||||||
Reference in New Issue
Block a user