aboutsummaryrefslogtreecommitdiff
path: root/crates/vfs-notify/src/include.rs
blob: 59da3d77a10b9779219d576c99fa82eebed9bfec (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
//! 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 including file globs
/// and recursively excluding 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))
    }
}