summaryrefslogtreecommitdiff
path: root/src/table.rs
blob: ecc4f454a8a3940fd06461d523b8a7aa4b1b92ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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, b))| Column { name: n, alignment: a, border_right: b };
            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 border_right: bool,
}

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),
        }
    }
}

/// Returns the contents of each cell in the row, and whether the right edge
/// is a vertical border.
fn split_columns(line: &str) -> Option<Vec<(&str, bool)>> {
    if let Some(("", tail)) = line.split_once('|') {
        if let Some((head, "")) = tail.rsplit_once('|') {
            let mut output: Vec<(&str, bool)> = Vec::new();
            for cell in head.split('|') {
                if cell.is_empty () {
                    if let Some(last) = output.last_mut() {
                        last.1 = true;
                    }
                } else {
                    output.push((cell.trim(), false));
                }
            }
            return Some(output);
        }
    }
    return None;
}

fn split_cells(line: &str) -> Option<Vec<Line>> {
    Some(split_columns(line)?.into_iter().map(|(cell, _)| Line::from_str(cell)).collect())
}

fn parse_alignments(line: &str) -> Option<Vec<(Alignment, bool)>> {
    let mut alignments = Vec::new();
    for (cell, border_right) in split_columns(line)? {
        alignments.push((Alignment::from_str(cell)?, border_right));
    }
    Some(alignments)
}