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

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

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<Box<dyn Project<Error = crate::project::Error>>> {
        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 = Box<dyn Project<Error = crate::project::Error>>;

    #[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");
                self.next()
            }
        }
    }
}