1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use super::Tree;
use crate::{Nesting, NestingFunction};

pub trait IntoTreeExt<Token> {
    fn into_tree<F>(self, fun: F) -> Tree<Token>
    where
        F: NestingFunction<Token>;
}

impl<Iterable, Token> IntoTreeExt<Token> for Iterable
where
    Iterable: IntoIterator<Item = Token>,
{
    fn into_tree<F>(self, mut fun: F) -> Tree<Token>
    where
        F: NestingFunction<Token>,
    {
        let mut container = Vec::new();
        let mut parents = Vec::new();

        for token in self {
            match fun.direction(&token) {
                Nesting::Increase => {
                    let mut parent = Vec::new();
                    core::mem::swap(&mut parent, &mut container);
                    parents.push(parent);
                    container.push(Tree::Leaf(token));
                }

                Nesting::Maintain => container.push(Tree::Leaf(token)),
                Nesting::Decrease => {
                    let mut parent = parents.pop().unwrap_or(Vec::new());
                    container.push(Tree::Leaf(token));
                    parent.push(Tree::Node(container));
                    container = parent;
                }
            }
        }

        while let Some(mut parent) = parents.pop() {
            parent.push(Tree::Node(container));
            container = parent;
        }

        Tree::Node(container)
    }
}