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
mod envs;
mod error;
mod id;
mod results;
mod workflow_event;
mod workflow_state;
mod workflow_state_event;

pub use envs::*;
pub use error::*;
pub use id::*;
pub use results::*;
pub use workflow_event::*;
pub use workflow_state::*;
pub use workflow_state_event::*;

use serde::{Deserialize, Serialize};

// #[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
// pub struct Secret {
//   pub key: String,
//   pub value: String,
// }

// #[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
// pub struct Volume {
//   pub from: String,
//   pub to: String,
// }

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum WorkflowLogType {
  Error,
  Log,
}

impl ToString for WorkflowLogType {
  fn to_string(&self) -> String {
    match self {
      WorkflowLogType::Error => "error".to_string(),
      WorkflowLogType::Log => "log".to_string(),
    }
  }
}

impl From<String> for WorkflowLogType {
  fn from(s: String) -> Self {
    match s.as_str() {
      "error" => WorkflowLogType::Error,
      "log" => WorkflowLogType::Log,
      _ => WorkflowLogType::Log,
    }
  }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct WorkflowLog {
  pub step_id: StepId,
  pub log_type: WorkflowLogType,
  pub message: String,
  pub time: chrono::DateTime<chrono::Utc>,
}

impl Default for WorkflowLog {
  fn default() -> Self {
    WorkflowLog {
      step_id: StepId::default(),
      log_type: WorkflowLogType::Log,
      message: "".to_string(),
      time: chrono::Utc::now(),
    }
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_workflow_log_type() {
    assert_eq!(
      WorkflowLogType::Error,
      WorkflowLogType::from("error".to_string())
    );
    assert_eq!(
      WorkflowLogType::Log,
      WorkflowLogType::from("log".to_string())
    );
    assert_eq!(
      WorkflowLogType::Log,
      WorkflowLogType::from("unknown".to_string())
    );
  }

  #[test]
  fn test_workflow_log_type_to_string() {
    assert_eq!("error", WorkflowLogType::Error.to_string());
    assert_eq!("log", WorkflowLogType::Log.to_string());
  }
}