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