use crate::mapping::MapKey; use super::{MappingTree, Node, NodeValue}; pub struct MappingTreeIterator { children: Vec<(Vec, String)>, } impl MappingTreeIterator { pub fn new(tree: &MappingTree, ignore_extendable: bool) -> Self { let children = extract_child(vec![], &tree.root, ignore_extendable); Self { children } } } fn extract_child( current_key: Vec, node: &Node, ignore_extendable: bool, ) -> Vec<(Vec, String)> { match &node.value { NodeValue::Parent { children } => children .iter() .map(|(key, value)| { let mut new_key = current_key.clone(); new_key.push(key.to_owned()); extract_child(new_key, value, ignore_extendable) }) .flatten() .collect(), NodeValue::Child { path, extandable } => { if ignore_extendable { vec![(current_key, path.to_string())] } else { if *extandable { vec![(current_key, path.to_string())] } else { vec![] } } } } } impl Iterator for MappingTreeIterator { type Item = (Vec, String); fn next(&mut self) -> Option { self.children.pop() } }