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
use std::ops::{Deref, DerefMut};

use crate::{Vars, WildDocValue};

#[derive(Clone)]
pub struct Stack {
    vars: Vec<Vars>,
}

impl Stack {
    pub fn new(inital: Option<Vars>) -> Self {
        Self {
            vars: inital.map_or(vec![], |v| [v].into()),
        }
    }

    pub fn get(&self, key: &str) -> Option<&WildDocValue> {
        for vars in self.vars.iter().rev() {
            if let Some(vars) = vars.get(key) {
                return Some(vars);
            }
        }
        None
    }
}

impl Deref for Stack {
    type Target = Vec<Vars>;

    fn deref(&self) -> &Self::Target {
        &self.vars
    }
}

impl DerefMut for Stack {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.vars
    }
}