X-Git-Url: http://git.scottworley.com/tablify/blobdiff_plain/88a08162523cf4a0a7c1286b1438fdf16ca6c802..06a6a5cad195d0b7504c4e7b4caf085487f53578:/src/lib.rs?ds=inline
diff --git a/src/lib.rs b/src/lib.rs
index b406f95..73e517b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -4,11 +4,15 @@ use std::fmt::Write;
use std::io::BufRead;
use std::iter::Iterator;
-const HEADER: &str = "
+pub struct Config {
+ pub column_threshold: usize,
+}
+
+const HEADER: &str = r#"
-
-
+
+
@@ -33,7 +38,7 @@ const HEADER: &str = "
-";
+"#;
const FOOTER: &str = "
@@ -104,6 +109,12 @@ struct Row {
entries: HashMap>>,
}
+#[derive(Debug, PartialEq, Eq)]
+enum Rowlike {
+ Row(Row),
+ Spacer,
+}
+
struct Reader>> {
input: std::iter::Enumerate,
row: Option,
@@ -117,15 +128,15 @@ 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::Entry(col, instance) => match &mut self.row {
@@ -149,7 +160,7 @@ impl>> Iterator for Reader
entries: HashMap::new(),
});
if prev.is_some() {
- return Ok(prev).transpose();
+ return Ok(prev.map(Rowlike::Row)).transpose();
}
}
},
@@ -158,14 +169,18 @@ impl>> Iterator for Reader
}
}
-fn read_rows(input: impl std::io::Read) -> impl Iterator- > {
+fn read_rows(input: impl std::io::Read) -> impl Iterator
- > {
Reader::new(std::io::BufReader::new(input).lines())
}
-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)
@@ -178,66 +193,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