]> git.scottworley.com Git - tablify/commitdiff
Treat all-whitespace lines as blank lines
authorScott Worley <scottworley@scottworley.com>
Mon, 19 Aug 2024 08:48:16 +0000 (01:48 -0700)
committerScott Worley <scottworley@scottworley.com>
Mon, 19 Aug 2024 08:53:37 +0000 (01:53 -0700)
As a side effect, this also removes trailing whitespace, which nice.

src/lib.rs

index b583c3eefb0b3e3f1ea74f0aae24556b711865a1..72a3add0ef2f9edccc34cc6f39b9ce8df9f79b56 100644 (file)
@@ -23,7 +23,11 @@ impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader
     type Item = Result<RowInput, std::io::Error>;
     fn next(&mut self) -> Option<Self::Item> {
         loop {
     type Item = Result<RowInput, std::io::Error>;
     fn next(&mut self) -> Option<Self::Item> {
         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() => {
                 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<_>>(),
+            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<_>>(),
+            vec![RowInput {
+                label: String::from("foo"),
+                entries: vec![String::from("bar")]
+            }]
+        );
 
         let bad = read_rows(&b" foo"[..]).next().unwrap();
         assert!(bad.is_err());
 
         let bad = read_rows(&b" foo"[..]).next().unwrap();
         assert!(bad.is_err());