2023-04-12 14:07:33 -04:00
|
|
|
use super::PlainListItem;
|
2023-03-24 17:00:27 -04:00
|
|
|
use super::error::Res;
|
2023-04-07 17:14:44 -04:00
|
|
|
use super::footnote_definition::footnote_definition;
|
2023-04-03 18:33:07 -04:00
|
|
|
use super::greater_block::greater_block;
|
2023-04-07 17:14:44 -04:00
|
|
|
use super::greater_element::FootnoteDefinition;
|
2023-04-03 17:36:56 -04:00
|
|
|
use super::greater_element::GreaterBlock;
|
2023-03-23 17:26:07 -04:00
|
|
|
use super::greater_element::PlainList;
|
|
|
|
use super::lesser_element::Paragraph;
|
2023-03-25 12:53:57 -04:00
|
|
|
use super::paragraph::paragraph;
|
2023-03-27 13:06:41 -04:00
|
|
|
use super::plain_list::plain_list;
|
2023-03-23 17:51:49 -04:00
|
|
|
use super::source::Source;
|
2023-03-24 17:00:27 -04:00
|
|
|
use super::Context;
|
2023-03-27 13:06:41 -04:00
|
|
|
use crate::parser::parser_with_context::parser_with_context;
|
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::combinator::map;
|
2023-03-23 17:26:07 -04:00
|
|
|
|
2023-03-23 17:51:49 -04:00
|
|
|
#[derive(Debug)]
|
2023-03-23 17:26:07 -04:00
|
|
|
pub enum Element<'s> {
|
|
|
|
Paragraph(Paragraph<'s>),
|
|
|
|
PlainList(PlainList<'s>),
|
2023-04-03 17:36:56 -04:00
|
|
|
GreaterBlock(GreaterBlock<'s>),
|
2023-04-07 17:14:44 -04:00
|
|
|
FootnoteDefinition(FootnoteDefinition<'s>),
|
2023-03-23 17:26:07 -04:00
|
|
|
}
|
2023-03-23 17:51:49 -04:00
|
|
|
|
|
|
|
impl<'s> Source<'s> for Element<'s> {
|
|
|
|
fn get_source(&'s self) -> &'s str {
|
|
|
|
match self {
|
|
|
|
Element::Paragraph(obj) => obj.source,
|
|
|
|
Element::PlainList(obj) => obj.source,
|
2023-04-03 17:36:56 -04:00
|
|
|
Element::GreaterBlock(obj) => obj.source,
|
2023-04-07 17:14:44 -04:00
|
|
|
Element::FootnoteDefinition(obj) => obj.source,
|
2023-03-23 17:51:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-24 17:00:27 -04:00
|
|
|
|
2023-04-12 11:46:49 -04:00
|
|
|
impl<'s> Source<'s> for Paragraph<'s> {
|
|
|
|
fn get_source(&'s self) -> &'s str {
|
2023-04-12 13:09:44 -04:00
|
|
|
self.source
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'s> Source<'s> for PlainList<'s> {
|
|
|
|
fn get_source(&'s self) -> &'s str {
|
2023-04-12 13:45:22 -04:00
|
|
|
self.source
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:07:33 -04:00
|
|
|
impl<'s> Source<'s> for PlainListItem<'s> {
|
|
|
|
fn get_source(&'s self) -> &'s str {
|
|
|
|
self.source
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 13:45:22 -04:00
|
|
|
impl<'s> Source<'s> for GreaterBlock<'s> {
|
|
|
|
fn get_source(&'s self) -> &'s str {
|
|
|
|
self.source
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'s> Source<'s> for FootnoteDefinition<'s> {
|
|
|
|
fn get_source(&'s self) -> &'s str {
|
2023-04-12 11:46:49 -04:00
|
|
|
self.source
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-27 18:08:17 -04:00
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
2023-03-24 17:00:27 -04:00
|
|
|
pub fn element<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Element<'s>> {
|
2023-03-27 13:06:41 -04:00
|
|
|
let non_paragraph_matcher = parser_with_context!(non_paragraph_element)(context);
|
2023-03-25 11:22:59 -04:00
|
|
|
let paragraph_matcher = parser_with_context!(paragraph)(context);
|
|
|
|
|
2023-03-27 13:06:41 -04:00
|
|
|
alt((
|
|
|
|
non_paragraph_matcher,
|
|
|
|
map(paragraph_matcher, Element::Paragraph),
|
|
|
|
))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn non_paragraph_element<'r, 's>(
|
|
|
|
context: Context<'r, 's>,
|
|
|
|
input: &'s str,
|
|
|
|
) -> Res<&'s str, Element<'s>> {
|
|
|
|
let plain_list_matcher = parser_with_context!(plain_list)(context);
|
2023-04-03 18:33:07 -04:00
|
|
|
let greater_block_matcher = parser_with_context!(greater_block)(context);
|
2023-04-07 17:14:44 -04:00
|
|
|
let footnote_definition_matcher = parser_with_context!(footnote_definition)(context);
|
2023-04-03 18:33:07 -04:00
|
|
|
alt((
|
|
|
|
map(plain_list_matcher, Element::PlainList),
|
|
|
|
map(greater_block_matcher, Element::GreaterBlock),
|
2023-04-07 17:14:44 -04:00
|
|
|
map(footnote_definition_matcher, Element::FootnoteDefinition),
|
2023-04-03 18:33:07 -04:00
|
|
|
))(input)
|
2023-03-25 11:22:59 -04:00
|
|
|
}
|