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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RuleConfig {
    #[serde(skip_serializing_if = "Option::is_none")]
    description: Option<String>,
    patterns: Vec<Pattern>,
    #[serde(skip_serializing_if = "Option::is_none")]
    filter: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    action: Option<RuleAction>,
}

impl RuleConfig {
    pub fn new() -> RuleConfig {
        RuleConfig {
            description: None,
            patterns: vec![],
            filter: None,
            action: None,
        }
    }

    pub fn with_pattern(mut self, pattern: Pattern) -> RuleConfig {
        self.add_pattern(pattern);
        self
    }

    pub fn add_pattern(&mut self, pattern: Pattern) {
        self.patterns.push(pattern);
    }

    pub fn with_action(mut self, action: RuleAction) -> RuleConfig {
        self.set_action(Some(action));
        self
    }

    pub fn set_action(&mut self, action: Option<RuleAction>) {
        self.action = action;
    }

    pub fn action(&self) -> RuleAction {
        self.action.as_ref().map(|a| a.clone()).unwrap_or_default()
    }

    pub fn patterns(&self) -> &[Pattern] {
        self.patterns.as_slice()
    }

    pub fn add_description(&mut self, description: &str) {
        self.description = Some(description.to_owned());
    }

    pub fn with_description(mut self, description: &str) -> RuleConfig {
        self.add_description(description);
        self
    }

    pub fn description(&self) -> Option<&str> {
        self.description.as_ref().map(|d| d.as_str())
    }

    pub fn filter(&self) -> Option<bool> {
        self.filter
    }
}

#[derive(Debug, Serialize, Deserialize, PartialOrd, PartialEq, Clone)]
pub enum Pattern {
    #[serde(rename = "glob")]
    GLOB(String),
    #[serde(rename = "regex")]
    REGEX(String),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum RuleAction {
    COPY,
    RENDER,
    SKIP,
}

impl Default for RuleAction {
    fn default() -> Self {
        RuleAction::RENDER
    }
}

#[cfg(test)]
mod tests {
    use crate::config::rule::{Pattern, RuleConfig};
    use crate::config::RuleAction;

    #[test]
    fn test_serialize_rule_config() {
        let result = serde_yaml::to_string(
            &RuleConfig::new()
                .with_pattern(Pattern::GLOB("*.jpg".to_owned()))
                .with_pattern(Pattern::GLOB("*.gif".to_owned()))
                .with_action(RuleAction::COPY),
        )
        .unwrap();
        println!("{}", result);
    }

    #[test]
    fn test_serialize_vec_rule_config() {
        let rules = vec![
            RuleConfig::new()
                .with_pattern(Pattern::GLOB("*.jpg".to_owned()))
                .with_pattern(Pattern::GLOB("*.gif".to_owned()))
                .with_action(RuleAction::COPY),
            RuleConfig::new()
                .with_pattern(Pattern::REGEX("^(.*)*.java".to_owned()))
                .with_action(RuleAction::RENDER),
        ];

        let result = serde_yaml::to_string(&rules).unwrap();
        println!("{}", result);
    }
}