From 1f6bd845277daaea2513cc3dc19a0907fe9f6d6c Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Mon, 19 Aug 2024 01:48:16 -0700 Subject: [PATCH] Treat all-whitespace lines as blank lines As a side effect, this also removes trailing whitespace, which nice. --- src/lib.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index b583c3e..72a3add 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,11 @@ impl>> Iterator for Reader type Item = Result; fn next(&mut self) -> Option { loop { - match self.input.next() { + match self + .input + .next() + .map(|r| r.map(|line| String::from(line.trim_end()))) + { None => return Ok(std::mem::take(&mut self.row)).transpose(), Some(Err(e)) => return Some(Err(e)), Some(Ok(line)) if line.is_empty() && self.row.is_some() => { @@ -122,6 +126,30 @@ mod tests { } ] ); + assert_eq!( + read_rows(&b"foo\n \nbar\n"[..]) + .flatten() + .collect::>(), + vec![ + RowInput { + label: String::from("foo"), + entries: vec![] + }, + RowInput { + label: String::from("bar"), + entries: vec![] + } + ] + ); + assert_eq!( + read_rows(&b"foo \n bar \n"[..]) + .flatten() + .collect::>(), + vec![RowInput { + label: String::from("foo"), + entries: vec![String::from("bar")] + }] + ); let bad = read_rows(&b" foo"[..]).next().unwrap(); assert!(bad.is_err()); -- 2.44.1