Avoid closures for the intermediate macro.

This commit is contained in:
Tom Alexander 2023-10-29 18:35:42 -04:00
parent 3d44d20384
commit 24b9782146
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
16 changed files with 40 additions and 51 deletions

View File

@ -7,7 +7,7 @@ pub(crate) struct IEntity {
pub(crate) html: String,
}
intermediate!(IEntity, Entity, |registry, original| async {
intermediate!(IEntity, Entity, original, registry, {
Ok(IEntity {
html: original.html.to_owned(),
})

View File

@ -9,7 +9,9 @@ pub(crate) struct IFootnoteDefinition {}
intermediate!(
IFootnoteDefinition,
FootnoteDefinition,
|registry, original| async {
original,
registry,
{
registry
.register_footnote_definition(original.label, &original.children)
.await?;

View File

@ -8,19 +8,15 @@ pub(crate) struct IFootnoteReference {
duplicate_offset: usize,
}
intermediate!(
IFootnoteReference,
FootnoteReference,
|registry, original| async {
let footnote_id = registry
.get_footnote_reference_id(original.label, &original.definition)
.await?;
Ok(IFootnoteReference {
footnote_id,
duplicate_offset: 0, // TODO
})
}
);
intermediate!(IFootnoteReference, FootnoteReference, original, registry, {
let footnote_id = registry
.get_footnote_reference_id(original.label, &original.definition)
.await?;
Ok(IFootnoteReference {
footnote_id,
duplicate_offset: 0, // TODO
})
});
impl IFootnoteReference {
pub(crate) fn get_display_label(&self) -> String {

View File

@ -11,7 +11,7 @@ pub(crate) struct IHeading {
pub(crate) children: Vec<IDocumentElement>,
}
intermediate!(IHeading, Heading, |registry, original| async {
intermediate!(IHeading, Heading, original, registry, {
let title = {
let mut ret = Vec::new();
for obj in original.title.iter() {

View File

@ -7,12 +7,8 @@ pub(crate) struct IInlineSourceBlock {
pub(crate) value: String,
}
intermediate!(
IInlineSourceBlock,
InlineSourceBlock,
|registry, original| async {
Ok(IInlineSourceBlock {
value: original.value.to_owned(),
})
}
);
intermediate!(IInlineSourceBlock, InlineSourceBlock, original, registry, {
Ok(IInlineSourceBlock {
value: original.value.to_owned(),
})
});

View File

@ -23,13 +23,15 @@ pub(crate) use inoop;
///
/// This exists to make changing the type signature easier.
macro_rules! intermediate {
($istruct:ident, $pstruct:ident, $fnbody:expr) => {
($istruct:ident, $pstruct:ident, $original:ident, $registry:ident, $fnbody:tt) => {
impl $istruct {
pub(crate) async fn new<'reg, 'orig, 'parse>(
registry: &'reg mut Registry<'orig, 'parse>,
original: &'orig organic::types::$pstruct<'parse>,
) -> Result<$istruct, CustomError> {
$fnbody(registry, original).await
let $original = original;
let $registry = registry;
$fnbody
}
}
};

View File

@ -8,7 +8,7 @@ pub(crate) struct IParagraph {
pub(crate) children: Vec<IObject>,
}
intermediate!(IParagraph, Paragraph, |registry, original| async {
intermediate!(IParagraph, Paragraph, original, registry, {
let children = {
let mut ret = Vec::new();
for obj in original.children.iter() {

View File

@ -9,7 +9,7 @@ pub(crate) struct IPlainList {
pub(crate) children: Vec<IPlainListItem>,
}
intermediate!(IPlainList, PlainList, |registry, original| async {
intermediate!(IPlainList, PlainList, original, registry, {
let children = {
let mut ret = Vec::new();
for obj in original.children.iter() {

View File

@ -10,7 +10,7 @@ pub(crate) struct IPlainListItem {
pub(crate) children: Vec<IElement>,
}
intermediate!(IPlainListItem, PlainListItem, |registry, original| async {
intermediate!(IPlainListItem, PlainListItem, original, registry, {
let tag = {
let mut ret = Vec::new();
for obj in original.tag.iter() {

View File

@ -8,7 +8,7 @@ pub(crate) struct IPlainText {
pub(crate) source: String,
}
intermediate!(IPlainText, PlainText, |registry, original| async {
intermediate!(IPlainText, PlainText, original, registry, {
Ok(IPlainText {
source: coalesce_whitespace(original.source).into_owned(),
})

View File

@ -8,7 +8,7 @@ pub(crate) struct IQuoteBlock {
pub(crate) children: Vec<IElement>,
}
intermediate!(IQuoteBlock, QuoteBlock, |registry, original| async {
intermediate!(IQuoteBlock, QuoteBlock, original, registry, {
let children = {
let mut ret = Vec::new();
for obj in original.children.iter() {

View File

@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::marker::PhantomData;
use super::ast_node::IntoIAstNode;
use crate::error::CustomError;
@ -12,10 +13,8 @@ type IdCounter = u16;
pub(crate) struct Registry<'orig, 'parse> {
id_counter: IdCounter,
targets: HashMap<&'parse str, String>,
footnote_ids: Vec<(
Option<&'parse str>,
FootnoteDefinitionContents<'orig, 'parse>,
)>,
footnote_ids: Vec<(Option<&'parse str>, Vec<IAstNode>)>,
_phantom: PhantomData<&'orig ()>,
}
impl<'orig, 'parse> Registry<'orig, 'parse> {
@ -24,6 +23,7 @@ impl<'orig, 'parse> Registry<'orig, 'parse> {
id_counter: 0,
targets: HashMap::new(),
footnote_ids: Vec::new(),
_phantom: PhantomData,
}
}
@ -35,12 +35,10 @@ impl<'orig, 'parse> Registry<'orig, 'parse> {
}
pub(crate) fn get_footnote_ids(&self) -> impl Iterator<Item = (usize, &Vec<IAstNode>)> {
// TODO
std::iter::empty()
// self.footnote_ids
// .iter()
// .map(|(_label, definition)| definition)
// .enumerate()
self.footnote_ids
.iter()
.map(|(_label, definition)| definition)
.enumerate()
}
/// Get a 0-indexed ID for a footnote.
@ -126,8 +124,3 @@ async fn convert_definition_contents<'reg, 'orig, 'parse>(
Ok(contents)
}
enum FootnoteDefinitionContents<'orig, 'parse> {
FromReference(&'orig Vec<Object<'parse>>),
FromDefinition(&'orig Vec<Object<'parse>>),
}

View File

@ -9,7 +9,7 @@ pub(crate) struct IRegularLink {
pub(crate) children: Vec<IObject>,
}
intermediate!(IRegularLink, RegularLink, |registry, original| async {
intermediate!(IRegularLink, RegularLink, original, registry, {
let children = {
let mut ret = Vec::new();
for obj in original.children.iter() {

View File

@ -8,7 +8,7 @@ pub(crate) struct ISection {
pub(crate) children: Vec<IElement>,
}
intermediate!(ISection, Section, |registry, original| async {
intermediate!(ISection, Section, original, registry, {
let children = {
let mut ret = Vec::new();
for elem in original.children.iter() {

View File

@ -7,7 +7,7 @@ pub(crate) struct ISrcBlock {
pub(crate) lines: Vec<String>,
}
intermediate!(ISrcBlock, SrcBlock, |registry, original| async {
intermediate!(ISrcBlock, SrcBlock, original, registry, {
let lines = original
.contents
.split_inclusive('\n')

View File

@ -8,7 +8,7 @@ pub(crate) struct ITarget {
value: String,
}
intermediate!(ITarget, Target, |registry, original| async {
intermediate!(ITarget, Target, original, registry, {
let id = registry.get_target(original.value);
Ok(ITarget {
id: id.clone(),