summaryrefslogtreecommitdiffstats
path: root/src/search/entry.rs
blob: 1d885c880bda0cc5b38d2b5f77e230f6dfa59c94 (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
use ignore::{DirEntry, Walk};
use tracing::error;

use crate::project::{GitProject, PathProject, ProjectItem};

pub use config::Config;

mod config;

pub struct Entry {
    config: Config,
    iter: Walk,
}

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

impl Entry {
    pub fn parse_dir_entry(&self, dir_entry: DirEntry) -> Option<ProjectItem> {
        if self.config.git {
            if let Ok(git) = GitProject::try_from(dir_entry.to_owned()) {
                return Some(Box::new(git));
            };
        };

        if let Some(pattern) = &self.config.pattern {
            if let Ok(proj) = PathProject::try_from((pattern, dir_entry)) {
                return Some(Box::new(proj));
            };
        };

        None
    }
}

impl Iterator for Entry {
    type Item = ProjectItem;

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