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
/// A single job and relevant information
#[derive(Debug, Serialize, Deserialize)]
pub struct MatrixElement {
    finished_at: Option<String>,
    result: Option<u32>,
    number: String,
    id: Option<u32>,
}

/// A list of jobs
#[derive(Debug, Serialize, Deserialize)]
pub struct Matrix {
    id: u32,
    matrix: Vec<MatrixElement>,
}

impl MatrixElement {
    /// Get the id of the job
    pub fn id(&self) -> u32 {
        self.id.unwrap()
    }

    /// Check if the job was run on the build leader
    pub fn is_leader(&self) -> bool {
        super::is_leader(&self.number)
    }

    /// Check if the job finished and succeeded
    pub fn is_succeeded(&self) -> bool {
        if !self.is_finished() {
           return false;
        }

        match self.result {
            None => false,
            Some(0) => true,
            Some(_) => false,
        }
    }

    /// Check if the job finished
    pub fn is_finished(&self) -> bool {
        self.finished_at.is_some()
    }
}

impl Matrix {
    /// Get the build matrix
    pub fn builds(&self) -> &[MatrixElement] {
        &self.matrix
    }

    /// Check that all non-leader jobs finished
    pub fn others_finished(&self) -> bool {
        self.matrix.iter()
            .filter(|build| !build.is_leader())
            .all(|build| build.is_finished())
    }

    /// Check that all non-leader jobs succeeded
    pub fn others_succeeded(&self) -> bool {
        self.matrix.iter()
            .filter(|build| !build.is_leader())
            .all(|build| build.is_succeeded())
    }
}