diff --git a/src/iter/ast_node_iter.rs b/src/iter/ast_node_iter.rs index 1c08490..1a633dd 100644 --- a/src/iter/ast_node_iter.rs +++ b/src/iter/ast_node_iter.rs @@ -87,64 +87,22 @@ pub enum AstNodeIter<'r, 's> { // Timestamp(TimestampIter<'r, 's>), } -pub struct DocumentIter<'r, 's> { - zeroth_section_next: std::option::Iter<'r, Section<'s>>, - children_next: std::slice::Iter<'r, Heading<'s>>, -} - -impl<'r, 's> Iterator for DocumentIter<'r, 's> { - type Item = AstNode<'r, 's>; - - fn next(&mut self) -> Option { - self.zeroth_section_next - .next() - .map(Into::::into) - .or_else(|| self.children_next.next().map(Into::::into)) - } -} - -impl<'r, 's> IntoIterator for &'r Document<'s> { - type Item = AstNode<'r, 's>; - - type IntoIter = DocumentIter<'r, 's>; - - fn into_iter(self) -> Self::IntoIter { - DocumentIter { - zeroth_section_next: self.zeroth_section.iter(), - children_next: self.children.iter(), - } - } -} - -pub struct HeadingIter<'r, 's> { - title_next: std::slice::Iter<'r, Object<'s>>, - children_next: std::slice::Iter<'r, DocumentElement<'s>>, -} - -impl<'r, 's> Iterator for HeadingIter<'r, 's> { - type Item = AstNode<'r, 's>; - - fn next(&mut self) -> Option { - self.title_next - .next() - .map(Into::::into) - .or_else(|| self.children_next.next().map(Into::::into)) - } -} - -impl<'r, 's> IntoIterator for &'r Heading<'s> { - type Item = AstNode<'r, 's>; - - type IntoIter = HeadingIter<'r, 's>; - - fn into_iter(self) -> Self::IntoIter { - HeadingIter { - title_next: self.title.iter(), - children_next: self.children.iter(), - } - } -} - +multi_field_iter!( + Document<'s>, + DocumentIter, + zeroth_section, + std::option::Iter<'r, Section<'s>>, + children, + std::slice::Iter<'r, Heading<'s>> +); +multi_field_iter!( + Heading<'s>, + HeadingIter, + title, + std::slice::Iter<'r, Object<'s>>, + children, + std::slice::Iter<'r, DocumentElement<'s>> +); children_iter!(Section<'s>, SectionIter, std::slice::Iter<'r, Element<'s>>); children_iter!( Paragraph<'s>, @@ -156,7 +114,6 @@ children_iter!( PlainListIter, std::slice::Iter<'r, PlainListItem<'s>> ); - multi_field_iter!( PlainListItem<'s>, PlainListItemIter,