summaryrefslogtreecommitdiffstats
path: root/src/search/entry.rs
blob: efd287bd46c5747d386adc5efb55222afcae3c51 (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;

use ignore::{Walk, WalkBuilder};
use tracing::{debug, error};

use crate::{
    config::Filters,
    project::{path::PathMatcher, Project, ProjectParserGroup},
};

pub struct SearchPath {
    path_buf: PathBuf,
    parsers: ProjectParserGroup,
    iter: Walk,
}

impl std::fmt::Debug for SearchPath {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SearchPath")
            .field("path_buf", &self.path_buf)
            .finish()
    }
}

impl SearchPath {
    pub fn new(path_buf: PathBuf, config: &Filters) -> Self {
        let mut parsers = ProjectParserGroup::new();

        if config.all {
            parsers.push(Box::new(PathMatcher::All))
        }

        if let Some(s) = config.pattern.as_ref() {
            parsers.push(Box::new(PathMatcher::Pattern(s.to_owned())));
        };

        #[cfg(feature = "git")]
        if config.git {
            parsers.push(Box::new(crate::project::git::GitMatcher));
        };

        let iter = WalkBuilder::new(&path_buf)
            .standard_filters(true)
            .max_depth(config.max_depth)
            .hidden(!config.hidden)
            .build();

        Self {
            path_buf,
            parsers,
            iter,
        }
    }
}

impl Iterator for SearchPath {
    type Item = Project;

    fn next(&mut self) -> Option<Self::Item> {
        match self.iter.next()? {
            Ok(dir_entry) if dir_entry.path() == self.path_buf => {
                debug!("Ignoring parent directory");
                None
            }
            Ok(dir_entry) => self.parsers.parse(dir_entry.into_path()),
            Err(err) => {
                error!(%err, "Ignoring errored path");
                None
            }
        }
        .or_else(|| self.next())
    }
}