Skip to main content

gitfox_client/models/
rule.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use super::Principal;
5
6/// A protection rule on a repository.
7///
8/// `pattern` and `definition` stay JSON: their shape depends on the rule type
9/// and grows with every GitFox release, and gf only ever displays them.
10#[derive(Debug, Clone, Default, Serialize, Deserialize)]
11pub struct Rule {
12    #[serde(default)]
13    pub identifier: String,
14    #[serde(default)]
15    pub description: Option<String>,
16    /// `branch` today.
17    #[serde(default, rename = "type")]
18    pub kind: Option<String>,
19    /// `active`, `disabled` or `monitor`.
20    #[serde(default)]
21    pub state: Option<String>,
22    #[serde(default)]
23    pub pattern: Value,
24    #[serde(default)]
25    pub definition: Value,
26    #[serde(default)]
27    pub buildin: Option<bool>,
28    #[serde(default)]
29    pub created_by: Option<Principal>,
30    #[serde(default)]
31    pub created: Option<i64>,
32    #[serde(default)]
33    pub updated: Option<i64>,
34}
35
36impl Rule {
37    /// The branch patterns the rule includes, `default` meaning the default
38    /// branch.
39    pub fn targets(&self) -> Vec<String> {
40        let mut out = Vec::new();
41        if self.pattern.get("default").and_then(Value::as_bool) == Some(true) {
42            out.push("default branch".to_string());
43        }
44        if let Some(include) = self.pattern.get("include").and_then(Value::as_array) {
45            out.extend(include.iter().filter_map(Value::as_str).map(str::to_string));
46        }
47        out
48    }
49}