aboutsummaryrefslogtreecommitdiff
path: root/crates/server/src/vfs.rs
blob: 69a7654af21e01a1f70d857fcbf3339849ada7c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use std::{
    path::{PathBuf, Path},
    fs,
};

use crossbeam_channel::{Sender, Receiver, bounded};
use walkdir::WalkDir;

use {
    thread_watcher::ThreadWatcher,
};


#[derive(Debug)]
pub struct FileEvent {
    pub path: PathBuf,
    pub kind: FileEventKind,
}

#[derive(Debug)]
pub enum FileEventKind {
    Add(String),
}

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)
            })
            .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
}