organic/src/parser/element.rs
2023-04-15 17:36:07 -04:00

114 lines
3.2 KiB
Rust

use super::comment::comment;
use super::drawer::drawer;
use super::error::Res;
use super::footnote_definition::footnote_definition;
use super::greater_block::greater_block;
use super::greater_element::FootnoteDefinition;
use super::greater_element::GreaterBlock;
use super::greater_element::PlainList;
use super::lesser_element::Comment;
use super::lesser_element::Paragraph;
use super::paragraph::paragraph;
use super::plain_list::plain_list;
use super::source::Source;
use super::Context;
use super::Drawer;
use super::PlainListItem;
use crate::parser::parser_with_context::parser_with_context;
use nom::branch::alt;
use nom::combinator::map;
#[derive(Debug)]
pub enum Element<'s> {
Paragraph(Paragraph<'s>),
PlainList(PlainList<'s>),
GreaterBlock(GreaterBlock<'s>),
FootnoteDefinition(FootnoteDefinition<'s>),
Comment(Comment<'s>),
Drawer(Drawer<'s>),
}
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,
Element::GreaterBlock(obj) => obj.source,
Element::FootnoteDefinition(obj) => obj.source,
Element::Comment(obj) => obj.source,
Element::Drawer(obj) => obj.source,
}
}
}
impl<'s> Source<'s> for Paragraph<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}
impl<'s> Source<'s> for PlainList<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}
impl<'s> Source<'s> for PlainListItem<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}
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 {
self.source
}
}
impl<'s> Source<'s> for Comment<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}
impl<'s> Source<'s> for Drawer<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}
#[tracing::instrument(ret, level = "debug")]
pub fn element<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Element<'s>> {
let non_paragraph_matcher = parser_with_context!(non_paragraph_element)(context);
let paragraph_matcher = parser_with_context!(paragraph)(context);
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);
let greater_block_matcher = parser_with_context!(greater_block)(context);
let footnote_definition_matcher = parser_with_context!(footnote_definition)(context);
let comment_matcher = parser_with_context!(comment)(context);
let drawer_matcher = parser_with_context!(drawer)(context);
alt((
map(plain_list_matcher, Element::PlainList),
map(greater_block_matcher, Element::GreaterBlock),
map(footnote_definition_matcher, Element::FootnoteDefinition),
map(comment_matcher, Element::Comment),
map(drawer_matcher, Element::Drawer),
))(input)
}