rust/crates/vfs/src/loader.rs

72 lines
1.9 KiB
Rust
Raw Normal View History

2020-06-15 06:29:07 -05:00
//! Object safe interface for file watching and reading.
use std::fmt;
2020-06-24 08:52:07 -05:00
use paths::{AbsPath, AbsPathBuf};
2020-06-15 06:29:07 -05:00
2020-06-11 04:04:09 -05:00
#[derive(Debug)]
2020-06-15 06:29:07 -05:00
pub enum Entry {
Files(Vec<AbsPathBuf>),
2020-06-11 04:04:09 -05:00
Directory { path: AbsPathBuf, include: Vec<String> },
2020-06-15 06:29:07 -05:00
}
2020-06-11 04:04:09 -05:00
#[derive(Debug)]
2020-06-15 06:29:07 -05:00
pub struct Config {
pub load: Vec<Entry>,
pub watch: Vec<usize>,
}
pub enum Message {
2020-06-24 09:58:49 -05:00
Progress { n_total: usize, n_done: usize },
2020-06-15 06:29:07 -05: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);
2020-06-24 08:52:07 -05:00
fn load_sync(&mut self, path: &AbsPath) -> Option<Vec<u8>>;
2020-06-15 06:29:07 -05:00
}
impl Entry {
pub fn rs_files_recursively(base: AbsPathBuf) -> Entry {
2020-06-11 04:04:09 -05:00
Entry::Directory { path: base, include: globs(&["*.rs", "!/.git/"]) }
2020-06-15 06:29:07 -05:00
}
pub fn local_cargo_package(base: AbsPathBuf) -> Entry {
2020-06-11 04:04:09 -05:00
Entry::Directory { path: base, include: globs(&["*.rs", "!/target/", "!/.git/"]) }
2020-06-15 06:29:07 -05:00
}
pub fn cargo_package_dependency(base: AbsPathBuf) -> Entry {
Entry::Directory {
path: base,
2020-06-11 04:04:09 -05:00
include: globs(&["*.rs", "!/tests/", "!/examples/", "!/benches/", "!/.git/"]),
2020-06-15 06:29:07 -05: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-24 09:58:49 -05:00
Message::Progress { n_total, n_done } => f
2020-06-11 04:04:09 -05:00
.debug_struct("Progress")
2020-06-24 09:58:49 -05:00
.field("n_total", n_total)
.field("n_done", n_done)
2020-06-11 04:04:09 -05:00
.finish(),
2020-06-15 06:29:07 -05:00
}
}
}
#[test]
fn handle_is_object_safe() {
fn _assert(_: &dyn Handle) {}
}