diff options
Diffstat (limited to 'crates/vfs/src/loader.rs')
-rw-r--r-- | crates/vfs/src/loader.rs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/crates/vfs/src/loader.rs b/crates/vfs/src/loader.rs new file mode 100644 index 000000000..6de2e5b3f --- /dev/null +++ b/crates/vfs/src/loader.rs | |||
@@ -0,0 +1,71 @@ | |||
1 | //! Object safe interface for file watching and reading. | ||
2 | use std::fmt; | ||
3 | |||
4 | use paths::{AbsPath, AbsPathBuf}; | ||
5 | |||
6 | #[derive(Debug)] | ||
7 | pub enum Entry { | ||
8 | Files(Vec<AbsPathBuf>), | ||
9 | Directory { path: AbsPathBuf, include: Vec<String> }, | ||
10 | } | ||
11 | |||
12 | #[derive(Debug)] | ||
13 | pub struct Config { | ||
14 | pub load: Vec<Entry>, | ||
15 | pub watch: Vec<usize>, | ||
16 | } | ||
17 | |||
18 | pub enum Message { | ||
19 | Progress { n_total: usize, n_done: usize }, | ||
20 | Loaded { files: Vec<(AbsPathBuf, Option<Vec<u8>>)> }, | ||
21 | } | ||
22 | |||
23 | pub type Sender = Box<dyn Fn(Message) + Send>; | ||
24 | |||
25 | pub trait Handle: fmt::Debug { | ||
26 | fn spawn(sender: Sender) -> Self | ||
27 | where | ||
28 | Self: Sized; | ||
29 | fn set_config(&mut self, config: Config); | ||
30 | fn invalidate(&mut self, path: AbsPathBuf); | ||
31 | fn load_sync(&mut self, path: &AbsPath) -> Option<Vec<u8>>; | ||
32 | } | ||
33 | |||
34 | impl Entry { | ||
35 | pub fn rs_files_recursively(base: AbsPathBuf) -> Entry { | ||
36 | Entry::Directory { path: base, include: globs(&["*.rs", "!/.git/"]) } | ||
37 | } | ||
38 | pub fn local_cargo_package(base: AbsPathBuf) -> Entry { | ||
39 | Entry::Directory { path: base, include: globs(&["*.rs", "!/target/", "!/.git/"]) } | ||
40 | } | ||
41 | pub fn cargo_package_dependency(base: AbsPathBuf) -> Entry { | ||
42 | Entry::Directory { | ||
43 | path: base, | ||
44 | include: globs(&["*.rs", "!/tests/", "!/examples/", "!/benches/", "!/.git/"]), | ||
45 | } | ||
46 | } | ||
47 | } | ||
48 | |||
49 | fn globs(globs: &[&str]) -> Vec<String> { | ||
50 | globs.iter().map(|it| it.to_string()).collect() | ||
51 | } | ||
52 | |||
53 | impl fmt::Debug for Message { | ||
54 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
55 | match self { | ||
56 | Message::Loaded { files } => { | ||
57 | f.debug_struct("Loaded").field("n_files", &files.len()).finish() | ||
58 | } | ||
59 | Message::Progress { n_total, n_done } => f | ||
60 | .debug_struct("Progress") | ||
61 | .field("n_total", n_total) | ||
62 | .field("n_done", n_done) | ||
63 | .finish(), | ||
64 | } | ||
65 | } | ||
66 | } | ||
67 | |||
68 | #[test] | ||
69 | fn handle_is_object_safe() { | ||
70 | fn _assert(_: &dyn Handle) {} | ||
71 | } | ||