archetect_core/config/
rule.rs

1#[derive(Debug, Serialize, Deserialize, Clone)]
2pub struct RuleConfig {
3    #[serde(skip_serializing_if = "Option::is_none")]
4    description: Option<String>,
5    patterns: Vec<Pattern>,
6    #[serde(skip_serializing_if = "Option::is_none")]
7    filter: Option<bool>,
8    #[serde(skip_serializing_if = "Option::is_none")]
9    action: Option<RuleAction>,
10}
11
12impl RuleConfig {
13    pub fn new() -> RuleConfig {
14        RuleConfig {
15            description: None,
16            patterns: vec![],
17            filter: None,
18            action: None,
19        }
20    }
21
22    pub fn with_pattern(mut self, pattern: Pattern) -> RuleConfig {
23        self.add_pattern(pattern);
24        self
25    }
26
27    pub fn add_pattern(&mut self, pattern: Pattern) {
28        self.patterns.push(pattern);
29    }
30
31    pub fn with_action(mut self, action: RuleAction) -> RuleConfig {
32        self.set_action(Some(action));
33        self
34    }
35
36    pub fn set_action(&mut self, action: Option<RuleAction>) {
37        self.action = action;
38    }
39
40    pub fn action(&self) -> RuleAction {
41        self.action.as_ref().map(|a| a.clone()).unwrap_or_default()
42    }
43
44    pub fn patterns(&self) -> &[Pattern] {
45        self.patterns.as_slice()
46    }
47
48    pub fn add_description(&mut self, description: &str) {
49        self.description = Some(description.to_owned());
50    }
51
52    pub fn with_description(mut self, description: &str) -> RuleConfig {
53        self.add_description(description);
54        self
55    }
56
57    pub fn description(&self) -> Option<&str> {
58        self.description.as_ref().map(|d| d.as_str())
59    }
60
61    pub fn filter(&self) -> Option<bool> {
62        self.filter
63    }
64}
65
66#[derive(Debug, Serialize, Deserialize, PartialOrd, PartialEq, Clone)]
67pub enum Pattern {
68    #[serde(rename = "glob")]
69    GLOB(String),
70    #[serde(rename = "regex")]
71    REGEX(String),
72}
73
74#[derive(Debug, Serialize, Deserialize, Clone)]
75pub enum RuleAction {
76    COPY,
77    RENDER,
78    SKIP,
79}
80
81impl Default for RuleAction {
82    fn default() -> Self {
83        RuleAction::RENDER
84    }
85}
86
87#[cfg(test)]
88mod tests {
89    use crate::config::rule::{Pattern, RuleConfig};
90    use crate::config::RuleAction;
91
92    #[test]
93    fn test_serialize_rule_config() {
94        let result = serde_yaml::to_string(
95            &RuleConfig::new()
96                .with_pattern(Pattern::GLOB("*.jpg".to_owned()))
97                .with_pattern(Pattern::GLOB("*.gif".to_owned()))
98                .with_action(RuleAction::COPY),
99        )
100        .unwrap();
101        println!("{}", result);
102    }
103
104    #[test]
105    fn test_serialize_vec_rule_config() {
106        let rules = vec![
107            RuleConfig::new()
108                .with_pattern(Pattern::GLOB("*.jpg".to_owned()))
109                .with_pattern(Pattern::GLOB("*.gif".to_owned()))
110                .with_action(RuleAction::COPY),
111            RuleConfig::new()
112                .with_pattern(Pattern::REGEX("^(.*)*.java".to_owned()))
113                .with_action(RuleAction::RENDER),
114        ];
115
116        let result = serde_yaml::to_string(&rules).unwrap();
117        println!("{}", result);
118    }
119}