diff --git a/src/blocktree.rs b/src/blocktree.rs index eba32bf..6e42380 100644 --- a/src/blocktree.rs +++ b/src/blocktree.rs @@ -38,6 +38,42 @@ impl BlockTreeNode { } } +struct BlockTreeIterator<'a> { + node_stack: Vec<&'a BlockTreeNode> +} + +impl<'a> BlockTreeIterator<'a> { + fn new(root_node: &'a BlockTreeNode) -> Self { + Self { + node_stack: vec![root_node] + } + } +} + +impl<'a> Iterator for BlockTreeIterator<'a> { + type Item = i128; + fn next(&mut self) -> Option { + + // stack is empty, we are done otherwise continue with current_node + let Some(current_node) = self.node_stack.pop() else { + return None; + }; + + // if the current node has a next sibling, stack it. + if let Some(next_sibling_node) = ¤t_node.next_sibling_node { + self.node_stack.push(next_sibling_node); + } + + // if the current node has a first child, stack it. + if let Some(first_child_node) = ¤t_node.first_child_node { + self.node_stack.push(first_child_node); + } + + // yield a clone of the block pointed to by current_node + Some(current_node.block_id) + } +} + #[derive(Debug)] pub struct NotePage { title: String,