wild_doc_script/
stack.rs

1use std::{
2    ops::{Deref, DerefMut},
3    sync::Arc,
4};
5
6use crate::{Vars, WildDocValue};
7
8#[derive(Clone)]
9pub struct Stack {
10    vars: Vec<Vars>,
11}
12
13impl Stack {
14    pub fn new(inital: Option<Vars>) -> Self {
15        Self {
16            vars: inital.map_or(vec![], |v| [v].into()),
17        }
18    }
19
20    pub fn get(&self, key: &Arc<String>) -> Option<&WildDocValue> {
21        for vars in self.vars.iter().rev() {
22            if let Some(vars) = vars.get(key) {
23                return Some(vars);
24            }
25        }
26        None
27    }
28}
29
30impl Deref for Stack {
31    type Target = Vec<Vars>;
32
33    fn deref(&self) -> &Self::Target {
34        &self.vars
35    }
36}
37
38impl DerefMut for Stack {
39    fn deref_mut(&mut self) -> &mut Self::Target {
40        &mut self.vars
41    }
42}