]> git.scottworley.com Git - tablify/blobdiff - src/lib.rs
column_order()
[tablify] / src / lib.rs
index 01668eb3db650e39157a85f0dc1f50b3663422c2..2dd396469d8252876047f61752091c55b320bcfc 100644 (file)
@@ -51,8 +51,8 @@ impl From<&str> for Entry {
                 instance: None,
             },
             Some((col, instance)) => Entry {
-                col: String::from(col),
-                instance: Some(String::from(instance)),
+                col: String::from(col.trim()),
+                instance: Some(String::from(instance.trim())),
             },
         }
     }
@@ -141,6 +141,12 @@ fn column_counts(rows: &[RowInput]) -> Vec<(usize, String)> {
     counts.sort();
     counts
 }
+fn column_order(rows: &[RowInput]) -> Vec<String> {
+    column_counts(rows)
+        .into_iter()
+        .map(|(_, col)| col)
+        .collect()
+}
 
 /// # Errors
 ///
@@ -150,7 +156,7 @@ fn column_counts(rows: &[RowInput]) -> Vec<(usize, String)> {
 ///     * an indented line with no preceding non-indented line
 pub fn tablify(input: impl std::io::Read) -> Result<String, std::io::Error> {
     let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?;
-    let _columns = column_counts(&rows);
+    let _columns = column_order(&rows);
     Ok(String::from(HEADER) + "Hello, world!" + FOOTER)
 }
 
@@ -174,6 +180,13 @@ mod tests {
                 instance: Some(String::from("bar"))
             }
         );
+        assert_eq!(
+            Entry::from("foo: bar"),
+            Entry {
+                col: String::from("foo"),
+                instance: Some(String::from("bar"))
+            }
+        );
     }
 
     #[test]