The object parsers separated out.

This commit is contained in:
Tom Alexander 2023-04-22 20:48:01 -04:00
parent d2fc8a513f
commit 036f4add4a
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 186 additions and 38 deletions

View File

@ -917,78 +917,138 @@ fn compare_plain_text<'s>(
}
fn compare_bold<'s>(
_source: &'s str,
source: &'s str,
emacs: &'s Token<'s>,
rust: &'s Bold<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
let mut this_status = DiffStatus::Good;
let emacs_name = "bold";
if assert_name(emacs, emacs_name).is_err() {
this_status = DiffStatus::Bad;
}
if assert_bounds(source, emacs, rust).is_err() {
this_status = DiffStatus::Bad;
}
Ok(DiffResult {
status: DiffStatus::Good,
name: "bold".to_owned(),
status: this_status,
name: emacs_name.to_owned(),
message: None,
children: Vec::new(),
})
}
fn compare_italic<'s>(
_source: &'s str,
source: &'s str,
emacs: &'s Token<'s>,
rust: &'s Italic<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
let mut this_status = DiffStatus::Good;
let emacs_name = "italic";
if assert_name(emacs, emacs_name).is_err() {
this_status = DiffStatus::Bad;
}
if assert_bounds(source, emacs, rust).is_err() {
this_status = DiffStatus::Bad;
}
Ok(DiffResult {
status: DiffStatus::Good,
name: "italic".to_owned(),
status: this_status,
name: emacs_name.to_owned(),
message: None,
children: Vec::new(),
})
}
fn compare_underline<'s>(
_source: &'s str,
source: &'s str,
emacs: &'s Token<'s>,
rust: &'s Underline<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
let mut this_status = DiffStatus::Good;
let emacs_name = "underline";
if assert_name(emacs, emacs_name).is_err() {
this_status = DiffStatus::Bad;
}
if assert_bounds(source, emacs, rust).is_err() {
this_status = DiffStatus::Bad;
}
Ok(DiffResult {
status: DiffStatus::Good,
name: "underline".to_owned(),
status: this_status,
name: emacs_name.to_owned(),
message: None,
children: Vec::new(),
})
}
fn compare_verbatim<'s>(
_source: &'s str,
source: &'s str,
emacs: &'s Token<'s>,
rust: &'s Verbatim<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
let mut this_status = DiffStatus::Good;
let emacs_name = "verbatim";
if assert_name(emacs, emacs_name).is_err() {
this_status = DiffStatus::Bad;
}
if assert_bounds(source, emacs, rust).is_err() {
this_status = DiffStatus::Bad;
}
Ok(DiffResult {
status: DiffStatus::Good,
name: "verbatim".to_owned(),
status: this_status,
name: emacs_name.to_owned(),
message: None,
children: Vec::new(),
})
}
fn compare_code<'s>(
_source: &'s str,
source: &'s str,
emacs: &'s Token<'s>,
rust: &'s Code<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
let mut this_status = DiffStatus::Good;
let emacs_name = "code";
if assert_name(emacs, emacs_name).is_err() {
this_status = DiffStatus::Bad;
}
if assert_bounds(source, emacs, rust).is_err() {
this_status = DiffStatus::Bad;
}
Ok(DiffResult {
status: DiffStatus::Good,
name: "code".to_owned(),
status: this_status,
name: emacs_name.to_owned(),
message: None,
children: Vec::new(),
})
}
fn compare_strike_through<'s>(
_source: &'s str,
source: &'s str,
emacs: &'s Token<'s>,
rust: &'s StrikeThrough<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
let mut this_status = DiffStatus::Good;
let emacs_name = "strike-through";
if assert_name(emacs, emacs_name).is_err() {
this_status = DiffStatus::Bad;
}
if assert_bounds(source, emacs, rust).is_err() {
this_status = DiffStatus::Bad;
}
Ok(DiffResult {
status: DiffStatus::Good,
name: "strike-through".to_owned(),
status: this_status,
name: emacs_name.to_owned(),
message: None,
children: Vec::new(),
})
@ -996,8 +1056,8 @@ fn compare_strike_through<'s>(
fn compare_regular_link<'s>(
_source: &'s str,
emacs: &'s Token<'s>,
rust: &'s RegularLink<'s>,
_emacs: &'s Token<'s>,
_rust: &'s RegularLink<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
Ok(DiffResult {
status: DiffStatus::Good,

View File

@ -75,3 +75,39 @@ impl<'s> Source<'s> for Object<'s> {
}
}
}
impl<'s> Source<'s> for Bold<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}
impl<'s> Source<'s> for Italic<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}
impl<'s> Source<'s> for Underline<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}
impl<'s> Source<'s> for StrikeThrough<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}
impl<'s> Source<'s> for Code<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}
impl<'s> Source<'s> for Verbatim<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}

View File

@ -18,10 +18,7 @@ pub fn standard_set_object<'r, 's>(
not(|i| context.check_exit_matcher(i))(input)?;
alt((
map(
parser_with_context!(text_markup)(context),
Object::TextMarkup,
),
parser_with_context!(text_markup)(context),
map(parser_with_context!(plain_text)(context), Object::PlainText),
))(input)
}
@ -35,10 +32,7 @@ pub fn minimal_set_object<'r, 's>(
not(|i| context.check_exit_matcher(i))(input)?;
alt((
map(
parser_with_context!(text_markup)(context),
Object::TextMarkup,
),
parser_with_context!(text_markup)(context),
map(parser_with_context!(plain_text)(context), Object::PlainText),
))(input)
}
@ -49,8 +43,5 @@ pub fn any_object_except_plain_text<'r, 's>(
input: &'s str,
) -> Res<&'s str, Object<'s>> {
// Used for exit matchers so this does not check exit matcher condition.
alt((map(
parser_with_context!(text_markup)(context),
Object::TextMarkup,
),))(input)
alt((parser_with_context!(text_markup)(context),))(input)
}

View File

@ -3,6 +3,7 @@ use nom::bytes::complete::tag;
use nom::character::complete::line_ending;
use nom::character::complete::one_of;
use nom::character::complete::space0;
use nom::combinator::map;
use nom::combinator::peek;
use nom::combinator::recognize;
use nom::combinator::verify;
@ -21,12 +22,75 @@ 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::Bold;
use crate::parser::Italic;
use crate::parser::Object;
use crate::parser::StrikeThrough;
use crate::parser::Underline;
#[tracing::instrument(ret, level = "debug")]
pub fn text_markup<'r, 's>(
pub fn text_markup<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Object<'s>> {
alt((
map(parser_with_context!(bold)(context), Object::Bold),
map(parser_with_context!(italic)(context), Object::Italic),
map(parser_with_context!(underline)(context), Object::Underline),
map(
parser_with_context!(strike_through)(context),
Object::StrikeThrough,
),
// map(parser_with_context!(verbatim)(context), Object::Verbatim),
// map(parser_with_context!(code)(context), Object::Code),
))(input)
}
#[tracing::instrument(ret, level = "debug")]
pub fn bold<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Bold<'s>> {
let text_markup_object_specialized = text_markup_object("*");
let (remaining, children) = text_markup_object_specialized(context, input)?;
let source = get_consumed(input, remaining);
Ok((remaining, Bold { source, children }))
}
#[tracing::instrument(ret, level = "debug")]
pub fn italic<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Italic<'s>> {
let text_markup_object_specialized = text_markup_object("/");
let (remaining, children) = text_markup_object_specialized(context, input)?;
let source = get_consumed(input, remaining);
Ok((remaining, Italic { source, children }))
}
#[tracing::instrument(ret, level = "debug")]
pub fn underline<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Underline<'s>> {
let text_markup_object_specialized = text_markup_object("_");
let (remaining, children) = text_markup_object_specialized(context, input)?;
let source = get_consumed(input, remaining);
Ok((remaining, Underline { source, children }))
}
#[tracing::instrument(ret, level = "debug")]
pub fn strike_through<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, TextMarkupObject<'s>> {
) -> Res<&'s str, StrikeThrough<'s>> {
let text_markup_object_specialized = text_markup_object("+");
let (remaining, children) = text_markup_object_specialized(context, input)?;
let source = get_consumed(input, remaining);
Ok((remaining, StrikeThrough { source, children }))
}
fn text_markup_object(
marker_symbol: &str,
) -> impl for<'r, 's> Fn(Context<'r, 's>, &'s str) -> Res<&'s str, Vec<Object<'s>>> {
let marker_symbol = marker_symbol.to_owned();
move |context: Context, input: &str| _text_markup_object(context, input, marker_symbol.as_str())
}
#[tracing::instrument(ret, level = "debug")]
fn _text_markup_object<'r, 's, 'x>(
context: Context<'r, 's>,
input: &'s str,
marker_symbol: &'x str,
) -> Res<&'s str, Vec<Object<'s>>> {
let (remaining, _) = pre(context, input)?;
let (remaining, open) = marker(remaining)?;
let text_markup_end_specialized = text_markup_end(open);
@ -47,10 +111,7 @@ pub fn text_markup<'r, 's>(
// TODO: Sometimes its plain text, not objects
let (remaining, _close) = text_markup_end_specialized(context, remaining)?;
let (remaining, _trailing_whitespace) = space0(remaining)?;
let source = get_consumed(input, remaining);
Ok((remaining, TextMarkupObject { source, children }))
Ok((remaining, children))
}
#[tracing::instrument(ret, level = "debug")]