1use ignore::gitignore::Gitignore;
2use std::path::Path;
3
4const IGNORE_FILE_NAME: &str = ".sqruffignore";
7
8pub struct IgnoreFile {
9 ignore: Gitignore,
10}
11
12impl IgnoreFile {
13 pub fn new_from_root(root: &Path) -> Result<Self, String> {
15 let ignore_file = root.join(IGNORE_FILE_NAME);
16 if ignore_file.exists() {
17 let ignore = Gitignore::new(ignore_file);
18 match ignore {
19 (ignore, None) => Ok(IgnoreFile { ignore }),
20 (_, Some(err)) => Err(err.to_string()),
21 }
22 } else {
23 Ok(Self::empty())
24 }
25 }
26
27 pub fn empty() -> Self {
28 Self {
29 ignore: Gitignore::empty(),
30 }
31 }
32
33 pub fn is_ignored(&self, path: &Path) -> bool {
35 let is_dir = path.is_dir();
36 let match_result = self.ignore.matched(path, is_dir);
37 let is_ignored = match_result.is_ignore();
38
39 if is_ignored {
40 let path_type = if is_dir { "directory" } else { "file" };
41 log::debug!(
42 "Ignoring {} '{}' due to ignore pattern",
43 path_type,
44 path.display()
45 );
46
47 if let Some(pattern) = match_result.inner() {
48 log::debug!("Matched ignore pattern: '{}'", pattern.original());
49 }
50 }
51
52 is_ignored
53 }
54}