diff options
Diffstat (limited to 'pkgs/sources/lf-make-map/src/mapping/map_tree/iterator.rs')
-rw-r--r-- | pkgs/sources/lf-make-map/src/mapping/map_tree/iterator.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/pkgs/sources/lf-make-map/src/mapping/map_tree/iterator.rs b/pkgs/sources/lf-make-map/src/mapping/map_tree/iterator.rs new file mode 100644 index 00000000..4364bb2b --- /dev/null +++ b/pkgs/sources/lf-make-map/src/mapping/map_tree/iterator.rs @@ -0,0 +1,53 @@ +use crate::mapping::MapKey; + +use super::{MappingTree, Node, NodeValue}; + +pub struct MappingTreeIterator { + children: Vec<(Vec<MapKey>, 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<MapKey>, + node: &Node, + ignore_extendable: bool, +) -> Vec<(Vec<MapKey>, 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<MapKey>, String); + + fn next(&mut self) -> Option<Self::Item> { + self.children.pop() + } +} |