aboutsummaryrefslogtreecommitdiff
path: root/crates/vfs-notify/src/include.rs
blob: 7378766f51dad47be6d1b1f0957847165d5ba851 (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
//! See `Include`.

use std::convert::TryFrom;

use globset::{Glob, GlobSet, GlobSetBuilder};
use paths::{RelPath, RelPathBuf};

/// `Include` is the opposite of .gitignore.
///
/// It describes the set of files inside some directory.
///
/// The current implementation is very limited, it allows white-listing file
/// globs and black-listing directories.
#[derive(Debug, Clone)]
pub(crate) struct Include {
    include_files: GlobSet,
    exclude_dirs: Vec<RelPathBuf>,
}

impl Include {
    pub(crate) fn new(include: Vec<String>) -> Include {
        let mut include_files = GlobSetBuilder::new();
        let mut exclude_dirs = Vec::new();

        for glob in include {
            if glob.starts_with("!/") {
                if let Ok(path) = RelPathBuf::try_from(&glob["!/".len()..]) {
                    exclude_dirs.push(path)
                }
            } else {
                include_files.add(Glob::new(&glob).unwrap());
            }
        }
        let include_files = include_files.build().unwrap();
        Include { include_files, exclude_dirs }
    }
    pub(crate) fn include_file(&self, path: &RelPath) -> bool {
        self.include_files.is_match(path)
    }
    pub(crate) fn exclude_dir(&self, path: &RelPath) -> bool {
        self.exclude_dirs.iter().any(|excluded| path.starts_with(excluded))
    }
}