X-Git-Url: http://git.scottworley.com/tablify/blobdiff_plain/88a08162523cf4a0a7c1286b1438fdf16ca6c802..529cbaa2f61a4318ea6e6982f7ff08c44cbcf1ab:/src/lib.rs?ds=inline
diff --git a/src/lib.rs b/src/lib.rs
index b406f95..fbadb2a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -4,28 +4,34 @@ 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 +39,7 @@ const HEADER: &str = "
-";
+"#;
const FOOTER: &str = "
@@ -104,6 +110,12 @@ struct Row {
entries: HashMap>>,
}
+#[derive(Debug, PartialEq, Eq)]
+enum Rowlike {
+ Row(Row),
+ Spacer,
+}
+
struct Reader>> {
input: std::iter::Enumerate,
row: Option,
@@ -117,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!(
@@ -149,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();
}
}
},
@@ -158,14 +170,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 +194,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