handlebars/
local_vars.rs

1use std::collections::BTreeMap;
2
3use serde_json::value::Value as Json;
4
5#[derive(Default, Debug, Clone)]
6pub struct LocalVars {
7    first: Option<Json>,
8    last: Option<Json>,
9    index: Option<Json>,
10    key: Option<Json>,
11
12    extra: BTreeMap<String, Json>,
13}
14
15impl LocalVars {
16    pub fn put(&mut self, key: &str, value: Json) {
17        match key {
18            "first" => self.first = Some(value),
19            "last" => self.last = Some(value),
20            "index" => self.index = Some(value),
21            "key" => self.key = Some(value),
22            _ => {
23                self.extra.insert(key.to_owned(), value);
24            }
25        }
26    }
27
28    pub fn get(&self, key: &str) -> Option<&Json> {
29        match key {
30            "first" => self.first.as_ref(),
31            "last" => self.last.as_ref(),
32            "index" => self.index.as_ref(),
33            "key" => self.key.as_ref(),
34            _ => self.extra.get(key),
35        }
36    }
37}