expr/
context.rs

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
use crate::Value;
use indexmap::IndexMap;
use std::fmt::Display;

#[derive(Debug, Clone, Default)]
pub struct Context(pub(crate) IndexMap<String, Value>);

impl Context {
    pub fn insert<K, V>(&mut self, key: K, value: V)
    where
        K: Into<String>,
        V: Into<Value>,
    {
        self.0.insert(key.into(), value.into());
    }

    pub fn get(&self, key: &str) -> Option<&Value> {
        self.0.get(key)
    }
}

impl<S: Display, T: Into<Value>> FromIterator<(S, T)> for Context {
    fn from_iter<I: IntoIterator<Item=(S, T)>>(iter: I) -> Self {
        let mut ctx = Self::default();
        for (k, v) in iter {
            ctx.insert(k.to_string(), v);
        }
        ctx
    }
}