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
use crate::{
error::{Error::*, Result},
throw,
};
use fs::File;
use jrsonnet_interner::IStr;
use std::fs;
use std::io::Read;
use std::{
any::Any,
cell::RefCell,
collections::HashMap,
path::{Path, PathBuf},
rc::Rc,
};
pub trait ImportResolver {
fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>>;
fn load_file_contents(&self, resolved: &Path) -> Result<IStr>;
unsafe fn as_any(&self) -> &dyn Any;
}
pub struct DummyImportResolver;
impl ImportResolver for DummyImportResolver {
fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
throw!(ImportNotSupported(from.into(), path.into()))
}
fn load_file_contents(&self, _resolved: &Path) -> Result<IStr> {
panic!("dummy resolver can't load any file")
}
unsafe fn as_any(&self) -> &dyn Any {
panic!("`as_any($self)` is not supported by dummy resolver")
}
}
#[allow(clippy::use_self)]
impl Default for Box<dyn ImportResolver> {
fn default() -> Self {
Box::new(DummyImportResolver)
}
}
#[derive(Default)]
pub struct FileImportResolver {
pub library_paths: Vec<PathBuf>,
}
impl ImportResolver for FileImportResolver {
fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
let mut direct = from.to_path_buf();
direct.push(path);
if direct.exists() {
Ok(direct.into())
} else {
for library_path in self.library_paths.iter() {
let mut cloned = library_path.clone();
cloned.push(path);
if cloned.exists() {
return Ok(cloned.into());
}
}
throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
}
}
fn load_file_contents(&self, id: &Path) -> Result<IStr> {
let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?;
let mut out = String::new();
file.read_to_string(&mut out)
.map_err(|_e| ImportBadFileUtf8(id.to_owned()))?;
Ok(out.into())
}
unsafe fn as_any(&self) -> &dyn Any {
panic!("this resolver can't be used as any")
}
}
type ResolutionData = (PathBuf, PathBuf);
pub struct CachingImportResolver {
resolution_cache: RefCell<HashMap<ResolutionData, Result<Rc<Path>>>>,
loading_cache: RefCell<HashMap<PathBuf, Result<IStr>>>,
inner: Box<dyn ImportResolver>,
}
impl ImportResolver for CachingImportResolver {
fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
self.resolution_cache
.borrow_mut()
.entry((from.to_owned(), path.to_owned()))
.or_insert_with(|| self.inner.resolve_file(from, path))
.clone()
}
fn load_file_contents(&self, resolved: &Path) -> Result<IStr> {
self.loading_cache
.borrow_mut()
.entry(resolved.to_owned())
.or_insert_with(|| self.inner.load_file_contents(resolved))
.clone()
}
unsafe fn as_any(&self) -> &dyn Any {
panic!("this resolver can't be used as any")
}
}