Index deps

This commit is contained in:
Aleksey Kladov 2018-09-03 21:03:37 +03:00
parent b04c14d4ad
commit 47cbaeba6f
7 changed files with 128 additions and 84 deletions

View file

@ -1,5 +1,5 @@
use std::{
path::PathBuf,
path::{PathBuf, Path},
fs,
};
@ -20,46 +20,54 @@ pub struct FileEvent {
#[derive(Debug)]
pub enum FileEventKind {
Add(String),
#[allow(unused)]
Remove,
}
pub fn watch(roots: Vec<PathBuf>) -> (Receiver<Vec<FileEvent>>, ThreadWatcher) {
let (sender, receiver) = bounded(16);
let watcher = ThreadWatcher::spawn("vfs", move || run(roots, sender));
(receiver, watcher)
}
fn run(roots: Vec<PathBuf>, sender: Sender<Vec<FileEvent>>) {
for root in roots {
let mut events = Vec::new();
for entry in WalkDir::new(root.as_path()) {
let entry = match entry {
Ok(entry) => entry,
Err(e) => {
warn!("watcher error: {}", e);
continue;
}
};
if !entry.file_type().is_file() {
continue;
}
let path = entry.path();
if path.extension().and_then(|os| os.to_str()) != Some("rs") {
continue;
}
let text = match fs::read_to_string(path) {
Ok(text) => text,
Err(e) => {
warn!("watcher error: {}", e);
continue;
}
};
events.push(FileEvent {
path: path.to_owned(),
kind: FileEventKind::Add(text),
pub fn roots_loader() -> (Sender<PathBuf>, Receiver<(PathBuf, Vec<FileEvent>)>, ThreadWatcher) {
let (path_sender, path_receiver) = bounded::<PathBuf>(2048);
let (event_sender, event_receiver) = bounded::<(PathBuf, Vec<FileEvent>)>(1);
let thread = ThreadWatcher::spawn("roots loader", move || {
path_receiver
.into_iter()
.map(|path| {
debug!("loading {} ...", path.as_path().display());
let events = load_root(path.as_path());
debug!("... loaded {}", path.as_path().display());
(path, events)
})
}
sender.send(events)
}
.for_each(|it| event_sender.send(it))
});
(path_sender, event_receiver, thread)
}
fn load_root(path: &Path) -> Vec<FileEvent> {
let mut res = Vec::new();
for entry in WalkDir::new(path) {
let entry = match entry {
Ok(entry) => entry,
Err(e) => {
warn!("watcher error: {}", e);
continue;
}
};
if !entry.file_type().is_file() {
continue;
}
let path = entry.path();
if path.extension().and_then(|os| os.to_str()) != Some("rs") {
continue;
}
let text = match fs::read_to_string(path) {
Ok(text) => text,
Err(e) => {
warn!("watcher error: {}", e);
continue;
}
};
res.push(FileEvent {
path: path.to_owned(),
kind: FileEventKind::Add(text),
})
}
res
}