rust-analyzer/crates/vfs/src/loader.rs

72 lines
2 KiB
Rust
Raw Normal View History

2020-06-15 11:29:07 +00:00
//! Object safe interface for file watching and reading.
use std::fmt;
use paths::AbsPathBuf;
2020-06-11 09:04:09 +00:00
#[derive(Debug)]
2020-06-15 11:29:07 +00:00
pub enum Entry {
Files(Vec<AbsPathBuf>),
2020-06-11 09:04:09 +00:00
Directory { path: AbsPathBuf, include: Vec<String> },
2020-06-15 11:29:07 +00:00
}
2020-06-11 09:04:09 +00:00
#[derive(Debug)]
2020-06-15 11:29:07 +00:00
pub struct Config {
pub load: Vec<Entry>,
pub watch: Vec<usize>,
}
pub enum Message {
2020-06-11 09:04:09 +00:00
Progress { n_entries_total: usize, n_entries_done: usize },
2020-06-15 11:29:07 +00:00
Loaded { files: Vec<(AbsPathBuf, Option<Vec<u8>>)> },
}
pub type Sender = Box<dyn Fn(Message) + Send>;
pub trait Handle: fmt::Debug {
fn spawn(sender: Sender) -> Self
where
Self: Sized;
fn set_config(&mut self, config: Config);
fn invalidate(&mut self, path: AbsPathBuf);
fn load_sync(&mut self, path: &AbsPathBuf) -> Option<Vec<u8>>;
}
impl Entry {
pub fn rs_files_recursively(base: AbsPathBuf) -> Entry {
2020-06-11 09:04:09 +00:00
Entry::Directory { path: base, include: globs(&["*.rs", "!/.git/"]) }
2020-06-15 11:29:07 +00:00
}
pub fn local_cargo_package(base: AbsPathBuf) -> Entry {
2020-06-11 09:04:09 +00:00
Entry::Directory { path: base, include: globs(&["*.rs", "!/target/", "!/.git/"]) }
2020-06-15 11:29:07 +00:00
}
pub fn cargo_package_dependency(base: AbsPathBuf) -> Entry {
Entry::Directory {
path: base,
2020-06-11 09:04:09 +00:00
include: globs(&["*.rs", "!/tests/", "!/examples/", "!/benches/", "!/.git/"]),
2020-06-15 11:29:07 +00:00
}
}
}
fn globs(globs: &[&str]) -> Vec<String> {
globs.iter().map(|it| it.to_string()).collect()
}
impl fmt::Debug for Message {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Message::Loaded { files } => {
f.debug_struct("Loaded").field("n_files", &files.len()).finish()
}
2020-06-11 09:04:09 +00:00
Message::Progress { n_entries_total, n_entries_done } => f
.debug_struct("Progress")
.field("n_entries_total", n_entries_total)
.field("n_entries_done", n_entries_done)
.finish(),
2020-06-15 11:29:07 +00:00
}
}
}
#[test]
fn handle_is_object_safe() {
fn _assert(_: &dyn Handle) {}
}