1use std::collections::BTreeMap;
2use std::path::{Path, PathBuf};
3use std::sync::RwLock;
4use std::sync::atomic::{AtomicU64, Ordering};
5
6use super::{Stamp, Vfs, VfsError};
7
8#[derive(Debug, Default)]
11pub struct MemoryVfs {
12 files: RwLock<BTreeMap<PathBuf, Entry>>,
13 versions: AtomicU64,
14}
15
16#[derive(Debug)]
17struct Entry {
18 contents: String,
19 version: u64,
20}
21
22impl MemoryVfs {
23 pub fn new() -> Self {
24 Self::default()
25 }
26
27 pub fn with_file(self, path: impl Into<PathBuf>, contents: impl Into<String>) -> Self {
29 self.write(&path.into(), &contents.into())
30 .expect("MemoryVfs write cannot fail");
31 self
32 }
33
34 fn entry(&self, contents: String) -> Entry {
35 Entry {
36 contents,
37 version: self.versions.fetch_add(1, Ordering::Relaxed),
38 }
39 }
40}
41
42impl<P: Into<PathBuf>, S: Into<String>> FromIterator<(P, S)> for MemoryVfs {
43 fn from_iter<I: IntoIterator<Item = (P, S)>>(iter: I) -> Self {
44 iter.into_iter()
45 .fold(MemoryVfs::new(), |vfs, (path, contents)| {
46 vfs.with_file(path, contents)
47 })
48 }
49}
50
51impl Vfs for MemoryVfs {
52 fn read(&self, path: &Path) -> Result<String, VfsError> {
53 self.files
54 .read()
55 .expect("MemoryVfs lock poisoned")
56 .get(path)
57 .map(|e| e.contents.clone())
58 .ok_or_else(|| VfsError::NotFound(path.to_path_buf()))
59 }
60
61 fn stamp(&self, path: &Path) -> Result<Stamp, VfsError> {
62 self.files
63 .read()
64 .expect("MemoryVfs lock poisoned")
65 .get(path)
66 .map(|e| Stamp::new(u128::from(e.version), e.contents.len() as u64))
67 .ok_or_else(|| VfsError::NotFound(path.to_path_buf()))
68 }
69
70 fn write(&self, path: &Path, contents: &str) -> Result<(), VfsError> {
71 let entry = self.entry(contents.to_owned());
72 self.files
73 .write()
74 .expect("MemoryVfs lock poisoned")
75 .insert(path.to_path_buf(), entry);
76 Ok(())
77 }
78
79 fn exists(&self, path: &Path) -> bool {
80 self.files
81 .read()
82 .expect("MemoryVfs lock poisoned")
83 .contains_key(path)
84 }
85
86 fn rename(&self, from: &Path, to: &Path) -> Result<(), VfsError> {
87 let mut files = self.files.write().expect("MemoryVfs lock poisoned");
88 let moved = files
89 .remove(from)
90 .ok_or_else(|| VfsError::NotFound(from.to_path_buf()))?;
91 let entry = Entry {
92 contents: moved.contents,
93 version: self.versions.fetch_add(1, Ordering::Relaxed),
94 };
95 files.insert(to.to_path_buf(), entry);
96 Ok(())
97 }
98
99 fn walk(&self, root: &Path) -> Result<Vec<PathBuf>, VfsError> {
100 Ok(self
101 .files
102 .read()
103 .expect("MemoryVfs lock poisoned")
104 .keys()
105 .filter(|path| path.starts_with(root))
106 .cloned()
107 .collect())
108 }
109}
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114
115 #[test]
116 fn walk_is_scoped_and_sorted() {
117 let vfs = MemoryVfs::new()
118 .with_file("/ws/b.rs", "")
119 .with_file("/ws/a.rs", "")
120 .with_file("/other/c.rs", "");
121 let files = vfs.walk(Path::new("/ws")).unwrap();
122 assert_eq!(
123 files,
124 vec![PathBuf::from("/ws/a.rs"), PathBuf::from("/ws/b.rs")]
125 );
126 }
127}