summaryrefslogtreecommitdiffstats
path: root/src/config.rs
blob: cc1f28f888cdb38dbd8184336a57040e3529c7cb (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
use serde::{Deserialize, Serialize};
use std::{convert::Infallible, path::PathBuf, str::FromStr};

#[serde_with::serde_as]
#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)]
pub struct Config {
    #[serde_as(as = "Vec<serde_with::PickFirst<(_, serde_with::DisplayFromStr)>>")]
    pub paths: Vec<SearchEntryConfig>,
}

#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct SearchEntryConfig {
    pub path_buf: PathBuf,
    pub hidden: bool,
    pub max_depth: Option<usize>,
    pub pattern: Option<String>,
    #[cfg(feature = "git")]
    pub git: bool,
}

impl SearchEntryConfig {
    pub fn new(path_buf: PathBuf) -> Self {
        Self {
            path_buf,
            ..Default::default()
        }
    }
}

impl From<PathBuf> for SearchEntryConfig {
    fn from(path_buf: PathBuf) -> Self {
        Self::new(path_buf)
    }
}

impl FromStr for SearchEntryConfig {
    type Err = Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        s.parse().map(|path_buf| Self {
            path_buf,
            ..Default::default()
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;

    #[test]
    fn test_extract_config() {
        let s = r#"
            paths = [
                "/path/to/projects",
                { path_buf = "/path/to/other_projects", hidden = true, max_depth = 1 },
                { path_buf = "/path/to/another_project", max_depth = 0 }
            ]
        "#;

        let config: Config = toml::from_str(s).unwrap();

        assert_eq!(
            config,
            Config {
                paths: Vec::from([
                    SearchEntryConfig {
                        path_buf: "/path/to/projects".into(),
                        hidden: false,
                        max_depth: None,
                        ..Default::default()
                    },
                    SearchEntryConfig {
                        path_buf: "/path/to/other_projects".into(),
                        hidden: true,
                        max_depth: Some(1),
                        ..Default::default()
                    },
                    SearchEntryConfig {
                        path_buf: "/path/to/another_project".into(),
                        hidden: false,
                        max_depth: Some(0),
                        ..Default::default()
                    },
                ]),
            }
        );
    }
}