X-Git-Url: http://git.scottworley.com/tablify/blobdiff_plain/28cf4fa29f4e47d8b78894fbe86b175e0ab292be..5a070b309feffc87f170365411c5e71626f72520:/src/lib.rs?ds=sidebyside
diff --git a/src/lib.rs b/src/lib.rs
index 3e0efd0..5c994c9 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -4,30 +4,34 @@ use std::fmt::Write;
use std::io::BufRead;
use std::iter::Iterator;
-pub struct Config {}
+pub struct Config {
+ pub column_threshold: usize,
+}
-const HEADER: &str = "
+const HEADER: &str = r#"
-
-
+
+
@@ -35,7 +39,7 @@ const HEADER: &str = "
-";
+"#;
const FOOTER: &str = "
@@ -106,6 +110,12 @@ struct Row {
entries: HashMap>>,
}
+#[derive(Debug, PartialEq, Eq)]
+enum Rowlike {
+ Row(Row),
+ Spacer,
+}
+
struct Reader>> {
input: std::iter::Enumerate,
row: Option,
@@ -119,17 +129,17 @@ impl>> Reader {
}
}
impl>> Iterator for Reader {
- type Item = Result;
+ type Item = Result;
fn next(&mut self) -> Option {
loop {
match self.input.next() {
- None => return Ok(std::mem::take(&mut self.row)).transpose(),
+ None => return Ok(std::mem::take(&mut self.row).map(Rowlike::Row)).transpose(),
Some((_, Err(e))) => return Some(Err(e)),
Some((n, Ok(line))) => match InputLine::from(line.as_ref()) {
InputLine::Blank if self.row.is_some() => {
- return Ok(std::mem::take(&mut self.row)).transpose()
+ return Ok(std::mem::take(&mut self.row).map(Rowlike::Row)).transpose()
}
- InputLine::Blank => {}
+ InputLine::Blank => return Some(Ok(Rowlike::Spacer)),
InputLine::Entry(col, instance) => match &mut self.row {
None => {
return Some(Err(std::io::Error::other(format!(
@@ -151,7 +161,7 @@ impl>> Iterator for Reader
entries: HashMap::new(),
});
if prev.is_some() {
- return Ok(prev).transpose();
+ return Ok(prev.map(Rowlike::Row)).transpose();
}
}
},
@@ -160,14 +170,23 @@ impl>> Iterator for Reader
}
}
-fn read_rows(input: impl std::io::Read) -> impl Iterator- > {
+fn read_input(input: impl std::io::Read) -> Result<(Vec, Config), std::io::Error> {
+ let default_config = Config {
+ column_threshold: 2,
+ };
Reader::new(std::io::BufReader::new(input).lines())
+ .collect::, _>>()
+ .map(|rows| (rows, default_config))
}
-fn column_counts(rows: &[Row]) -> Vec<(usize, String)> {
+fn column_counts(rows: &[Rowlike]) -> Vec<(usize, String)> {
+ let empty = HashMap::new();
let mut counts: Vec<_> = rows
.iter()
- .flat_map(|r| r.entries.keys())
+ .flat_map(|rl| match rl {
+ Rowlike::Row(r) => r.entries.keys(),
+ Rowlike::Spacer => empty.keys(),
+ })
.fold(HashMap::new(), |mut cs, col| {
cs.entry(col.to_owned())
.and_modify(|n| *n += 1)
@@ -180,66 +199,101 @@ fn column_counts(rows: &[Row]) -> Vec<(usize, String)> {
counts.sort_unstable_by(|(an, acol), (bn, bcol)| bn.cmp(an).then(acol.cmp(bcol)));
counts
}
-fn column_order(rows: &[Row]) -> Vec {
+fn column_order(config: &Config, rows: &[Rowlike]) -> Vec {
column_counts(rows)
.into_iter()
- .map(|(_, col)| col)
+ .filter_map(|(n, col)| (n >= config.column_threshold).then_some(col))
.collect()
}
-fn render_instance(instance: &Option) -> HTML {
+fn render_one_instance(instance: &Option) -> HTML {
match instance {
None => HTML::from("â"),
Some(instance) => HTML::escape(instance.as_ref()),
}
}
-fn render_cell(col: &str, row: &Row) -> HTML {
- let row_label = HTML::escape(row.label.as_ref());
- let col_label = HTML::escape(col);
- let instances: Option<&Vec