Skip to main content

opal/gitlab/
rules.rs

1use serde::Deserialize;
2use std::collections::HashMap;
3
4#[derive(Debug, Clone, Default, Deserialize)]
5#[serde(default)]
6pub struct JobRule {
7    #[serde(rename = "if")]
8    pub if_expr: Option<String>,
9    pub changes: Option<RuleChangesRaw>,
10    pub exists: Option<RuleExistsRaw>,
11    pub when: Option<String>,
12    pub allow_failure: Option<bool>,
13    pub start_in: Option<String>,
14    #[serde(default)]
15    pub variables: HashMap<String, String>,
16}
17
18#[derive(Debug, Clone, Deserialize)]
19#[serde(untagged)]
20pub enum RuleChangesRaw {
21    Simple(Vec<String>),
22    Nested {
23        paths: Vec<String>,
24        compare_to: Option<String>,
25    },
26}
27
28impl Default for RuleChangesRaw {
29    fn default() -> Self {
30        RuleChangesRaw::Simple(Vec::new())
31    }
32}
33
34impl RuleChangesRaw {
35    pub fn paths(&self) -> &[String] {
36        match self {
37            RuleChangesRaw::Simple(paths) => paths,
38            RuleChangesRaw::Nested { paths, .. } => paths,
39        }
40    }
41
42    pub fn compare_to(&self) -> Option<&str> {
43        match self {
44            RuleChangesRaw::Simple(_) => None,
45            RuleChangesRaw::Nested { compare_to, .. } => compare_to.as_deref(),
46        }
47    }
48}
49
50#[derive(Debug, Clone, Deserialize)]
51#[serde(untagged)]
52pub enum RuleExistsRaw {
53    Simple(Vec<String>),
54    Nested { paths: Vec<String> },
55}
56
57impl Default for RuleExistsRaw {
58    fn default() -> Self {
59        RuleExistsRaw::Simple(Vec::new())
60    }
61}
62
63impl RuleExistsRaw {
64    pub fn paths(&self) -> &[String] {
65        match self {
66            RuleExistsRaw::Simple(paths) => paths,
67            RuleExistsRaw::Nested { paths } => paths,
68        }
69    }
70}