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
use crate::errors::*;
use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
#[derive(Clone, Debug, Default, Serialize)]
pub struct PatchedBuiltinsMap {
pub env: HashMap<String, String>,
}
impl PatchedBuiltinsMap {
pub fn with_capacity(capacity: usize) -> Self {
PatchedBuiltinsMap {
env: HashMap::with_capacity(capacity),
}
}
pub fn insert(&mut self, name: String, imported_name: String) -> Option<String> {
self.env.insert(name, imported_name)
}
pub fn write_to_file<P: AsRef<Path>>(
&self,
builtins_map_path: P,
original_names: bool,
) -> Result<(), WError> {
let mut map_with_original_names;
let map = if original_names {
self
} else {
map_with_original_names = PatchedBuiltinsMap::default();
for imported_name in self.env.values() {
map_with_original_names
.env
.insert(imported_name.clone(), imported_name.clone());
}
&map_with_original_names
};
let json = serde_json::to_string_pretty(map).map_err(|_| WError::ParseError)?;
File::create(builtins_map_path)?.write_all(json.as_bytes())?;
Ok(())
}
pub fn builtins_map(
&self,
module: &str,
original_names: bool,
) -> Result<HashMap<String, String>, Error> {
if module != "env" {
bail!(WError::UsageError("Empty module"));
}
if original_names {
return Ok(self.env.clone());
}
let mut env_map_with_original_names = HashMap::new();
for imported_name in self.env.values() {
env_map_with_original_names.insert(imported_name.clone(), imported_name.clone());
}
Ok(env_map_with_original_names)
}
}