expr/
context.rs

1use crate::Value;
2use indexmap::IndexMap;
3use std::fmt::Display;
4
5#[derive(Debug, Clone, Default)]
6pub struct Context(pub(crate) IndexMap<String, Value>);
7
8impl Context {
9    pub fn insert<K, V>(&mut self, key: K, value: V)
10    where
11        K: Into<String>,
12        V: Into<Value>,
13    {
14        self.0.insert(key.into(), value.into());
15    }
16
17    pub fn get(&self, key: &str) -> Option<&Value> {
18        self.0.get(key)
19    }
20}
21
22impl<S: Display, T: Into<Value>> FromIterator<(S, T)> for Context {
23    fn from_iter<I: IntoIterator<Item=(S, T)>>(iter: I) -> Self {
24        let mut ctx = Self::default();
25        for (k, v) in iter {
26            ctx.insert(k.to_string(), v);
27        }
28        ctx
29    }
30}