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
48
use crate::structure::rlt::new_types::Symbol;
use crate::structure::rlt::Reference;

pub struct StartsFromRoot;

#[derive(Debug, Clone, PartialEq)]
pub enum Context {
    Global {
        colon: Symbol
    },
    Local,
    Inner {
        parent: Box<Context>,
        needle: Reference
    }
}

impl Context {
    pub fn is_global(&self) -> bool {
        let mut current = self;
        
        loop {
            match current {
                Context::Global { .. } => return true,
                Context::Local => return false,
                Context::Inner { parent, .. } => {
                    current = parent.as_ref();
                    continue;
                }
            }
        }
    }
    
    pub fn unfold(self) -> (Option<StartsFromRoot>, Vec<Reference>) {
        let mut refs = vec![];
        let mut current = self;
        loop {
            match current {
                Context::Global { .. } => return (Some(StartsFromRoot), refs),
                Context::Local => return (None, refs),
                Context::Inner { needle, parent } => {
                    refs.push(needle);
                    current = *parent;
                }
            }
        }
    }
}