summaryrefslogtreecommitdiff
path: root/src/table.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/table.rs')
-rw-r--r--src/table.rs85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/table.rs b/src/table.rs
new file mode 100644
index 0000000..071bd1a
--- /dev/null
+++ b/src/table.rs
@@ -0,0 +1,85 @@
+use crate::*;
+
+pub struct Table {
+ /// A [Line] is the content of a cell, a group of cells forms a table row,
+ /// a group of rows forms a separated section of the table, and a group of
+ /// sections forms the table itself.
+ /// Each row in the table has the same number of columns as the table header.
+ pub sections: Vec<Vec<Vec<Line>>>,
+ pub columns: Vec<Column>,
+}
+
+impl Table {
+ pub fn from_strs(lines: &[&str]) -> Option<Self> {
+ let mut lines = lines.into_iter();
+ let columns: Vec<Column> = {
+ let names = split_cells(lines.next()?)?;
+ let alignments = parse_alignments(lines.next()?)?;
+ if names.len() != alignments.len() { return None }
+ let make_column = |(n, a)| Column { name: n, alignment: a };
+ std::iter::zip(names, alignments).map(make_column).collect()
+ };
+ let mut sections = Vec::new();
+ let mut rows = Vec::new();
+
+ for line in lines {
+ if let Some(alignments) = parse_alignments(line) {
+ if alignments.len() != columns.len() { return None }
+ sections.push(std::mem::take(&mut rows))
+ } else {
+ let row: Vec<Line> = split_cells(line)?;
+ if row.len() != columns.len() { return None }
+ rows.push(row);
+ }
+ }
+ if !rows.is_empty() {
+ sections.push(std::mem::take(&mut rows));
+ }
+ return Some( Self { columns, sections } );
+ }
+}
+
+pub struct Column {
+ pub name: Line,
+ pub alignment: Alignment,
+}
+
+pub enum Alignment {
+ Left,
+ Center,
+ Right,
+}
+
+impl Alignment {
+ pub fn from_str(cell: &str) -> Option<Self> {
+ if !cell.chars().all(|c| c == ':' || c == '-') {
+ return None }
+ match (cell.starts_with(':'), cell.ends_with(':')) {
+ (false, false) => Some(Alignment::Left),
+ (false, true ) => Some(Alignment::Right),
+ (true, false) => Some(Alignment::Left),
+ (true, true ) => Some(Alignment::Center),
+ }
+ }
+}
+
+fn split_columns(line: &str) -> Option<Vec<&str>> {
+ if let Some(("", tail)) = line.split_once('|') {
+ if let Some((head, "")) = tail.rsplit_once('|') {
+ return Some(head.split('|').map(str::trim).collect());
+ }
+ }
+ return None;
+}
+
+fn split_cells(line: &str) -> Option<Vec<Line>> {
+ Some(split_columns(line)?.into_iter().map(Line::from_str).collect())
+}
+
+fn parse_alignments(line: &str) -> Option<Vec<Alignment>> {
+ let mut alignments = Vec::new();
+ for cell in split_columns(line)? {
+ alignments.push(Alignment::from_str(cell)?);
+ }
+ Some(alignments)
+}