summaryrefslogtreecommitdiffstats
path: root/src/search/entry.rs
blob: 1e49a67d1797d2cadba0850d1ea62e37d4664b58 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use ignore::{Walk, WalkBuilder};
use tracing::{error, warn};

use crate::{
    project::{path::PathMatcher, ProjectParser},
    search::ProjectItem,
};

pub use config::Config;

mod config;

pub struct Entry {
    path_parser: Option<PathMatcher>,

    #[cfg(feature = "git")]
    git_parser: Option<crate::project::git::GitMatcher>,

    iter: Walk,
}

impl std::fmt::Debug for Entry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut debug = f.debug_struct("Entry");
        debug.field("path_matcher", &self.path_parser);

        #[cfg(feature = "git")]
        debug.field("git_matcher", &self.git_parser);

        debug.finish()
    }
}

impl Entry {
    fn new(config: &Config) -> Self {
        let iter = WalkBuilder::new(&config.path_buf)
            .standard_filters(true)
            .max_depth(config.max_depth)
            .hidden(!config.hidden)
            .build();

        Self {
            iter,
            path_parser: config.pattern.as_ref().map(|s| PathMatcher(s.to_owned())),

            #[cfg(feature = "git")]
            git_parser: config.git.then_some(crate::project::git::GitMatcher),
        }
    }
}

impl ProjectParser for Entry {
    #[tracing::instrument]
    fn parse_project(&self, path_buf: std::path::PathBuf) -> Option<ProjectItem> {
        #[cfg(feature = "git")]
        if let Some(p) = self
            .git_parser
            .as_ref()
            .and_then(|m| m.parse_project(path_buf.to_owned()))
        {
            return Some(p);
        };

        if let Some(p) = self
            .path_parser
            .as_ref()
            .and_then(|m| m.parse_project(path_buf))
        {
            return Some(p);
        };

        None
    }
}

impl From<Config> for Entry {
    fn from(config: Config) -> Self {
        Self::new(&config)
    }
}

impl Iterator for Entry {
    type Item = ProjectItem;

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