From: Scott Worley Date: Mon, 19 Aug 2024 08:48:16 +0000 (-0700) Subject: Treat all-whitespace lines as blank lines X-Git-Tag: v0.2.0~18 X-Git-Url: http://git.scottworley.com/tablify/commitdiff_plain/1f6bd845277daaea2513cc3dc19a0907fe9f6d6c?hp=201b9ef3e07ed5948730079475f79703b0cd28f8 Treat all-whitespace lines as blank lines As a side effect, this also removes trailing whitespace, which nice. --- 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());