vfs crate scaffold

This commit is contained in:
Aleksey Kladov 2018-12-18 13:18:55 +03:00
parent 7509901fa0
commit 2ae05a6163
6 changed files with 89 additions and 68 deletions

View file

@ -6,67 +6,64 @@ use std::{
use walkdir::WalkDir;
use crossbeam_channel::{Sender, Receiver};
use thread_worker::{WorkerHandle, Worker};
pub(crate) fn start_io() -> (JoinHandle<(), Sender<()>, Receiver()>) {}
#[derive(Debug)]
pub struct FileEvent {
pub path: PathBuf,
pub kind: FileEventKind,
}
// use crate::thread_watcher::{ThreadWatcher, Worker};
#[derive(Debug)]
pub enum FileEventKind {
Add(String),
}
// #[derive(Debug)]
// pub struct FileEvent {
// pub path: PathBuf,
// pub kind: FileEventKind,
// }
pub fn start() -> (Worker<PathBuf, (PathBuf, Vec<FileEvent>)>, WorkerHandle) {
thread_worker::spawn::<PathBuf, (PathBuf, Vec<FileEvent>), _>(
"vfs",
128,
|input_receiver, output_sender| {
input_receiver
.map(|path| {
log::debug!("loading {} ...", path.as_path().display());
let events = load_root(path.as_path());
log::debug!("... loaded {}", path.as_path().display());
(path, events)
})
.for_each(|it| output_sender.send(it))
},
)
}
// #[derive(Debug)]
// pub enum FileEventKind {
// Add(String),
// }
// pub fn roots_loader() -> (Worker<PathBuf, (PathBuf, Vec<FileEvent>)>, ThreadWatcher) {
// Worker::<PathBuf, (PathBuf, Vec<FileEvent>)>::spawn(
// "roots loader",
// 128,
// |input_receiver, output_sender| {
// input_receiver
// .map(|path| {
// log::debug!("loading {} ...", path.as_path().display());
// let events = load_root(path.as_path());
// log::debug!("... loaded {}", path.as_path().display());
// (path, events)
// })
// .for_each(|it| output_sender.send(it))
// },
// )
// }
// 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) => {
// log::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) => {
// log::warn!("watcher error: {}", e);
// continue;
// }
// };
// res.push(FileEvent {
// path: path.to_owned(),
// kind: FileEventKind::Add(text),
// })
// }
// res
// }
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) => {
log::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) => {
log::warn!("watcher error: {}", e);
continue;
}
};
res.push(FileEvent {
path: path.to_owned(),
kind: FileEventKind::Add(text),
})
}
res
}