-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathvfs.rs
97 lines (80 loc) · 3.14 KB
/
vfs.rs
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
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use crate::dict::Dict;
use crate::io::Input;
use crate::pathutil::NormalizedPathBuf;
use crate::shared::Shared;
/// In-memory virtual file system.
/// This is for cases where the contents of the code and the real file system do not link (e.g. ELS).
#[derive(Debug, Default)]
pub struct VirtualFileSystem {
cache: Shared<Dict<NormalizedPathBuf, String>>,
path_cache: Shared<Dict<(Input, PathBuf), Option<PathBuf>>>,
}
impl VirtualFileSystem {
pub fn new() -> Self {
Self {
cache: Shared::new(Dict::new()),
path_cache: Shared::new(Dict::new()),
}
}
pub fn update(&self, path: impl AsRef<Path>, contents: String) {
let path = NormalizedPathBuf::from(path.as_ref());
self.cache.borrow_mut().insert(path, contents);
}
pub fn cache_path(&self, input: Input, path: PathBuf, result: Option<PathBuf>) {
self.path_cache.borrow_mut().insert((input, path), result);
}
pub fn get_cached_path(&self, input: Input, path: PathBuf) -> Option<Option<PathBuf>> {
self.path_cache.borrow().get(&(input, path)).cloned()
}
pub fn remove(&self, path: impl AsRef<Path>) {
let path = NormalizedPathBuf::from(path.as_ref());
self.cache.borrow_mut().remove(&path);
}
pub fn rename(&self, from: impl AsRef<Path>, to: impl AsRef<Path>) {
let from = NormalizedPathBuf::from(from.as_ref());
let to = NormalizedPathBuf::from(to.as_ref());
let contents = self.cache.borrow_mut().remove(&from);
self.cache.borrow_mut().insert(to, contents.unwrap());
}
pub fn read(&self, path: impl AsRef<Path>) -> std::io::Result<String> {
let path = NormalizedPathBuf::from(path.as_ref());
if let Some(cache) = self.cache.borrow().get(&path) {
return Ok(cache.clone());
}
let contents = std::fs::read_to_string(&path)?;
self.cache
.borrow_mut()
.insert(path.clone(), contents.clone());
Ok(contents)
}
}
pub struct SharedVFS(OnceLock<VirtualFileSystem>);
impl SharedVFS {
pub fn read(&self, path: impl AsRef<Path>) -> std::io::Result<String> {
self.0.get_or_init(VirtualFileSystem::new).read(path)
}
pub fn update(&self, path: impl AsRef<Path>, contents: String) {
self.0
.get_or_init(VirtualFileSystem::new)
.update(path, contents)
}
pub fn rename(&self, from: impl AsRef<Path>, to: impl AsRef<Path>) {
self.0.get_or_init(VirtualFileSystem::new).rename(from, to)
}
pub fn remove(&self, path: impl AsRef<Path>) {
self.0.get_or_init(VirtualFileSystem::new).remove(path)
}
pub fn cache_path(&self, input: Input, path: PathBuf, result: Option<PathBuf>) {
self.0
.get_or_init(VirtualFileSystem::new)
.cache_path(input, path, result)
}
pub fn get_cached_path(&self, input: Input, path: PathBuf) -> Option<Option<PathBuf>> {
self.0
.get_or_init(VirtualFileSystem::new)
.get_cached_path(input, path)
}
}
pub static VFS: SharedVFS = SharedVFS(OnceLock::new());