diff --git a/src/intermediate/angle_link.rs b/src/intermediate/angle_link.rs index 888d339..736def0 100644 --- a/src/intermediate/angle_link.rs +++ b/src/intermediate/angle_link.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IAngleLink {} - -impl IAngleLink { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::AngleLink<'parse>, - ) -> Result { - Ok(IAngleLink {}) - } -} +inoop!(IAngleLink, AngleLink); diff --git a/src/intermediate/ast_node.rs b/src/intermediate/ast_node.rs index 189bc7b..0958180 100644 --- a/src/intermediate/ast_node.rs +++ b/src/intermediate/ast_node.rs @@ -113,19 +113,19 @@ pub(crate) enum IAstNode { } pub(crate) trait IntoIAstNode { - fn into_ast_node<'parse, 'b>( - &'b self, - registry: &'b mut Registry<'parse>, - ) -> BoxFuture<'b, Result> + fn into_ast_node<'reg, 'orig, 'parse, 'inp: 'reg + 'orig>( + &'orig self, + registry: &'reg mut Registry<'orig, 'parse>, + ) -> BoxFuture<'inp, Result> where Self: 'parse; } impl IntoIAstNode for organic::types::DocumentElement<'_> { - fn into_ast_node<'parse, 'b>( - &'b self, - registry: &'b mut Registry<'parse>, - ) -> BoxFuture<'b, Result> + fn into_ast_node<'reg, 'orig, 'parse, 'inp: 'reg + 'orig>( + &'orig self, + registry: &'reg mut Registry<'orig, 'parse>, + ) -> BoxFuture<'inp, Result> where Self: 'parse, { @@ -144,10 +144,10 @@ impl IntoIAstNode for organic::types::DocumentElement<'_> { } impl IntoIAstNode for organic::types::Element<'_> { - fn into_ast_node<'parse, 'b>( - &'b self, - registry: &'b mut Registry<'parse>, - ) -> BoxFuture<'b, Result> + fn into_ast_node<'reg, 'orig, 'parse, 'inp: 'reg + 'orig>( + &'orig self, + registry: &'reg mut Registry<'orig, 'parse>, + ) -> BoxFuture<'inp, Result> where Self: 'parse, { @@ -232,10 +232,10 @@ impl IntoIAstNode for organic::types::Element<'_> { } impl IntoIAstNode for organic::types::Object<'_> { - fn into_ast_node<'parse, 'b>( - &'b self, - registry: &'b mut Registry<'parse>, - ) -> BoxFuture<'b, Result> + fn into_ast_node<'reg, 'orig, 'parse, 'inp: 'reg + 'orig>( + &'orig self, + registry: &'reg mut Registry<'orig, 'parse>, + ) -> BoxFuture<'inp, Result> where Self: 'parse, { diff --git a/src/intermediate/babel_call.rs b/src/intermediate/babel_call.rs index 957bd11..cffc2e5 100644 --- a/src/intermediate/babel_call.rs +++ b/src/intermediate/babel_call.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IBabelCall {} - -impl IBabelCall { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::BabelCall<'parse>, - ) -> Result { - Ok(IBabelCall {}) - } -} +inoop!(IBabelCall, BabelCall); diff --git a/src/intermediate/bold.rs b/src/intermediate/bold.rs index f69cbe7..bee42d1 100644 --- a/src/intermediate/bold.rs +++ b/src/intermediate/bold.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IBold {} - -impl IBold { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::Bold<'parse>, - ) -> Result { - Ok(IBold {}) - } -} +inoop!(IBold, Bold); diff --git a/src/intermediate/center_block.rs b/src/intermediate/center_block.rs index bc37461..3b93a29 100644 --- a/src/intermediate/center_block.rs +++ b/src/intermediate/center_block.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct ICenterBlock {} - -impl ICenterBlock { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::CenterBlock<'parse>, - ) -> Result { - Ok(ICenterBlock {}) - } -} +inoop!(ICenterBlock, CenterBlock); diff --git a/src/intermediate/citation.rs b/src/intermediate/citation.rs index b9b5176..916ff72 100644 --- a/src/intermediate/citation.rs +++ b/src/intermediate/citation.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct ICitation {} - -impl ICitation { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::Citation<'parse>, - ) -> Result { - Ok(ICitation {}) - } -} +inoop!(ICitation, Citation); diff --git a/src/intermediate/citation_reference.rs b/src/intermediate/citation_reference.rs index ce46683..f7b2e9b 100644 --- a/src/intermediate/citation_reference.rs +++ b/src/intermediate/citation_reference.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct ICitationReference {} - -impl ICitationReference { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::CitationReference<'parse>, - ) -> Result { - Ok(ICitationReference {}) - } -} +inoop!(ICitationReference, CitationReference); diff --git a/src/intermediate/clock.rs b/src/intermediate/clock.rs index 30626f0..056d582 100644 --- a/src/intermediate/clock.rs +++ b/src/intermediate/clock.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IClock {} - -impl IClock { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::Clock<'parse>, - ) -> Result { - Ok(IClock {}) - } -} +inoop!(IClock, Clock); diff --git a/src/intermediate/code.rs b/src/intermediate/code.rs index 634e5f2..01d7dbe 100644 --- a/src/intermediate/code.rs +++ b/src/intermediate/code.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct ICode {} - -impl ICode { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::Code<'parse>, - ) -> Result { - Ok(ICode {}) - } -} +inoop!(ICode, Code); diff --git a/src/intermediate/comment.rs b/src/intermediate/comment.rs index 75b8cf8..19c706a 100644 --- a/src/intermediate/comment.rs +++ b/src/intermediate/comment.rs @@ -1,16 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -/// Essentially a no-op since the comment is not rendered. -#[derive(Debug, Clone)] -pub(crate) struct IComment {} - -impl IComment { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - comment: &'b organic::types::Comment<'parse>, - ) -> Result { - Ok(IComment {}) - } -} +inoop!(IComment, Comment); diff --git a/src/intermediate/comment_block.rs b/src/intermediate/comment_block.rs index 2560408..89f4bc3 100644 --- a/src/intermediate/comment_block.rs +++ b/src/intermediate/comment_block.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct ICommentBlock {} - -impl ICommentBlock { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::CommentBlock<'parse>, - ) -> Result { - Ok(ICommentBlock {}) - } -} +inoop!(ICommentBlock, CommentBlock); diff --git a/src/intermediate/diary_sexp.rs b/src/intermediate/diary_sexp.rs index 3a65fbf..ebc7b3b 100644 --- a/src/intermediate/diary_sexp.rs +++ b/src/intermediate/diary_sexp.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IDiarySexp {} - -impl IDiarySexp { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::DiarySexp<'parse>, - ) -> Result { - Ok(IDiarySexp {}) - } -} +inoop!(IDiarySexp, DiarySexp); diff --git a/src/intermediate/document_element.rs b/src/intermediate/document_element.rs index 2e0591e..205ae5a 100644 --- a/src/intermediate/document_element.rs +++ b/src/intermediate/document_element.rs @@ -1,8 +1,8 @@ -use crate::error::CustomError; - +use super::macros::iselector; use super::registry::Registry; use super::IHeading; use super::ISection; +use crate::error::CustomError; use futures::future::{BoxFuture, FutureExt}; #[derive(Debug, Clone)] @@ -11,21 +11,17 @@ pub(crate) enum IDocumentElement { Section(ISection), } -impl IDocumentElement { - pub(crate) fn new<'parse, 'b>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::DocumentElement<'parse>, - ) -> BoxFuture<'b, Result> { - async move { - match original { - organic::types::DocumentElement::Heading(inner) => Ok(IDocumentElement::Heading( - IHeading::new(registry, inner).await?, - )), - organic::types::DocumentElement::Section(inner) => Ok(IDocumentElement::Section( - ISection::new(registry, inner).await?, - )), - } +iselector!( + IDocumentElement, + DocumentElement, + |registry, original| async { + match &original { + organic::types::DocumentElement::Heading(inner) => Ok(IDocumentElement::Heading( + IHeading::new(registry, inner).await?, + )), + organic::types::DocumentElement::Section(inner) => Ok(IDocumentElement::Section( + ISection::new(registry, inner).await?, + )), } - .boxed() } -} +); diff --git a/src/intermediate/drawer.rs b/src/intermediate/drawer.rs index 6efaf1d..4a1c0a3 100644 --- a/src/intermediate/drawer.rs +++ b/src/intermediate/drawer.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IDrawer {} - -impl IDrawer { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::Drawer<'parse>, - ) -> Result { - Ok(IDrawer {}) - } -} +inoop!(IDrawer, Drawer); diff --git a/src/intermediate/dynamic_block.rs b/src/intermediate/dynamic_block.rs index 3f3f419..fc8cbaf 100644 --- a/src/intermediate/dynamic_block.rs +++ b/src/intermediate/dynamic_block.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IDynamicBlock {} - -impl IDynamicBlock { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::DynamicBlock<'parse>, - ) -> Result { - Ok(IDynamicBlock {}) - } -} +inoop!(IDynamicBlock, DynamicBlock); diff --git a/src/intermediate/element.rs b/src/intermediate/element.rs index 2deaf6a..40771d6 100644 --- a/src/intermediate/element.rs +++ b/src/intermediate/element.rs @@ -1,7 +1,6 @@ -use crate::error::CustomError; - use super::comment::IComment; use super::keyword::IKeyword; +use super::macros::iselector; use super::registry::Registry; use super::IBabelCall; use super::ICenterBlock; @@ -16,6 +15,7 @@ use super::IFixedWidthArea; use super::IFootnoteDefinition; use super::IHorizontalRule; use super::ILatexEnvironment; +use super::IObject; use super::IParagraph; use super::IPlainList; use super::IPlanning; @@ -25,6 +25,7 @@ use super::ISpecialBlock; use super::ISrcBlock; use super::ITable; use super::IVerseBlock; +use crate::error::CustomError; use futures::future::{BoxFuture, FutureExt}; #[derive(Debug, Clone)] @@ -55,87 +56,79 @@ pub(crate) enum IElement { LatexEnvironment(ILatexEnvironment), } -impl IElement { - pub(crate) fn new<'parse, 'b>( - registry: &'b mut Registry<'parse>, - elem: &'b organic::types::Element<'parse>, - ) -> BoxFuture<'b, Result> { - async move { - match elem { - organic::types::Element::Paragraph(inner) => { - Ok(IElement::Paragraph(IParagraph::new(registry, inner).await?)) - } - organic::types::Element::PlainList(inner) => { - Ok(IElement::PlainList(IPlainList::new(registry, inner).await?)) - } - organic::types::Element::CenterBlock(inner) => Ok(IElement::CenterBlock( - ICenterBlock::new(registry, inner).await?, - )), - organic::types::Element::QuoteBlock(inner) => Ok(IElement::QuoteBlock( - IQuoteBlock::new(registry, inner).await?, - )), - organic::types::Element::SpecialBlock(inner) => Ok(IElement::SpecialBlock( - ISpecialBlock::new(registry, inner).await?, - )), - organic::types::Element::DynamicBlock(inner) => Ok(IElement::DynamicBlock( - IDynamicBlock::new(registry, inner).await?, - )), - organic::types::Element::FootnoteDefinition(inner) => Ok( - IElement::FootnoteDefinition(IFootnoteDefinition::new(registry, inner).await?), - ), - organic::types::Element::Comment(inner) => { - Ok(IElement::Comment(IComment::new(registry, inner).await?)) - } - organic::types::Element::Drawer(inner) => { - Ok(IElement::Drawer(IDrawer::new(registry, inner).await?)) - } - organic::types::Element::PropertyDrawer(inner) => Ok(IElement::PropertyDrawer( - IPropertyDrawer::new(registry, inner).await?, - )), - organic::types::Element::Table(inner) => { - Ok(IElement::Table(ITable::new(registry, inner).await?)) - } - organic::types::Element::VerseBlock(inner) => Ok(IElement::VerseBlock( - IVerseBlock::new(registry, inner).await?, - )), - organic::types::Element::CommentBlock(inner) => Ok(IElement::CommentBlock( - ICommentBlock::new(registry, inner).await?, - )), - organic::types::Element::ExampleBlock(inner) => Ok(IElement::ExampleBlock( - IExampleBlock::new(registry, inner).await?, - )), - organic::types::Element::ExportBlock(inner) => Ok(IElement::ExportBlock( - IExportBlock::new(registry, inner).await?, - )), - organic::types::Element::SrcBlock(inner) => { - Ok(IElement::SrcBlock(ISrcBlock::new(registry, inner).await?)) - } - organic::types::Element::Clock(inner) => { - Ok(IElement::Clock(IClock::new(registry, inner).await?)) - } - organic::types::Element::DiarySexp(inner) => { - Ok(IElement::DiarySexp(IDiarySexp::new(registry, inner).await?)) - } - organic::types::Element::Planning(inner) => { - Ok(IElement::Planning(IPlanning::new(registry, inner).await?)) - } - organic::types::Element::FixedWidthArea(inner) => Ok(IElement::FixedWidthArea( - IFixedWidthArea::new(registry, inner).await?, - )), - organic::types::Element::HorizontalRule(inner) => Ok(IElement::HorizontalRule( - IHorizontalRule::new(registry, inner).await?, - )), - organic::types::Element::Keyword(inner) => { - Ok(IElement::Keyword(IKeyword::new(registry, inner).await?)) - } - organic::types::Element::BabelCall(inner) => { - Ok(IElement::BabelCall(IBabelCall::new(registry, inner).await?)) - } - organic::types::Element::LatexEnvironment(inner) => Ok(IElement::LatexEnvironment( - ILatexEnvironment::new(registry, inner).await?, - )), - } +iselector!(IElement, Element, |registry, elem| async { + match elem { + organic::types::Element::Paragraph(inner) => { + Ok(IElement::Paragraph(IParagraph::new(registry, inner).await?)) } - .boxed() + organic::types::Element::PlainList(inner) => { + Ok(IElement::PlainList(IPlainList::new(registry, inner).await?)) + } + organic::types::Element::CenterBlock(inner) => Ok(IElement::CenterBlock( + ICenterBlock::new(registry, inner).await?, + )), + organic::types::Element::QuoteBlock(inner) => Ok(IElement::QuoteBlock( + IQuoteBlock::new(registry, inner).await?, + )), + organic::types::Element::SpecialBlock(inner) => Ok(IElement::SpecialBlock( + ISpecialBlock::new(registry, inner).await?, + )), + organic::types::Element::DynamicBlock(inner) => Ok(IElement::DynamicBlock( + IDynamicBlock::new(registry, inner).await?, + )), + organic::types::Element::FootnoteDefinition(inner) => Ok(IElement::FootnoteDefinition( + IFootnoteDefinition::new(registry, inner).await?, + )), + organic::types::Element::Comment(inner) => { + Ok(IElement::Comment(IComment::new(registry, inner).await?)) + } + organic::types::Element::Drawer(inner) => { + Ok(IElement::Drawer(IDrawer::new(registry, inner).await?)) + } + organic::types::Element::PropertyDrawer(inner) => Ok(IElement::PropertyDrawer( + IPropertyDrawer::new(registry, inner).await?, + )), + organic::types::Element::Table(inner) => { + Ok(IElement::Table(ITable::new(registry, inner).await?)) + } + organic::types::Element::VerseBlock(inner) => Ok(IElement::VerseBlock( + IVerseBlock::new(registry, inner).await?, + )), + organic::types::Element::CommentBlock(inner) => Ok(IElement::CommentBlock( + ICommentBlock::new(registry, inner).await?, + )), + organic::types::Element::ExampleBlock(inner) => Ok(IElement::ExampleBlock( + IExampleBlock::new(registry, inner).await?, + )), + organic::types::Element::ExportBlock(inner) => Ok(IElement::ExportBlock( + IExportBlock::new(registry, inner).await?, + )), + organic::types::Element::SrcBlock(inner) => { + Ok(IElement::SrcBlock(ISrcBlock::new(registry, inner).await?)) + } + organic::types::Element::Clock(inner) => { + Ok(IElement::Clock(IClock::new(registry, inner).await?)) + } + organic::types::Element::DiarySexp(inner) => { + Ok(IElement::DiarySexp(IDiarySexp::new(registry, inner).await?)) + } + organic::types::Element::Planning(inner) => { + Ok(IElement::Planning(IPlanning::new(registry, inner).await?)) + } + organic::types::Element::FixedWidthArea(inner) => Ok(IElement::FixedWidthArea( + IFixedWidthArea::new(registry, inner).await?, + )), + organic::types::Element::HorizontalRule(inner) => Ok(IElement::HorizontalRule( + IHorizontalRule::new(registry, inner).await?, + )), + organic::types::Element::Keyword(inner) => { + Ok(IElement::Keyword(IKeyword::new(registry, inner).await?)) + } + organic::types::Element::BabelCall(inner) => { + Ok(IElement::BabelCall(IBabelCall::new(registry, inner).await?)) + } + organic::types::Element::LatexEnvironment(inner) => Ok(IElement::LatexEnvironment( + ILatexEnvironment::new(registry, inner).await?, + )), } -} +}); diff --git a/src/intermediate/entity.rs b/src/intermediate/entity.rs index 1323143..6b22e7b 100644 --- a/src/intermediate/entity.rs +++ b/src/intermediate/entity.rs @@ -1,19 +1,14 @@ -use crate::error::CustomError; - +use super::macros::intermediate; use super::registry::Registry; +use crate::error::CustomError; #[derive(Debug, Clone)] pub(crate) struct IEntity { pub(crate) html: String, } -impl IEntity { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::Entity<'parse>, - ) -> Result { - Ok(IEntity { - html: original.html.to_owned(), - }) - } -} +intermediate!(IEntity, Entity, |registry, original| async { + Ok(IEntity { + html: original.html.to_owned(), + }) +}); diff --git a/src/intermediate/example_block.rs b/src/intermediate/example_block.rs index 7ea73b3..953c4d5 100644 --- a/src/intermediate/example_block.rs +++ b/src/intermediate/example_block.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IExampleBlock {} - -impl IExampleBlock { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::ExampleBlock<'parse>, - ) -> Result { - Ok(IExampleBlock {}) - } -} +inoop!(IExampleBlock, ExampleBlock); diff --git a/src/intermediate/export_block.rs b/src/intermediate/export_block.rs index 86afacc..2792eda 100644 --- a/src/intermediate/export_block.rs +++ b/src/intermediate/export_block.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IExportBlock {} - -impl IExportBlock { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::ExportBlock<'parse>, - ) -> Result { - Ok(IExportBlock {}) - } -} +inoop!(IExportBlock, ExportBlock); diff --git a/src/intermediate/export_snippet.rs b/src/intermediate/export_snippet.rs index dd0bc5b..7f23280 100644 --- a/src/intermediate/export_snippet.rs +++ b/src/intermediate/export_snippet.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IExportSnippet {} - -impl IExportSnippet { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::ExportSnippet<'parse>, - ) -> Result { - Ok(IExportSnippet {}) - } -} +inoop!(IExportSnippet, ExportSnippet); diff --git a/src/intermediate/fixed_width_area.rs b/src/intermediate/fixed_width_area.rs index e3736c4..6bed813 100644 --- a/src/intermediate/fixed_width_area.rs +++ b/src/intermediate/fixed_width_area.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IFixedWidthArea {} - -impl IFixedWidthArea { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::FixedWidthArea<'parse>, - ) -> Result { - Ok(IFixedWidthArea {}) - } -} +inoop!(IFixedWidthArea, FixedWidthArea); diff --git a/src/intermediate/footnote_definition.rs b/src/intermediate/footnote_definition.rs index 011242e..1b51fd4 100644 --- a/src/intermediate/footnote_definition.rs +++ b/src/intermediate/footnote_definition.rs @@ -1,24 +1,21 @@ -use crate::error::CustomError; - -use super::ast_node::IAstNode; +use super::macros::intermediate; use super::registry::Registry; -use super::IElement; -use super::IObject; +use super::IAstNode; +use crate::error::CustomError; #[derive(Debug, Clone)] pub(crate) struct IFootnoteDefinition {} -impl IFootnoteDefinition { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::FootnoteDefinition<'parse>, - ) -> Result { +intermediate!( + IFootnoteDefinition, + FootnoteDefinition, + |registry, original| async { registry .register_footnote_definition(original.label, &original.children) .await?; Ok(IFootnoteDefinition {}) } -} +); #[derive(Debug)] pub(crate) struct IRealFootnoteDefinition { @@ -27,8 +24,8 @@ pub(crate) struct IRealFootnoteDefinition { } impl IRealFootnoteDefinition { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, + pub(crate) async fn new<'reg, 'orig, 'parse>( + registry: &'reg mut Registry<'orig, 'parse>, footnote_id: usize, contents: Vec, ) -> Result { diff --git a/src/intermediate/footnote_reference.rs b/src/intermediate/footnote_reference.rs index c04951a..d06ffa7 100644 --- a/src/intermediate/footnote_reference.rs +++ b/src/intermediate/footnote_reference.rs @@ -1,6 +1,6 @@ -use crate::error::CustomError; - +use super::macros::intermediate; use super::registry::Registry; +use crate::error::CustomError; #[derive(Debug, Clone)] pub(crate) struct IFootnoteReference { @@ -8,11 +8,10 @@ pub(crate) struct IFootnoteReference { duplicate_offset: usize, } -impl IFootnoteReference { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::FootnoteReference<'parse>, - ) -> Result { +intermediate!( + IFootnoteReference, + FootnoteReference, + |registry, original| async { let footnote_id = registry .get_footnote_reference_id(original.label, &original.definition) .await?; @@ -21,7 +20,9 @@ impl IFootnoteReference { duplicate_offset: 0, // TODO }) } +); +impl IFootnoteReference { pub(crate) fn get_display_label(&self) -> String { format!("{}", self.footnote_id + 1) } diff --git a/src/intermediate/heading.rs b/src/intermediate/heading.rs index a5e3e66..501b706 100644 --- a/src/intermediate/heading.rs +++ b/src/intermediate/heading.rs @@ -1,8 +1,8 @@ -use crate::error::CustomError; - +use super::macros::intermediate; use super::registry::Registry; use super::IDocumentElement; use super::IObject; +use crate::error::CustomError; #[derive(Debug, Clone)] pub(crate) struct IHeading { @@ -11,29 +11,24 @@ pub(crate) struct IHeading { pub(crate) children: Vec, } -impl IHeading { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::Heading<'parse>, - ) -> Result { - let title = { - let mut ret = Vec::new(); - for obj in original.title.iter() { - ret.push(IObject::new(registry, obj).await?); - } - ret - }; - let children = { - let mut ret = Vec::new(); - for obj in original.children.iter() { - ret.push(IDocumentElement::new(registry, obj).await?); - } - ret - }; - Ok(IHeading { - title, - level: original.level, - children, - }) - } -} +intermediate!(IHeading, Heading, |registry, original| async { + let title = { + let mut ret = Vec::new(); + for obj in original.title.iter() { + ret.push(IObject::new(registry, obj).await?); + } + ret + }; + let children = { + let mut ret = Vec::new(); + for obj in original.children.iter() { + ret.push(IDocumentElement::new(registry, obj).await?); + } + ret + }; + Ok(IHeading { + title, + level: original.level, + children, + }) +}); diff --git a/src/intermediate/horizontal_rule.rs b/src/intermediate/horizontal_rule.rs index 59795ee..bc9e323 100644 --- a/src/intermediate/horizontal_rule.rs +++ b/src/intermediate/horizontal_rule.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IHorizontalRule {} - -impl IHorizontalRule { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::HorizontalRule<'parse>, - ) -> Result { - Ok(IHorizontalRule {}) - } -} +inoop!(IHorizontalRule, HorizontalRule); diff --git a/src/intermediate/inline_babel_call.rs b/src/intermediate/inline_babel_call.rs index 8e5e56d..cf1137a 100644 --- a/src/intermediate/inline_babel_call.rs +++ b/src/intermediate/inline_babel_call.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IInlineBabelCall {} - -impl IInlineBabelCall { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::InlineBabelCall<'parse>, - ) -> Result { - Ok(IInlineBabelCall {}) - } -} +inoop!(IInlineBabelCall, InlineBabelCall); diff --git a/src/intermediate/inline_source_block.rs b/src/intermediate/inline_source_block.rs index 41492bb..195afdc 100644 --- a/src/intermediate/inline_source_block.rs +++ b/src/intermediate/inline_source_block.rs @@ -1,19 +1,18 @@ -use crate::error::CustomError; - +use super::macros::intermediate; use super::registry::Registry; +use crate::error::CustomError; #[derive(Debug, Clone)] pub(crate) struct IInlineSourceBlock { pub(crate) value: String, } -impl IInlineSourceBlock { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::InlineSourceBlock<'parse>, - ) -> Result { +intermediate!( + IInlineSourceBlock, + InlineSourceBlock, + |registry, original| async { Ok(IInlineSourceBlock { value: original.value.to_owned(), }) } -} +); diff --git a/src/intermediate/italic.rs b/src/intermediate/italic.rs index 8cb0140..1c20c55 100644 --- a/src/intermediate/italic.rs +++ b/src/intermediate/italic.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IItalic {} - -impl IItalic { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::Italic<'parse>, - ) -> Result { - Ok(IItalic {}) - } -} +inoop!(IItalic, Italic); diff --git a/src/intermediate/keyword.rs b/src/intermediate/keyword.rs index c187f87..3f510f3 100644 --- a/src/intermediate/keyword.rs +++ b/src/intermediate/keyword.rs @@ -1,16 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -/// Essentially a no-op since the keyword is not rendered and any relevant impact on other elements is pulled from the parsed form of keyword. -#[derive(Debug, Clone)] -pub(crate) struct IKeyword {} - -impl IKeyword { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - keyword: &'b organic::types::Keyword<'parse>, - ) -> Result { - Ok(IKeyword {}) - } -} +inoop!(IKeyword, Keyword); diff --git a/src/intermediate/latex_environment.rs b/src/intermediate/latex_environment.rs index 6699078..361ae3e 100644 --- a/src/intermediate/latex_environment.rs +++ b/src/intermediate/latex_environment.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct ILatexEnvironment {} - -impl ILatexEnvironment { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::LatexEnvironment<'parse>, - ) -> Result { - Ok(ILatexEnvironment {}) - } -} +inoop!(ILatexEnvironment, LatexEnvironment); diff --git a/src/intermediate/latex_fragment.rs b/src/intermediate/latex_fragment.rs index 82e03d9..89aa832 100644 --- a/src/intermediate/latex_fragment.rs +++ b/src/intermediate/latex_fragment.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct ILatexFragment {} - -impl ILatexFragment { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::LatexFragment<'parse>, - ) -> Result { - Ok(ILatexFragment {}) - } -} +inoop!(ILatexFragment, LatexFragment); diff --git a/src/intermediate/line_break.rs b/src/intermediate/line_break.rs index 8c87978..8436553 100644 --- a/src/intermediate/line_break.rs +++ b/src/intermediate/line_break.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct ILineBreak {} - -impl ILineBreak { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::LineBreak<'parse>, - ) -> Result { - Ok(ILineBreak {}) - } -} +inoop!(ILineBreak, LineBreak); diff --git a/src/intermediate/macros.rs b/src/intermediate/macros.rs new file mode 100644 index 0000000..2ccbacf --- /dev/null +++ b/src/intermediate/macros.rs @@ -0,0 +1,56 @@ +/// Write the implementation for the intermediate ast node for a type that is a noop or is not yet implemented. +/// +/// This exists to make changing the type signature easier. +macro_rules! inoop { + ($istruct:ident, $pstruct:ident) => { + #[derive(Debug, Clone)] + pub(crate) struct $istruct {} + + 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> { + Ok($istruct {}) + } + } + }; +} + +pub(crate) use inoop; + +/// Write the implementation for the intermediate ast node. +/// +/// This exists to make changing the type signature easier. +macro_rules! intermediate { + ($istruct:ident, $pstruct:ident, $fnbody:expr) => { + 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 + } + } + }; +} + +pub(crate) use intermediate; + +/// Write the implementation for the intermediate ast node. +/// +/// This exists to make changing the type signature easier. +macro_rules! iselector { + ($istruct:ident, $pstruct:ident, $fnbody:expr) => { + impl $istruct { + pub(crate) fn new<'reg, 'orig, 'parse, 'inp: 'reg + 'orig>( + registry: &'reg mut Registry<'orig, 'parse>, + original: &'orig organic::types::$pstruct<'parse>, + ) -> BoxFuture<'inp, Result<$istruct, CustomError>> { + $fnbody(registry, original).boxed() + } + } + }; +} + +pub(crate) use iselector; diff --git a/src/intermediate/mod.rs b/src/intermediate/mod.rs index 0281285..bd8e38a 100644 --- a/src/intermediate/mod.rs +++ b/src/intermediate/mod.rs @@ -32,6 +32,7 @@ mod keyword; mod latex_environment; mod latex_fragment; mod line_break; +mod macros; mod object; mod org_macro; mod page; diff --git a/src/intermediate/object.rs b/src/intermediate/object.rs index 3eee490..b041384 100644 --- a/src/intermediate/object.rs +++ b/src/intermediate/object.rs @@ -13,6 +13,7 @@ use super::inline_source_block::IInlineSourceBlock; use super::italic::IItalic; use super::latex_fragment::ILatexFragment; use super::line_break::ILineBreak; +use super::macros::iselector; use super::org_macro::IOrgMacro; use super::plain_link::IPlainLink; use super::plain_text::IPlainText; @@ -61,96 +62,88 @@ pub(crate) enum IObject { Timestamp(ITimestamp), } -impl IObject { - pub(crate) fn new<'parse, 'b>( - registry: &'b mut Registry<'parse>, - obj: &'b organic::types::Object<'parse>, - ) -> BoxFuture<'b, Result> { - async move { - match obj { - organic::types::Object::Bold(inner) => { - Ok(IObject::Bold(IBold::new(registry, inner).await?)) - } - organic::types::Object::Italic(inner) => { - Ok(IObject::Italic(IItalic::new(registry, inner).await?)) - } - organic::types::Object::Underline(inner) => { - Ok(IObject::Underline(IUnderline::new(registry, inner).await?)) - } - organic::types::Object::StrikeThrough(inner) => Ok(IObject::StrikeThrough( - IStrikeThrough::new(registry, inner).await?, - )), - organic::types::Object::Code(inner) => { - Ok(IObject::Code(ICode::new(registry, inner).await?)) - } - organic::types::Object::Verbatim(inner) => { - Ok(IObject::Verbatim(IVerbatim::new(registry, inner).await?)) - } - organic::types::Object::PlainText(inner) => { - Ok(IObject::PlainText(IPlainText::new(registry, inner).await?)) - } - organic::types::Object::RegularLink(inner) => Ok(IObject::RegularLink( - IRegularLink::new(registry, inner).await?, - )), - organic::types::Object::RadioLink(inner) => { - Ok(IObject::RadioLink(IRadioLink::new(registry, inner).await?)) - } - organic::types::Object::RadioTarget(inner) => Ok(IObject::RadioTarget( - IRadioTarget::new(registry, inner).await?, - )), - organic::types::Object::PlainLink(inner) => { - Ok(IObject::PlainLink(IPlainLink::new(registry, inner).await?)) - } - organic::types::Object::AngleLink(inner) => { - Ok(IObject::AngleLink(IAngleLink::new(registry, inner).await?)) - } - organic::types::Object::OrgMacro(inner) => { - Ok(IObject::OrgMacro(IOrgMacro::new(registry, inner).await?)) - } - organic::types::Object::Entity(inner) => { - Ok(IObject::Entity(IEntity::new(registry, inner).await?)) - } - organic::types::Object::LatexFragment(inner) => Ok(IObject::LatexFragment( - ILatexFragment::new(registry, inner).await?, - )), - organic::types::Object::ExportSnippet(inner) => Ok(IObject::ExportSnippet( - IExportSnippet::new(registry, inner).await?, - )), - organic::types::Object::FootnoteReference(inner) => Ok(IObject::FootnoteReference( - IFootnoteReference::new(registry, inner).await?, - )), - organic::types::Object::Citation(inner) => { - Ok(IObject::Citation(ICitation::new(registry, inner).await?)) - } - organic::types::Object::CitationReference(inner) => Ok(IObject::CitationReference( - ICitationReference::new(registry, inner).await?, - )), - organic::types::Object::InlineBabelCall(inner) => Ok(IObject::InlineBabelCall( - IInlineBabelCall::new(registry, inner).await?, - )), - organic::types::Object::InlineSourceBlock(inner) => Ok(IObject::InlineSourceBlock( - IInlineSourceBlock::new(registry, inner).await?, - )), - organic::types::Object::LineBreak(inner) => { - Ok(IObject::LineBreak(ILineBreak::new(registry, inner).await?)) - } - organic::types::Object::Target(inner) => { - Ok(IObject::Target(ITarget::new(registry, inner).await?)) - } - organic::types::Object::StatisticsCookie(inner) => Ok(IObject::StatisticsCookie( - IStatisticsCookie::new(registry, inner).await?, - )), - organic::types::Object::Subscript(inner) => { - Ok(IObject::Subscript(ISubscript::new(registry, inner).await?)) - } - organic::types::Object::Superscript(inner) => Ok(IObject::Superscript( - ISuperscript::new(registry, inner).await?, - )), - organic::types::Object::Timestamp(inner) => { - Ok(IObject::Timestamp(ITimestamp::new(registry, inner).await?)) - } - } +iselector!(IObject, Object, |registry, obj| async { + match &obj { + organic::types::Object::Bold(inner) => { + Ok(IObject::Bold(IBold::new(registry, inner).await?)) + } + organic::types::Object::Italic(inner) => { + Ok(IObject::Italic(IItalic::new(registry, inner).await?)) + } + organic::types::Object::Underline(inner) => { + Ok(IObject::Underline(IUnderline::new(registry, inner).await?)) + } + organic::types::Object::StrikeThrough(inner) => Ok(IObject::StrikeThrough( + IStrikeThrough::new(registry, inner).await?, + )), + organic::types::Object::Code(inner) => { + Ok(IObject::Code(ICode::new(registry, inner).await?)) + } + organic::types::Object::Verbatim(inner) => { + Ok(IObject::Verbatim(IVerbatim::new(registry, inner).await?)) + } + organic::types::Object::PlainText(inner) => { + Ok(IObject::PlainText(IPlainText::new(registry, inner).await?)) + } + organic::types::Object::RegularLink(inner) => Ok(IObject::RegularLink( + IRegularLink::new(registry, inner).await?, + )), + organic::types::Object::RadioLink(inner) => { + Ok(IObject::RadioLink(IRadioLink::new(registry, inner).await?)) + } + organic::types::Object::RadioTarget(inner) => Ok(IObject::RadioTarget( + IRadioTarget::new(registry, inner).await?, + )), + organic::types::Object::PlainLink(inner) => { + Ok(IObject::PlainLink(IPlainLink::new(registry, inner).await?)) + } + organic::types::Object::AngleLink(inner) => { + Ok(IObject::AngleLink(IAngleLink::new(registry, inner).await?)) + } + organic::types::Object::OrgMacro(inner) => { + Ok(IObject::OrgMacro(IOrgMacro::new(registry, inner).await?)) + } + organic::types::Object::Entity(inner) => { + Ok(IObject::Entity(IEntity::new(registry, inner).await?)) + } + organic::types::Object::LatexFragment(inner) => Ok(IObject::LatexFragment( + ILatexFragment::new(registry, inner).await?, + )), + organic::types::Object::ExportSnippet(inner) => Ok(IObject::ExportSnippet( + IExportSnippet::new(registry, inner).await?, + )), + organic::types::Object::FootnoteReference(inner) => Ok(IObject::FootnoteReference( + IFootnoteReference::new(registry, inner).await?, + )), + organic::types::Object::Citation(inner) => { + Ok(IObject::Citation(ICitation::new(registry, inner).await?)) + } + organic::types::Object::CitationReference(inner) => Ok(IObject::CitationReference( + ICitationReference::new(registry, inner).await?, + )), + organic::types::Object::InlineBabelCall(inner) => Ok(IObject::InlineBabelCall( + IInlineBabelCall::new(registry, inner).await?, + )), + organic::types::Object::InlineSourceBlock(inner) => Ok(IObject::InlineSourceBlock( + IInlineSourceBlock::new(registry, inner).await?, + )), + organic::types::Object::LineBreak(inner) => { + Ok(IObject::LineBreak(ILineBreak::new(registry, inner).await?)) + } + organic::types::Object::Target(inner) => { + Ok(IObject::Target(ITarget::new(registry, inner).await?)) + } + organic::types::Object::StatisticsCookie(inner) => Ok(IObject::StatisticsCookie( + IStatisticsCookie::new(registry, inner).await?, + )), + organic::types::Object::Subscript(inner) => { + Ok(IObject::Subscript(ISubscript::new(registry, inner).await?)) + } + organic::types::Object::Superscript(inner) => Ok(IObject::Superscript( + ISuperscript::new(registry, inner).await?, + )), + organic::types::Object::Timestamp(inner) => { + Ok(IObject::Timestamp(ITimestamp::new(registry, inner).await?)) } - .boxed() } -} +}); diff --git a/src/intermediate/org_macro.rs b/src/intermediate/org_macro.rs index 7fb3fe5..bae7384 100644 --- a/src/intermediate/org_macro.rs +++ b/src/intermediate/org_macro.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IOrgMacro {} - -impl IOrgMacro { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::OrgMacro<'parse>, - ) -> Result { - Ok(IOrgMacro {}) - } -} +inoop!(IOrgMacro, OrgMacro); diff --git a/src/intermediate/page.rs b/src/intermediate/page.rs index 0333f75..957a1ad 100644 --- a/src/intermediate/page.rs +++ b/src/intermediate/page.rs @@ -21,9 +21,10 @@ pub(crate) struct BlogPostPage { } impl BlogPostPage { - pub(crate) async fn new<'b, 'parse, P: Into>( + // TODO: Move path into the registry so I can give this a standard interface like the others. + pub(crate) async fn new<'a, 'b, 'parse, P: Into>( path: P, - registry: &'b mut Registry<'parse>, + registry: &'a mut Registry<'b, 'parse>, document: &'b organic::types::Document<'parse>, ) -> Result { let path = path.into(); diff --git a/src/intermediate/paragraph.rs b/src/intermediate/paragraph.rs index d9b0893..6a33a4c 100644 --- a/src/intermediate/paragraph.rs +++ b/src/intermediate/paragraph.rs @@ -1,26 +1,21 @@ -use crate::error::CustomError; - +use super::macros::intermediate; use super::registry::Registry; use super::IObject; +use crate::error::CustomError; #[derive(Debug, Clone)] pub(crate) struct IParagraph { pub(crate) children: Vec, } -impl IParagraph { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::Paragraph<'parse>, - ) -> Result { - let children = { - let mut ret = Vec::new(); - for obj in original.children.iter() { - ret.push(IObject::new(registry, obj).await?); - } - ret - }; +intermediate!(IParagraph, Paragraph, |registry, original| async { + let children = { + let mut ret = Vec::new(); + for obj in original.children.iter() { + ret.push(IObject::new(registry, obj).await?); + } + ret + }; - Ok(IParagraph { children }) - } -} + Ok(IParagraph { children }) +}); diff --git a/src/intermediate/plain_link.rs b/src/intermediate/plain_link.rs index a6503c3..68d0861 100644 --- a/src/intermediate/plain_link.rs +++ b/src/intermediate/plain_link.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IPlainLink {} - -impl IPlainLink { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::PlainLink<'parse>, - ) -> Result { - Ok(IPlainLink {}) - } -} +inoop!(IPlainLink, PlainLink); diff --git a/src/intermediate/plain_list.rs b/src/intermediate/plain_list.rs index 17ac7cb..61ec964 100644 --- a/src/intermediate/plain_list.rs +++ b/src/intermediate/plain_list.rs @@ -1,7 +1,7 @@ -use crate::error::CustomError; - +use super::macros::intermediate; use super::registry::Registry; use super::IPlainListItem; +use crate::error::CustomError; #[derive(Debug, Clone)] pub(crate) struct IPlainList { @@ -9,22 +9,17 @@ pub(crate) struct IPlainList { pub(crate) children: Vec, } -impl IPlainList { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::PlainList<'parse>, - ) -> Result { - let children = { - let mut ret = Vec::new(); - for obj in original.children.iter() { - ret.push(IPlainListItem::new(registry, obj).await?); - } - ret - }; +intermediate!(IPlainList, PlainList, |registry, original| async { + let children = { + let mut ret = Vec::new(); + for obj in original.children.iter() { + ret.push(IPlainListItem::new(registry, obj).await?); + } + ret + }; - Ok(IPlainList { - list_type: original.list_type, - children, - }) - } -} + Ok(IPlainList { + list_type: original.list_type, + children, + }) +}); diff --git a/src/intermediate/plain_list_item.rs b/src/intermediate/plain_list_item.rs index 9e8f7ad..7af4933 100644 --- a/src/intermediate/plain_list_item.rs +++ b/src/intermediate/plain_list_item.rs @@ -1,8 +1,8 @@ -use crate::error::CustomError; - +use super::macros::intermediate; use super::registry::Registry; use super::IElement; use super::IObject; +use crate::error::CustomError; #[derive(Debug, Clone)] pub(crate) struct IPlainListItem { @@ -10,27 +10,22 @@ pub(crate) struct IPlainListItem { pub(crate) children: Vec, } -impl IPlainListItem { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::PlainListItem<'parse>, - ) -> Result { - let tag = { - let mut ret = Vec::new(); - for obj in original.tag.iter() { - ret.push(IObject::new(registry, obj).await?); - } - ret - }; +intermediate!(IPlainListItem, PlainListItem, |registry, original| async { + let tag = { + let mut ret = Vec::new(); + for obj in original.tag.iter() { + ret.push(IObject::new(registry, obj).await?); + } + ret + }; - let children = { - let mut ret = Vec::new(); - for elem in original.children.iter() { - ret.push(IElement::new(registry, elem).await?); - } - ret - }; + let children = { + let mut ret = Vec::new(); + for elem in original.children.iter() { + ret.push(IElement::new(registry, elem).await?); + } + ret + }; - Ok(IPlainListItem { tag, children }) - } -} + Ok(IPlainListItem { tag, children }) +}); diff --git a/src/intermediate/plain_text.rs b/src/intermediate/plain_text.rs index 30114ab..24c6a68 100644 --- a/src/intermediate/plain_text.rs +++ b/src/intermediate/plain_text.rs @@ -1,20 +1,15 @@ -use crate::error::CustomError; -use crate::intermediate::util::coalesce_whitespace; - +use super::macros::intermediate; use super::registry::Registry; +use super::util::coalesce_whitespace; +use crate::error::CustomError; #[derive(Debug, Clone)] pub(crate) struct IPlainText { pub(crate) source: String, } -impl IPlainText { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - plain_text: &'b organic::types::PlainText<'parse>, - ) -> Result { - Ok(IPlainText { - source: coalesce_whitespace(plain_text.source).into_owned(), - }) - } -} +intermediate!(IPlainText, PlainText, |registry, original| async { + Ok(IPlainText { + source: coalesce_whitespace(original.source).into_owned(), + }) +}); diff --git a/src/intermediate/planning.rs b/src/intermediate/planning.rs index 968f288..7de9746 100644 --- a/src/intermediate/planning.rs +++ b/src/intermediate/planning.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IPlanning {} - -impl IPlanning { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::Planning<'parse>, - ) -> Result { - Ok(IPlanning {}) - } -} +inoop!(IPlanning, Planning); diff --git a/src/intermediate/property_drawer.rs b/src/intermediate/property_drawer.rs index 000946a..9a42001 100644 --- a/src/intermediate/property_drawer.rs +++ b/src/intermediate/property_drawer.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IPropertyDrawer {} - -impl IPropertyDrawer { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::PropertyDrawer<'parse>, - ) -> Result { - Ok(IPropertyDrawer {}) - } -} +inoop!(IPropertyDrawer, PropertyDrawer); diff --git a/src/intermediate/quote_block.rs b/src/intermediate/quote_block.rs index db7c062..27ef6e2 100644 --- a/src/intermediate/quote_block.rs +++ b/src/intermediate/quote_block.rs @@ -1,26 +1,21 @@ -use crate::error::CustomError; - +use super::macros::intermediate; use super::registry::Registry; use super::IElement; +use crate::error::CustomError; #[derive(Debug, Clone)] pub(crate) struct IQuoteBlock { pub(crate) children: Vec, } -impl IQuoteBlock { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::QuoteBlock<'parse>, - ) -> Result { - let children = { - let mut ret = Vec::new(); - for obj in original.children.iter() { - ret.push(IElement::new(registry, obj).await?); - } - ret - }; +intermediate!(IQuoteBlock, QuoteBlock, |registry, original| async { + let children = { + let mut ret = Vec::new(); + for obj in original.children.iter() { + ret.push(IElement::new(registry, obj).await?); + } + ret + }; - Ok(IQuoteBlock { children }) - } -} + Ok(IQuoteBlock { children }) +}); diff --git a/src/intermediate/radio_link.rs b/src/intermediate/radio_link.rs index 0f382ee..6dfea17 100644 --- a/src/intermediate/radio_link.rs +++ b/src/intermediate/radio_link.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IRadioLink {} - -impl IRadioLink { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::RadioLink<'parse>, - ) -> Result { - Ok(IRadioLink {}) - } -} +inoop!(IRadioLink, RadioLink); diff --git a/src/intermediate/radio_target.rs b/src/intermediate/radio_target.rs index e8f4735..01b20e9 100644 --- a/src/intermediate/radio_target.rs +++ b/src/intermediate/radio_target.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IRadioTarget {} - -impl IRadioTarget { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::RadioTarget<'parse>, - ) -> Result { - Ok(IRadioTarget {}) - } -} +inoop!(IRadioTarget, RadioTarget); diff --git a/src/intermediate/registry.rs b/src/intermediate/registry.rs index a7a847f..4fe888f 100644 --- a/src/intermediate/registry.rs +++ b/src/intermediate/registry.rs @@ -9,14 +9,17 @@ use super::ast_node::IAstNode; type IdCounter = u16; -pub(crate) struct Registry<'parse> { +pub(crate) struct Registry<'orig, 'parse> { id_counter: IdCounter, targets: HashMap<&'parse str, String>, - footnote_ids: Vec<(Option<&'parse str>, Vec)>, + footnote_ids: Vec<( + Option<&'parse str>, + FootnoteDefinitionContents<'orig, 'parse>, + )>, } -impl<'parse> Registry<'parse> { - pub(crate) fn new() -> Registry<'parse> { +impl<'orig, 'parse> Registry<'orig, 'parse> { + pub(crate) fn new() -> Registry<'orig, 'parse> { Registry { id_counter: 0, targets: HashMap::new(), @@ -24,7 +27,7 @@ impl<'parse> Registry<'parse> { } } - pub(crate) fn get_target<'b>(&'b mut self, body: &'parse str) -> &'b String { + pub(crate) fn get_target<'reg>(&'reg mut self, body: &'parse str) -> &'reg String { self.targets.entry(body).or_insert_with(|| { self.id_counter += 1; format!("target_{}", self.id_counter) @@ -32,19 +35,21 @@ impl<'parse> Registry<'parse> { } pub(crate) fn get_footnote_ids(&self) -> impl Iterator)> { - self.footnote_ids - .iter() - .map(|(_label, definition)| definition) - .enumerate() + // TODO + std::iter::empty() + // self.footnote_ids + // .iter() + // .map(|(_label, definition)| definition) + // .enumerate() } /// Get a 0-indexed ID for a footnote. /// /// This needs to be incremented to be 1-indexed for render. - pub(crate) async fn get_footnote_reference_id<'b>( - &'b mut self, + pub(crate) async fn get_footnote_reference_id<'reg>( + &'reg mut self, label: Option<&'parse str>, - definition: &'b Vec>, + definition: &'orig Vec>, ) -> Result { if let None = label { // If it has no label then it must always get a new ID. @@ -75,10 +80,10 @@ impl<'parse> Registry<'parse> { } /// Update the definition to a footnote but do not mark it as referenced. - pub(crate) async fn register_footnote_definition<'b>( - &'b mut self, + pub(crate) async fn register_footnote_definition<'reg>( + &'reg mut self, label: &'parse str, - definition: &'b Vec>, + definition: &'orig Vec>, ) -> Result<(), CustomError> { let contents = convert_definition_contents(self, definition).await?; if let Some((_existing_id, existing_definition)) = self @@ -92,9 +97,9 @@ impl<'parse> Registry<'parse> { } } -async fn convert_reference_contents<'b, 'parse>( - registry: &'b mut Registry<'parse>, - contents: &'b Vec>, +async fn convert_reference_contents<'reg, 'orig, 'parse>( + registry: &'reg mut Registry<'orig, 'parse>, + contents: &'orig Vec>, ) -> Result, CustomError> { let contents = { let mut ret = Vec::new(); @@ -107,9 +112,9 @@ async fn convert_reference_contents<'b, 'parse>( Ok(contents) } -async fn convert_definition_contents<'b, 'parse>( - registry: &'b mut Registry<'parse>, - contents: &'b Vec>, +async fn convert_definition_contents<'reg, 'orig, 'parse>( + registry: &'reg mut Registry<'orig, 'parse>, + contents: &'orig Vec>, ) -> Result, CustomError> { let contents = { let mut ret = Vec::new(); @@ -121,3 +126,8 @@ async fn convert_definition_contents<'b, 'parse>( Ok(contents) } + +enum FootnoteDefinitionContents<'orig, 'parse> { + FromReference(&'orig Vec>), + FromDefinition(&'orig Vec>), +} diff --git a/src/intermediate/regular_link.rs b/src/intermediate/regular_link.rs index 87b34b2..6b155cc 100644 --- a/src/intermediate/regular_link.rs +++ b/src/intermediate/regular_link.rs @@ -1,7 +1,7 @@ -use crate::error::CustomError; - +use super::macros::intermediate; use super::registry::Registry; use super::IObject; +use crate::error::CustomError; #[derive(Debug, Clone)] pub(crate) struct IRegularLink { @@ -9,21 +9,16 @@ pub(crate) struct IRegularLink { pub(crate) children: Vec, } -impl IRegularLink { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::RegularLink<'parse>, - ) -> Result { - let children = { - let mut ret = Vec::new(); - for obj in original.children.iter() { - ret.push(IObject::new(registry, obj).await?); - } - ret - }; - Ok(IRegularLink { - raw_link: original.get_raw_link().into_owned(), - children, - }) - } -} +intermediate!(IRegularLink, RegularLink, |registry, original| async { + let children = { + let mut ret = Vec::new(); + for obj in original.children.iter() { + ret.push(IObject::new(registry, obj).await?); + } + ret + }; + Ok(IRegularLink { + raw_link: original.get_raw_link().into_owned(), + children, + }) +}); diff --git a/src/intermediate/section.rs b/src/intermediate/section.rs index 658a3d2..e1c81ee 100644 --- a/src/intermediate/section.rs +++ b/src/intermediate/section.rs @@ -1,26 +1,21 @@ -use crate::error::CustomError; - +use super::macros::intermediate; use super::registry::Registry; use super::IElement; +use crate::error::CustomError; #[derive(Debug, Clone)] pub(crate) struct ISection { pub(crate) children: Vec, } -impl ISection { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - section: &'b organic::types::Section<'parse>, - ) -> Result { - let children = { - let mut ret = Vec::new(); - for elem in section.children.iter() { - ret.push(IElement::new(registry, elem).await?); - } - ret - }; +intermediate!(ISection, Section, |registry, original| async { + let children = { + let mut ret = Vec::new(); + for elem in original.children.iter() { + ret.push(IElement::new(registry, elem).await?); + } + ret + }; - Ok(ISection { children }) - } -} + Ok(ISection { children }) +}); diff --git a/src/intermediate/special_block.rs b/src/intermediate/special_block.rs index 952468b..2109242 100644 --- a/src/intermediate/special_block.rs +++ b/src/intermediate/special_block.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct ISpecialBlock {} - -impl ISpecialBlock { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::SpecialBlock<'parse>, - ) -> Result { - Ok(ISpecialBlock {}) - } -} +inoop!(ISpecialBlock, SpecialBlock); diff --git a/src/intermediate/src_block.rs b/src/intermediate/src_block.rs index d5a4c92..2a1c608 100644 --- a/src/intermediate/src_block.rs +++ b/src/intermediate/src_block.rs @@ -1,22 +1,17 @@ -use crate::error::CustomError; - +use super::macros::intermediate; use super::registry::Registry; +use crate::error::CustomError; #[derive(Debug, Clone)] pub(crate) struct ISrcBlock { pub(crate) lines: Vec, } -impl ISrcBlock { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::SrcBlock<'parse>, - ) -> Result { - let lines = original - .contents - .split_inclusive('\n') - .map(|s| s.to_owned()) - .collect(); - Ok(ISrcBlock { lines }) - } -} +intermediate!(ISrcBlock, SrcBlock, |registry, original| async { + let lines = original + .contents + .split_inclusive('\n') + .map(|s| s.to_owned()) + .collect(); + Ok(ISrcBlock { lines }) +}); diff --git a/src/intermediate/statistics_cookie.rs b/src/intermediate/statistics_cookie.rs index b2915eb..ae6ec23 100644 --- a/src/intermediate/statistics_cookie.rs +++ b/src/intermediate/statistics_cookie.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IStatisticsCookie {} - -impl IStatisticsCookie { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::StatisticsCookie<'parse>, - ) -> Result { - Ok(IStatisticsCookie {}) - } -} +inoop!(IStatisticsCookie, StatisticsCookie); diff --git a/src/intermediate/strike_through.rs b/src/intermediate/strike_through.rs index c81ee12..d870835 100644 --- a/src/intermediate/strike_through.rs +++ b/src/intermediate/strike_through.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct IStrikeThrough {} - -impl IStrikeThrough { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::StrikeThrough<'parse>, - ) -> Result { - Ok(IStrikeThrough {}) - } -} +inoop!(IStrikeThrough, StrikeThrough); diff --git a/src/intermediate/subscript.rs b/src/intermediate/subscript.rs index 18c57ef..4ce1f3e 100644 --- a/src/intermediate/subscript.rs +++ b/src/intermediate/subscript.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct ISubscript {} - -impl ISubscript { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::Subscript<'parse>, - ) -> Result { - Ok(ISubscript {}) - } -} +inoop!(ISubscript, Subscript); diff --git a/src/intermediate/superscript.rs b/src/intermediate/superscript.rs index 247e250..9449de3 100644 --- a/src/intermediate/superscript.rs +++ b/src/intermediate/superscript.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct ISuperscript {} - -impl ISuperscript { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::Superscript<'parse>, - ) -> Result { - Ok(ISuperscript {}) - } -} +inoop!(ISuperscript, Superscript); diff --git a/src/intermediate/table.rs b/src/intermediate/table.rs index e5ceb9d..c4cb36b 100644 --- a/src/intermediate/table.rs +++ b/src/intermediate/table.rs @@ -1,15 +1,5 @@ +use super::macros::inoop; +use super::registry::Registry; use crate::error::CustomError; -use super::registry::Registry; - -#[derive(Debug, Clone)] -pub(crate) struct ITable {} - -impl ITable { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::Table<'parse>, - ) -> Result { - Ok(ITable {}) - } -} +inoop!(ITable, Table); diff --git a/src/intermediate/target.rs b/src/intermediate/target.rs index 4bc57ec..1e41833 100644 --- a/src/intermediate/target.rs +++ b/src/intermediate/target.rs @@ -1,7 +1,6 @@ -use crate::error::CustomError; -use crate::intermediate::util::coalesce_whitespace; - +use super::macros::intermediate; use super::registry::Registry; +use crate::error::CustomError; #[derive(Debug, Clone)] pub(crate) struct ITarget { @@ -9,15 +8,10 @@ pub(crate) struct ITarget { value: String, } -impl ITarget { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - target: &'b organic::types::Target<'parse>, - ) -> Result { - let id = registry.get_target(target.value); - Ok(ITarget { - id: id.clone(), - value: target.value.to_owned(), - }) - } -} +intermediate!(ITarget, Target, |registry, original| async { + let id = registry.get_target(original.value); + Ok(ITarget { + id: id.clone(), + value: original.value.to_owned(), + }) +}); diff --git a/src/intermediate/timestamp.rs b/src/intermediate/timestamp.rs index f590f64..d2a2692 100644 --- a/src/intermediate/timestamp.rs +++ b/src/intermediate/timestamp.rs @@ -1,15 +1,6 @@ use crate::error::CustomError; +use super::macros::inoop; use super::registry::Registry; -#[derive(Debug, Clone)] -pub(crate) struct ITimestamp {} - -impl ITimestamp { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::Timestamp<'parse>, - ) -> Result { - Ok(ITimestamp {}) - } -} +inoop!(ITimestamp, Timestamp); diff --git a/src/intermediate/underline.rs b/src/intermediate/underline.rs index a2e5c83..2e60589 100644 --- a/src/intermediate/underline.rs +++ b/src/intermediate/underline.rs @@ -1,15 +1,6 @@ use crate::error::CustomError; +use super::macros::inoop; use super::registry::Registry; -#[derive(Debug, Clone)] -pub(crate) struct IUnderline {} - -impl IUnderline { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::Underline<'parse>, - ) -> Result { - Ok(IUnderline {}) - } -} +inoop!(IUnderline, Underline); diff --git a/src/intermediate/verbatim.rs b/src/intermediate/verbatim.rs index 4d60d54..7aecb4e 100644 --- a/src/intermediate/verbatim.rs +++ b/src/intermediate/verbatim.rs @@ -1,15 +1,6 @@ use crate::error::CustomError; +use super::macros::inoop; use super::registry::Registry; -#[derive(Debug, Clone)] -pub(crate) struct IVerbatim {} - -impl IVerbatim { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::Verbatim<'parse>, - ) -> Result { - Ok(IVerbatim {}) - } -} +inoop!(IVerbatim, Verbatim); diff --git a/src/intermediate/verse_block.rs b/src/intermediate/verse_block.rs index bd37207..6a53749 100644 --- a/src/intermediate/verse_block.rs +++ b/src/intermediate/verse_block.rs @@ -1,15 +1,6 @@ use crate::error::CustomError; +use super::macros::inoop; use super::registry::Registry; -#[derive(Debug, Clone)] -pub(crate) struct IVerseBlock {} - -impl IVerseBlock { - pub(crate) async fn new<'b, 'parse>( - registry: &'b mut Registry<'parse>, - original: &'b organic::types::VerseBlock<'parse>, - ) -> Result { - Ok(IVerseBlock {}) - } -} +inoop!(IVerseBlock, VerseBlock);