Separate out the text markup types into their own types.

This commit is contained in:
Tom Alexander 2023-04-22 20:22:07 -04:00
parent 401fb339d0
commit d2fc8a513f
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 131 additions and 13 deletions

View File

@ -1,7 +1,9 @@
use super::util::assert_bounds;
use super::util::assert_name;
use crate::parser::sexp::Token;
use crate::parser::Bold;
use crate::parser::Clock;
use crate::parser::Code;
use crate::parser::Comment;
use crate::parser::CommentBlock;
use crate::parser::DiarySexp;
@ -17,6 +19,7 @@ use crate::parser::FootnoteDefinition;
use crate::parser::GreaterBlock;
use crate::parser::Heading;
use crate::parser::HorizontalRule;
use crate::parser::Italic;
use crate::parser::Keyword;
use crate::parser::LatexEnvironment;
use crate::parser::Object;
@ -29,10 +32,12 @@ use crate::parser::PropertyDrawer;
use crate::parser::RegularLink;
use crate::parser::Section;
use crate::parser::SrcBlock;
use crate::parser::StrikeThrough;
use crate::parser::Table;
use crate::parser::TableCell;
use crate::parser::TableRow;
use crate::parser::TextMarkup;
use crate::parser::Underline;
use crate::parser::Verbatim;
use crate::parser::VerseBlock;
#[derive(Debug)]
@ -131,7 +136,12 @@ fn compare_object<'s>(
rust: &'s Object<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
match rust {
Object::TextMarkup(obj) => compare_text_markup(source, emacs, obj),
Object::Bold(obj) => compare_bold(source, emacs, obj),
Object::Italic(obj) => compare_italic(source, emacs, obj),
Object::Underline(obj) => compare_underline(source, emacs, obj),
Object::Verbatim(obj) => compare_verbatim(source, emacs, obj),
Object::Code(obj) => compare_code(source, emacs, obj),
Object::StrikeThrough(obj) => compare_strike_through(source, emacs, obj),
Object::PlainText(obj) => compare_plain_text(source, emacs, obj),
Object::RegularLink(obj) => compare_regular_link(source, emacs, obj),
}
@ -906,14 +916,79 @@ fn compare_plain_text<'s>(
})
}
fn compare_text_markup<'s>(
fn compare_bold<'s>(
_source: &'s str,
emacs: &'s Token<'s>,
rust: &'s TextMarkup<'s>,
rust: &'s Bold<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
Ok(DiffResult {
status: DiffStatus::Good,
name: "text-markup".to_owned(),
name: "bold".to_owned(),
message: None,
children: Vec::new(),
})
}
fn compare_italic<'s>(
_source: &'s str,
emacs: &'s Token<'s>,
rust: &'s Italic<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
Ok(DiffResult {
status: DiffStatus::Good,
name: "italic".to_owned(),
message: None,
children: Vec::new(),
})
}
fn compare_underline<'s>(
_source: &'s str,
emacs: &'s Token<'s>,
rust: &'s Underline<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
Ok(DiffResult {
status: DiffStatus::Good,
name: "underline".to_owned(),
message: None,
children: Vec::new(),
})
}
fn compare_verbatim<'s>(
_source: &'s str,
emacs: &'s Token<'s>,
rust: &'s Verbatim<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
Ok(DiffResult {
status: DiffStatus::Good,
name: "verbatim".to_owned(),
message: None,
children: Vec::new(),
})
}
fn compare_code<'s>(
_source: &'s str,
emacs: &'s Token<'s>,
rust: &'s Code<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
Ok(DiffResult {
status: DiffStatus::Good,
name: "code".to_owned(),
message: None,
children: Vec::new(),
})
}
fn compare_strike_through<'s>(
_source: &'s str,
emacs: &'s Token<'s>,
rust: &'s StrikeThrough<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
Ok(DiffResult {
status: DiffStatus::Good,
name: "strike-through".to_owned(),
message: None,
children: Vec::new(),
})

View File

@ -61,9 +61,14 @@ pub use lesser_element::Planning;
pub use lesser_element::SrcBlock;
pub use lesser_element::TableCell;
pub use lesser_element::VerseBlock;
pub use object::Bold;
pub use object::Code;
pub use object::Italic;
pub use object::Object;
pub use object::PlainText;
pub use object::RegularLink;
pub use object::TextMarkup;
pub use object::StrikeThrough;
pub use object::Underline;
pub use object::Verbatim;
pub use source::Source;
type Context<'r, 's> = &'r parser_context::ContextTree<'r, 's>;

View File

@ -2,8 +2,12 @@ use super::source::Source;
#[derive(Debug)]
pub enum Object<'s> {
#[allow(dead_code)]
TextMarkup(TextMarkup<'s>),
Bold(Bold<'s>),
Italic(Italic<'s>),
Underline(Underline<'s>),
StrikeThrough(StrikeThrough<'s>),
Code(Code<'s>),
Verbatim(Verbatim<'s>),
PlainText(PlainText<'s>),
@ -12,11 +16,41 @@ pub enum Object<'s> {
}
#[derive(Debug)]
pub struct TextMarkup<'s> {
pub struct Bold<'s> {
pub source: &'s str,
pub children: Vec<Object<'s>>,
}
#[derive(Debug)]
pub struct Italic<'s> {
pub source: &'s str,
pub children: Vec<Object<'s>>,
}
#[derive(Debug)]
pub struct Underline<'s> {
pub source: &'s str,
pub children: Vec<Object<'s>>,
}
#[derive(Debug)]
pub struct StrikeThrough<'s> {
pub source: &'s str,
pub children: Vec<Object<'s>>,
}
#[derive(Debug)]
pub struct Code<'s> {
pub source: &'s str,
pub contents: &'s str,
}
#[derive(Debug)]
pub struct Verbatim<'s> {
pub source: &'s str,
pub contents: &'s str,
}
#[derive(Debug)]
pub struct PlainText<'s> {
pub source: &'s str,
@ -30,7 +64,12 @@ pub struct RegularLink<'s> {
impl<'s> Source<'s> for Object<'s> {
fn get_source(&'s self) -> &'s str {
match self {
Object::TextMarkup(obj) => obj.source,
Object::Bold(obj) => obj.source,
Object::Italic(obj) => obj.source,
Object::Underline(obj) => obj.source,
Object::StrikeThrough(obj) => obj.source,
Object::Code(obj) => obj.source,
Object::Verbatim(obj) => obj.source,
Object::PlainText(obj) => obj.source,
Object::RegularLink(obj) => obj.source,
}

View File

@ -21,13 +21,12 @@ use crate::parser::parser_with_context::parser_with_context;
use crate::parser::util::exit_matcher_parser;
use crate::parser::util::get_consumed;
use crate::parser::util::get_one_before;
use crate::parser::TextMarkup;
#[tracing::instrument(ret, level = "debug")]
pub fn text_markup<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, TextMarkup<'s>> {
) -> Res<&'s str, TextMarkupObject<'s>> {
let (remaining, _) = pre(context, input)?;
let (remaining, open) = marker(remaining)?;
let text_markup_end_specialized = text_markup_end(open);
@ -51,7 +50,7 @@ pub fn text_markup<'r, 's>(
let source = get_consumed(input, remaining);
Ok((remaining, TextMarkup { source, children }))
Ok((remaining, TextMarkupObject { source, children }))
}
#[tracing::instrument(ret, level = "debug")]