Introduce macro to simplify this.

This commit is contained in:
Tom Alexander 2023-09-27 15:28:43 -04:00
parent 9a1d91ae45
commit e4cfc296e5
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -1,5 +1,6 @@
use super::ast_node::AstNode;
use crate::types::Bold;
use crate::types::Italic;
use crate::types::Object;
pub enum AstNodeIter<'r, 's> {
@ -32,7 +33,7 @@ pub enum AstNodeIter<'r, 's> {
// LatexEnvironment(LatexEnvironmentIter<'r, 's>),
// Objects
Bold(BoldIter<'r, 's>),
// Italic(ItalicIter<'r, 's>),
Italic(ItalicIter<'r, 's>),
// Underline(UnderlineIter<'r, 's>),
// StrikeThrough(StrikeThroughIter<'r, 's>),
// Code(CodeIter<'r, 's>),
@ -83,3 +84,33 @@ 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,
}
impl<'r, 's> Iterator for $itertype<'r, 's> {
type Item = AstNode<'r, 's>;
fn next(&mut self) -> Option<Self::Item> {
self.next.next().map(Into::<AstNode>::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(),
}
}
}
};
}
simple_iter!(Italic<'s>, ItalicIter, std::slice::Iter<'r, Object<'s>>);