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
 99
100
101
102
103
104
105
106
use crate::composite::{Composite, CompositeStyle};
use crate::compound::{Alignment, Compound};
use crate::line_parser::LineParser;
use crate::tbl::{TableRow, TableRule};

pub const MAX_HEADER_DEPTH: usize = 8;

/// a parsed line
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Line<'a> {
    Normal(Composite<'a>),  //
    TableRow(TableRow<'a>), // a normal table row, with cells having content
    TableRule(TableRule),   // a separator/border in a table, optionally defining alignments
    HorizontalRule,         // an horizontal line dividing the screen
    CodeFence(Composite<'a>),
}

impl Line<'_> {
    pub fn from(md: &str) -> Line<'_> {
        LineParser::from(md).line()
    }
    #[inline(always)]
    pub fn char_length(&self) -> usize {
        match self {
            Line::Normal(composite) => composite.char_length(),
            Line::TableRow(row) => row.cells.iter().fold(0, |s, c| s + c.char_length()),
            _ => 0, // no known char length for table format lines
        }
    }
    pub fn new_paragraph(compounds: Vec<Compound<'_>>) -> Line<'_> {
        Line::Normal(Composite {
            style: CompositeStyle::Paragraph,
            compounds,
        })
    }
    pub fn empty_code_fence() -> Line<'static> {
        Line::CodeFence(Composite {
            style: CompositeStyle::Paragraph,
            compounds: vec![],
        })
    }
    pub fn new_code_fence(compounds: Vec<Compound<'_>>) -> Line<'_> {
        Line::CodeFence(Composite {
            style: CompositeStyle::Paragraph,
            compounds,
        })
    }
    pub fn new_code(compound: Compound<'_>) -> Line<'_> {
        Line::Normal(Composite {
            style: CompositeStyle::Code,
            compounds: vec![compound],
        })
    }
    pub fn new_quote(compounds: Vec<Compound<'_>>) -> Line<'_> {
        Line::Normal(Composite {
            style: CompositeStyle::Quote,
            compounds,
        })
    }
    pub fn new_list_item(compounds: Vec<Compound<'_>>) -> Line<'_> {
        Line::Normal(Composite {
            style: CompositeStyle::ListItem,
            compounds,
        })
    }
    pub fn new_header(level: u8, compounds: Vec<Compound<'_>>) -> Line<'_> {
        Line::Normal(Composite {
            style: CompositeStyle::Header(level),
            compounds,
        })
    }
    pub fn new_table_row(cells: Vec<Composite<'_>>) -> Line<'_> {
        Line::TableRow(TableRow { cells })
    }
    pub fn new_table_alignments(cells: Vec<Alignment>) -> Line<'static> {
        Line::TableRule(TableRule { cells })
    }
    #[inline(always)]
    pub fn is_table_row(&self) -> bool {
        match self {
            Line::TableRow(_) => true,
            _ => false,
        }
    }
    #[inline(always)]
    pub fn is_table_part(&self) -> bool {
        match self {
            Line::Normal(_) => false,
            _ => true,
        }
    }
    #[inline(always)]
    pub fn is_code(&self) -> bool {
        match self {
            Line::Normal(composite) => composite.is_code(),
            _ => false,
        }
    }
}

#[test]
pub fn count_chars() {
    assert_eq!(Line::from("τ").char_length(), 1);
    assert_eq!(Line::from("τ:`2π`").char_length(), 4);
    assert_eq!(Line::from("* item").char_length(), 4);
}