feat(elements): table parsing

This commit is contained in:
PoiScript 2019-08-06 14:03:16 +08:00
parent 470f90bfb5
commit 3e4772a896
6 changed files with 688 additions and 553 deletions

View file

@ -18,6 +18,7 @@ mod planning;
mod radio_target;
mod rule;
mod snippet;
mod table;
mod target;
mod timestamp;
mod title;
@ -46,6 +47,7 @@ pub use self::{
radio_target::RadioTarget,
rule::Rule,
snippet::Snippet,
table::{Table, TableRow},
target::Target,
timestamp::{Datetime, Timestamp},
title::Title,
@ -98,6 +100,9 @@ pub enum Element<'a> {
Comment { value: &'a str },
FixedWidth { value: &'a str },
Title(Title<'a>),
Table(Table<'a>),
TableRow(TableRow),
TableCell,
}
impl Element<'_> {
@ -118,7 +123,10 @@ impl Element<'_> {
| Element::Section
| Element::Strike
| Element::Underline
| Element::Title(_) => true,
| Element::Title(_)
| Element::Table(_)
| Element::TableRow(_)
| Element::TableCell => true,
_ => false,
}
}
@ -167,7 +175,9 @@ impl_from!(
SpecialBlock,
Target,
Timestamp,
Table,
VerseBlock;
RadioTarget,
List
List,
TableRow
);

34
src/elements/table.rs Normal file
View file

@ -0,0 +1,34 @@
#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(tag = "table_type"))]
pub enum Table<'a> {
#[cfg_attr(feature = "serde", serde(rename = "org"))]
Org { tblfm: Option<&'a str> },
#[cfg_attr(feature = "serde", serde(rename = "table.el"))]
TableEl { value: &'a str },
}
#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(
feature = "serde",
serde(tag = "table_row_type", rename_all = "kebab-case")
)]
pub enum TableRow {
Standard,
Rule,
}
impl TableRow {
pub(crate) fn parse(input: &str) -> Option<TableRow> {
if input.starts_with("|-") {
Some(TableRow::Rule)
} else if input.starts_with('|') {
Some(TableRow::Standard)
} else {
None
}
}
}