Skip to main content

arui_core/tree/
config.rs

1//! # 项目树行为配置
2//! 通过设置配置字段,我们可以控制构建、分析等操作中的细节行为,比如排除符合某规则的路径、忽略某文件的总结信息。
3use derive_builder::Builder;
4
5/// 项目树配置对象
6/// - `include` 需要包含的路径的规则
7/// - `eclude` 需要排除的路径的规则
8#[derive(Default, Debug, Builder, PartialEq, Clone)]
9#[builder(default, setter(into))]
10pub struct ProjectConfig {
11    /// 需要包含的路径
12    pub include: Vec<String>,
13    /// 需要排除的路径
14    pub exclude: Vec<String>,
15}
16
17impl ProjectConfig {
18    /// 以默认值填充创建一个项目配置对象
19    pub fn new() -> Self {
20        Self::default()
21    }
22
23    /// 添加单个 include(接受 &str 或 String)
24    pub fn add_include<S: Into<String>>(mut self, include: S) -> Self {
25        self.include.push(include.into());
26        self
27    }
28
29    /// 添加需要被包含的路径
30    pub fn add_includes<I, S>(mut self, includes: I) -> Self
31    where
32        I: IntoIterator<Item = S>,
33        S: Into<String>,
34    {
35        self.include.extend(includes.into_iter().map(Into::into));
36        self
37    }
38
39    /// 添加单个 exclude(接受 &str 或 String)
40    pub fn add_exclude<S: Into<String>>(mut self, exclude: S) -> Self {
41        self.exclude.push(exclude.into());
42        self
43    }
44
45    /// 添加需要被忽略的路径
46    pub fn add_excludes<I, S>(mut self, excludes: I) -> Self
47    where
48        I: IntoIterator<Item = S>,
49        S: Into<String>,
50    {
51        self.exclude.extend(excludes.into_iter().map(Into::into));
52        self
53    }
54
55    /// 清空需要被包含的路径
56    pub fn clear_include(mut self) -> Self {
57        self.include.clear();
58        self
59    }
60
61    /// 清空需要被忽略的路径
62    pub fn clear_exclude(mut self) -> Self {
63        self.exclude.clear();
64        self
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    #[test]
73    // 测试初始化,使用默认值创建成功
74    fn test_init_config() {
75        let config = ProjectConfig::new();
76        println!("{:?}", config);
77        assert_eq!(config.include.len(), 0);
78        assert_eq!(config.exclude.len(), 0);
79    }
80
81    #[test]
82    // &str 类型初始化
83    fn test_width_include() {
84        let config = ProjectConfig::new()
85            .add_includes(["./src"])
86            .add_excludes(["./node_modules", "./dist"]);
87        println!("{:?}", config);
88        assert_eq!(config.include.len(), 1);
89        assert_eq!(config.exclude.len(), 2);
90        let config = config.add_include("123").add_exclude("321");
91        println!("{:?}", config);
92        assert_eq!(config.include.len(), 2);
93        assert_eq!(config.exclude.len(), 3);
94    }
95
96    #[test]
97    // String 类型初始化
98    fn test_width_include_string() {
99        let config = ProjectConfig::new()
100            .add_includes(vec!["./src".to_string()])
101            .add_excludes(vec!["./node_modules".to_string(), "./dist".to_string()]);
102        println!("{:?}", config);
103        assert_eq!(config.include.len(), 1);
104        assert_eq!(config.exclude.len(), 2);
105        let config = config
106            .add_include("123".to_string())
107            .add_exclude("321".to_string());
108        println!("{:?}", config);
109        assert_eq!(config.include.len(), 2);
110        assert_eq!(config.exclude.len(), 3);
111    }
112
113    #[test]
114    // 测试清空 include 和 exclude
115    fn test_clear() {
116        let mut config = ProjectConfig::new()
117            .add_excludes(["123", "1231"])
118            .add_includes(["1111"]);
119        config = config.clear_include().clear_exclude();
120        println!("{:?}", config);
121        assert_eq!(config.include.len(), 0);
122        assert_eq!(config.exclude.len(), 0);
123    }
124}