X-Git-Url: http://git.scottworley.com/tablify/blobdiff_plain/d9bfcf4d1b74bebfcc9881aad4f0aacd01238b01..215d38d5b5ca4c77bc517c75fc93498a1d20b24b:/src/lib.rs?ds=inline
diff --git a/src/lib.rs b/src/lib.rs
index c5761d8..acf5383 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -4,30 +4,45 @@ use std::fmt::Write;
use std::io::BufRead;
use std::iter::Iterator;
-pub struct Config {}
+#[derive(PartialEq, Eq, Debug)]
+struct Config {
+ column_threshold: usize,
+}
+impl Config {
+ fn apply_command(&mut self, cmd: &str) -> Result<(), std::io::Error> {
+ if let Some(threshold) = cmd.strip_prefix("col_threshold ") {
+ self.column_threshold = threshold
+ .parse()
+ .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidInput, e))?;
+ }
+ Ok(())
+ }
+}
-const HEADER: &str = "
+const HEADER: &str = r#"
-
-
+
+
@@ -35,7 +50,7 @@ const HEADER: &str = "
-";
+"#;
const FOOTER: &str = "
@@ -83,12 +98,15 @@ enum InputLine<'a> {
Blank,
RowHeader(&'a str),
Entry(&'a str, Option<&'a str>),
+ Command(&'a str),
}
impl<'a> From<&'a str> for InputLine<'a> {
fn from(value: &'a str) -> InputLine<'a> {
let trimmed = value.trim_end();
if trimmed.is_empty() {
InputLine::Blank
+ } else if let Some(cmd) = trimmed.strip_prefix('!') {
+ InputLine::Command(cmd)
} else if !trimmed.starts_with(' ') {
InputLine::RowHeader(value.trim())
} else {
@@ -106,30 +124,45 @@ struct Row {
entries: HashMap>>,
}
-struct Reader>> {
+#[derive(Debug, PartialEq, Eq)]
+enum Rowlike {
+ Row(Row),
+ Spacer,
+}
+
+struct Reader<'cfg, Input: Iterator- >> {
input: std::iter::Enumerate,
row: Option
,
+ config: &'cfg mut Config,
}
-impl>> Reader {
- fn new(input: Input) -> Self {
+impl<'cfg, Input: Iterator- >> Reader<'cfg, Input> {
+ fn new(config: &'cfg mut Config, input: Input) -> Self {
Self {
input: input.enumerate(),
row: None,
+ config,
}
}
}
-impl>> Iterator for Reader {
- type Item = Result
;
+impl<'cfg, Input: Iterator- >> Iterator
+ for Reader<'cfg, Input>
+{
+ 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::Command(cmd) => {
+ if let Err(e) = self.config.apply_command(cmd) {
+ return Some(Err(e));
+ }
+ }
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 +184,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 +193,24 @@ impl>> Iterator for Reader
}
}
-fn read_rows(input: impl std::io::Read) -> impl Iterator
- > {
- Reader::new(std::io::BufReader::new(input).lines())
+fn read_input(input: impl std::io::Read) -> Result<(Vec, Config), std::io::Error> {
+ let mut config = Config {
+ column_threshold: 2,
+ };
+ let reader = Reader::new(&mut config, std::io::BufReader::new(input).lines());
+ reader
+ .collect::, _>>()
+ .map(|rows| (rows, 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,67 +223,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: &mut Row) -> HTML {
- let row_label = HTML::escape(row.label.as_ref());
- let col_label = HTML::escape(col);
- let instances: Option<&Vec