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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use std::collections::{BTreeMap, BTreeSet};
use std::fs::File;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::rc::Rc;
use super::cache;
use super::pointer::OpPointer;
use super::Error;
use super::Value;
use crate::build::AssertCollector;
use crate::build::Val;
use crate::convert::{ConverterRegistry, ImporterRegistry};
use crate::iter::OffsetStrIter;
use crate::parse::parse;
pub struct Environment<Stdout, Stderr>
where
Stdout: Write + Clone,
Stderr: Write + Clone,
{
pub val_cache: BTreeMap<String, Rc<Value>>,
pub op_cache: cache::Ops,
pub converter_registry: ConverterRegistry,
pub importer_registry: ImporterRegistry,
pub assert_results: AssertCollector,
pub stdout: Stdout,
pub stderr: Stderr,
pub env_vars: BTreeMap<String, String>,
pub out_lock: BTreeSet<PathBuf>,
}
impl<Stdout: Write + Clone, Stderr: Write + Clone> Environment<Stdout, Stderr> {
pub fn new(out: Stdout, err: Stderr) -> Self {
Self::new_with_vars(out, err, BTreeMap::new())
}
pub fn new_with_vars(out: Stdout, err: Stderr, vars: BTreeMap<String, String>) -> Self {
Self {
val_cache: BTreeMap::new(),
env_vars: vars,
op_cache: cache::Ops::new(),
assert_results: AssertCollector::new(),
converter_registry: ConverterRegistry::make_registry(),
importer_registry: ImporterRegistry::make_registry(),
stdout: out,
stderr: err,
out_lock: BTreeSet::new(),
}
}
pub fn get_cached_path_val(&self, path: &String) -> Option<Rc<Value>> {
self.val_cache.get(path).cloned()
}
pub fn update_path_val(&mut self, path: &String, val: Rc<Value>) {
self.val_cache.insert(path.clone(), val);
}
pub fn get_ops_for_path<P>(&mut self, path: P) -> Result<OpPointer, Error>
where
P: Into<PathBuf> + Clone,
{
let path_copy = path.clone();
self.op_cache.entry(path.clone()).get_pointer_or_else(
|| {
let p = path.into();
let root = p.parent().unwrap();
let mut f = File::open(&p)?;
let mut contents = String::new();
f.read_to_string(&mut contents)?;
let iter = OffsetStrIter::new(&contents).with_src_file(&p);
let stmts = parse(iter, None).unwrap();
let ops = super::translate::AST::translate(stmts, &root);
Ok(ops)
},
path_copy,
)
}
pub fn record_assert_result(&mut self, desc: &str, ok: bool) {
self.assert_results.record_assert_result(desc, ok);
}
pub fn get_out_lock_for_path<P: AsRef<Path>>(&self, path: P) -> bool {
self.out_lock.contains(path.as_ref())
}
pub fn set_out_lock_for_path<P: Into<PathBuf>>(&mut self, path: P) {
self.out_lock.insert(path.into());
}
pub fn reset_out_lock_for_path<P: AsRef<Path>>(&mut self, path: P) {
self.out_lock.remove(path.as_ref());
}
pub fn stdout(&self) -> Stdout {
self.stdout.clone()
}
pub fn stderr(&self) -> Stderr {
self.stderr.clone()
}
pub fn convert_val(&mut self, typ: &str, writer: &mut dyn Write, val: Rc<Val>) -> bool {
match self.converter_registry.get_converter(typ) {
Some(c) => {
if let Err(e) = c.convert(val, writer) {
writeln!(&mut self.stderr, "{}", e).unwrap();
return false;
}
}
None => {
writeln!(
&mut self.stderr,
"No such format {}\nrun `ucg converters` to see available formats.",
typ
)
.unwrap();
return false;
}
}
return true;
}
}