]> git.scottworley.com Git - tablify/blobdiff - src/lib.rs
Treat all-whitespace lines as blank lines
[tablify] / 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 {
-            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<_>>(),
+            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());