Struct conciliator::print::Tree
source · pub struct Tree<'s, I: ExactSizeIterator<Item = T>, T, F> { /* private fields */ }
Expand description
Tree structure of things to print, recursively traversed
Instead of constraining the user to any particular data structure, this object is built around a traversal function. This function is used to traverse the tree depth-first, starting from an iterator of “root” nodes.
Requires ExactSizeIterator
s so that it can print the correct symbols (├─
vs └─
).
For example:
struct Animal (&'static str, Vec<Animal>);
let tree = [
Animal ("Reptiles", vec![
Animal ("Frogs", vec![]),
Animal ("Lizards", vec![
Animal ("Chameleon", vec![]),
Animal ("Gecko", vec![]),
])
]),
Animal ("Mammals", vec![
Animal ("Tiger", vec![]),
Animal ("Monkey", vec![])
]),
Animal ("Crab", vec![])
];
impl Inline for Animal {
fn inline(&self, buffer: &mut Buffer) {
buffer.push_plain(self.0);
}
}
con.print(Tree::new(
"Animals:",
tree.iter(),
|a| Some(a.1.iter())
));
Produces something like:
[ ┬ ] Animals:
├─Reptiles
│ ├─Frogs
│ └─Lizards
│ ├─Chameleon
│ └─Gecko
├─Mammals
│ ├─Tiger
│ └─Monkey
└─Crab
Unfortunately, unlike the List
, the Tree does not currently support a formatter
- the items need to be Inline
.
Implementations§
source§impl<'s, I, T, F> Tree<'s, I, T, F>
impl<'s, I, T, F> Tree<'s, I, T, F>
sourcepub fn new(description: &'s str, root_nodes: I, traverser: F) -> Self
pub fn new(description: &'s str, root_nodes: I, traverser: F) -> Self
Create a new Tree
The traverser
function is called with each item (node) in the tree and returns an iterator of its children: FnMut(T) -> Option<ExactSizeIterator<Item = T>>
.
The Option<…>
return type was chosen to simplify printing trees with distinct Branch / Leaf types, for which the traverser
function, if they are merged into an enum Node
, might otherwise have difficulty creating an empty ExactSizeIterator
in the leaf case.