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
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter, Result};

#[derive(Debug, Clone, Deserialize, Eq, PartialEq, Serialize)]
pub enum Status {
    Failed,
    InProgress,
    Stopped,
    Succeeded,
    TimedOut,
    Undefined,
}

impl Status {
    pub fn is_failed(&self) -> bool {
        match self {
            Status::Failed | Status::Stopped | Status::TimedOut => true,
            Status::InProgress | Status::Succeeded => false,
            _ => true,
        }
    }
}

impl From<String> for Status {
    fn from(var: String) -> Self {
        match var.as_ref() {
            "FAILED" => Status::Failed,
            "IN_PROGRESS" => Status::InProgress,
            "STOPPED" => Status::Stopped,
            "SUCCEEDED" => Status::Succeeded,
            "TIMED_OUT" => Status::TimedOut,
            _ => Status::Undefined,
        }
    }
}

impl Display for Status {
    fn fmt(&self, f: &mut Formatter) -> Result {
        let name = match self {
            Status::Failed => "FAILED".to_string(),
            Status::InProgress => "IN_PROGRESS".to_string(),
            Status::Stopped => "STOPPED".to_string(),
            Status::Succeeded => "SUCCEEDED".to_string(),
            Status::TimedOut => "TIMED_OUT".to_string(),
            _ => "UNDEFINED".to_string(),
        };

        write!(f, "{}", name)
    }
}