kodept_core/structure/rlt/
context.rs

1use crate::structure::rlt::new_types::Symbol;
2use crate::structure::rlt::Reference;
3
4pub struct StartsFromRoot;
5
6#[derive(Debug, Clone, PartialEq)]
7pub enum Context {
8    Global {
9        colon: Symbol
10    },
11    Local,
12    Inner {
13        parent: Box<Context>,
14        needle: Reference
15    }
16}
17
18impl Context {
19    pub fn is_global(&self) -> bool {
20        let mut current = self;
21        
22        loop {
23            match current {
24                Context::Global { .. } => return true,
25                Context::Local => return false,
26                Context::Inner { parent, .. } => {
27                    current = parent.as_ref();
28                    continue;
29                }
30            }
31        }
32    }
33    
34    pub fn unfold(self) -> (Option<StartsFromRoot>, Vec<Reference>) {
35        let mut refs = vec![];
36        let mut current = self;
37        loop {
38            match current {
39                Context::Global { .. } => return (Some(StartsFromRoot), refs),
40                Context::Local => return (None, refs),
41                Context::Inner { needle, parent } => {
42                    refs.push(needle);
43                    current = *parent;
44                }
45            }
46        }
47    }
48}