From 7419b75d76fbae9c218eaf255bb9c391adaf7bc3 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 27 Sep 2023 15:38:33 -0400 Subject: [PATCH] Implement empty iterator for types with no ast node children. --- src/iter/ast_node_iter.rs | 70 +++++++++++++++++++++++---------------- src/iter/macros.rs | 30 +++++++++++++++++ src/iter/mod.rs | 1 + 3 files changed, 72 insertions(+), 29 deletions(-) create mode 100644 src/iter/macros.rs diff --git a/src/iter/ast_node_iter.rs b/src/iter/ast_node_iter.rs index 0c55a80..19b85c1 100644 --- a/src/iter/ast_node_iter.rs +++ b/src/iter/ast_node_iter.rs @@ -1,7 +1,13 @@ +use std::marker::PhantomData; + use super::ast_node::AstNode; +use super::macros::simple_iter; use crate::types::Bold; +use crate::types::Code; use crate::types::Italic; use crate::types::Object; +use crate::types::StrikeThrough; +use crate::types::Underline; pub enum AstNodeIter<'r, 's> { // Document Nodes @@ -34,9 +40,9 @@ pub enum AstNodeIter<'r, 's> { // Objects Bold(BoldIter<'r, 's>), Italic(ItalicIter<'r, 's>), - // Underline(UnderlineIter<'r, 's>), - // StrikeThrough(StrikeThroughIter<'r, 's>), - // Code(CodeIter<'r, 's>), + Underline(UnderlineIter<'r, 's>), + StrikeThrough(StrikeThroughIter<'r, 's>), + Code(CodeIter<'r, 's>), // Verbatim(VerbatimIter<'r, 's>), // PlainText(PlainTextIter<'r, 's>), // RegularLink(RegularLinkIter<'r, 's>), @@ -85,32 +91,38 @@ impl<'r, 's> IntoIterator for &'r Bold<'s> { } } -macro_rules! simple_iter { - ($astnodetype:ty, $itertype:ident, $innertype:ty) => { - pub struct $itertype<'r, 's> { - next: $innertype, - } +simple_iter!(Italic<'s>, ItalicIter, std::slice::Iter<'r, Object<'s>>); +simple_iter!( + Underline<'s>, + UnderlineIter, + std::slice::Iter<'r, Object<'s>> +); +simple_iter!( + StrikeThrough<'s>, + StrikeThroughIter, + std::slice::Iter<'r, Object<'s>> +); - impl<'r, 's> Iterator for $itertype<'r, 's> { - type Item = AstNode<'r, 's>; - - fn next(&mut self) -> Option { - self.next.next().map(Into::::into) - } - } - - impl<'r, 's> IntoIterator for &'r $astnodetype { - type Item = AstNode<'r, 's>; - - type IntoIter = $itertype<'r, 's>; - - fn into_iter(self) -> Self::IntoIter { - $itertype { - next: self.children.iter(), - } - } - } - }; +pub struct CodeIter<'r, 's> { + phantom: PhantomData<&'r Code<'s>>, } -simple_iter!(Italic<'s>, ItalicIter, std::slice::Iter<'r, Object<'s>>); +impl<'r, 's> Iterator for CodeIter<'r, 's> { + type Item = AstNode<'r, 's>; + + fn next(&mut self) -> Option { + None + } +} + +impl<'r, 's> IntoIterator for &'r Code<'s> { + type Item = AstNode<'r, 's>; + + type IntoIter = CodeIter<'r, 's>; + + fn into_iter(self) -> Self::IntoIter { + CodeIter { + phantom: PhantomData, + } + } +} diff --git a/src/iter/macros.rs b/src/iter/macros.rs new file mode 100644 index 0000000..acb0f40 --- /dev/null +++ b/src/iter/macros.rs @@ -0,0 +1,30 @@ +/// Create iterators for ast nodes where it only has to iterate over children +macro_rules! simple_iter { + ($astnodetype:ty, $itertype:ident, $innertype:ty) => { + pub struct $itertype<'r, 's> { + next: $innertype, + } + + impl<'r, 's> Iterator for $itertype<'r, 's> { + type Item = AstNode<'r, 's>; + + fn next(&mut self) -> Option { + self.next.next().map(Into::::into) + } + } + + impl<'r, 's> IntoIterator for &'r $astnodetype { + type Item = AstNode<'r, 's>; + + type IntoIter = $itertype<'r, 's>; + + fn into_iter(self) -> Self::IntoIter { + $itertype { + next: self.children.iter(), + } + } + } + }; +} + +pub(crate) use simple_iter; diff --git a/src/iter/mod.rs b/src/iter/mod.rs index b59a57f..b622d37 100644 --- a/src/iter/mod.rs +++ b/src/iter/mod.rs @@ -1,2 +1,3 @@ mod ast_node; mod ast_node_iter; +mod macros;