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