From b6002fee979606986458449b8ffe6d290dbba8d1 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 16 Sep 2020 22:45:52 +0200 Subject: write_since(): Handle errors in `diff.foreach()` callback * Ignore lines that don't parse to UTF-8. Might want to figure out how to support other encodings. * Ignore lines that don't have a `path`. * Panic on write errors. --- src/lib.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0b64b22..4faaf20 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,16 +41,18 @@ impl Todos<'_> { // ); if let Some(line_number) = line.new_lineno() { - let l = std::str::from_utf8(line.content()).unwrap(); - - if l.contains("TODO") { - write!( - write_to, - "{}:{}:{}", - delta.new_file().path().unwrap().display(), - line_number, - l, - ).unwrap(); + if let Ok(l) = std::str::from_utf8(line.content()) { + if l.contains("TODO") { + if let Some(path) = delta.new_file().path() { + write!( + write_to, + "{}:{}:{}", + path.display(), + line_number, + l, + ).expect("write error"); + } + } } } -- cgit v1.2.3