preserves_path/
context.rs

1use preserves::IOValue;
2use preserves::ReaderResult;
3
4use preserves_schema::compiler::load_schema_or_bundle;
5use preserves_schema::gen::schema::Definition;
6
7#[derive(Default)]
8pub struct Env(pub preserves_schema::support::interpret::Env<IOValue>);
9
10pub struct Context<'a> {
11    pub env: &'a Env,
12    path: Vec<IOValue>,
13}
14
15impl<'a> Context<'a> {
16    pub fn new(env: &'a Env) -> Self {
17        Context {
18            env,
19            path: Vec::new(),
20        }
21    }
22
23    pub fn with_path_step<R, F: FnOnce(&mut Self) -> R>(&mut self, v: &IOValue, f: F) -> R {
24        self.path.push(v.clone());
25        let result = f(self);
26        self.path.pop();
27        result
28    }
29}
30
31impl Env {
32    pub fn new() -> Self {
33        Default::default()
34    }
35
36    pub fn load_bundle(&mut self, filename: &std::path::PathBuf) -> ReaderResult<bool> {
37        load_schema_or_bundle(&mut self.0, filename)
38    }
39
40    pub fn lookup_definition(
41        &self,
42        module: &Vec<String>,
43        name: &str,
44    ) -> Option<&Definition<IOValue>> {
45        self.0.get(module).and_then(|s| s.definitions.0.get(name))
46    }
47
48    pub fn to_context(&self) -> Context<'_> {
49        Context::new(self)
50    }
51}