wfe_core/models/
lifecycle.rs1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct LifecycleEvent {
7 pub event_time_utc: DateTime<Utc>,
9 pub workflow_instance_id: String,
11 pub workflow_definition_id: String,
13 pub version: u32,
15 pub reference: Option<String>,
17 pub event_type: LifecycleEventType,
19}
20
21#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
22pub enum LifecycleEventType {
24 Started,
26 Resumed,
28 Suspended,
30 Completed,
32 Terminated,
34 Error {
36 message: String,
37 },
38 StepStarted {
40 step_id: usize,
41 step_name: Option<String>,
42 },
43 StepCompleted {
45 step_id: usize,
46 step_name: Option<String>,
47 },
48}
49
50impl LifecycleEvent {
51 pub fn new(
52 workflow_instance_id: impl Into<String>,
53 workflow_definition_id: impl Into<String>,
54 version: u32,
55 event_type: LifecycleEventType,
56 ) -> Self {
57 Self {
58 event_time_utc: Utc::now(),
59 workflow_instance_id: workflow_instance_id.into(),
60 workflow_definition_id: workflow_definition_id.into(),
61 version,
62 reference: None,
63 event_type,
64 }
65 }
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71 use pretty_assertions::assert_eq;
72
73 #[test]
74 fn lifecycle_event_types_are_distinct() {
75 assert_ne!(LifecycleEventType::Started, LifecycleEventType::Completed);
76 }
77
78 #[test]
79 fn serde_round_trip_simple_variant() {
80 let event = LifecycleEvent::new("wf-1", "def-1", 1, LifecycleEventType::Started);
81 let json = serde_json::to_string(&event).unwrap();
82 let deserialized: LifecycleEvent = serde_json::from_str(&json).unwrap();
83 assert_eq!(
84 event.workflow_instance_id,
85 deserialized.workflow_instance_id
86 );
87 assert_eq!(event.event_type, deserialized.event_type);
88 }
89
90 #[test]
91 fn serde_round_trip_error_variant() {
92 let event = LifecycleEvent::new(
93 "wf-1",
94 "def-1",
95 1,
96 LifecycleEventType::Error {
97 message: "boom".into(),
98 },
99 );
100 let json = serde_json::to_string(&event).unwrap();
101 let deserialized: LifecycleEvent = serde_json::from_str(&json).unwrap();
102 assert_eq!(
103 deserialized.event_type,
104 LifecycleEventType::Error {
105 message: "boom".into()
106 }
107 );
108 }
109
110 #[test]
111 fn serde_round_trip_step_variant() {
112 let event = LifecycleEvent::new(
113 "wf-1",
114 "def-1",
115 1,
116 LifecycleEventType::StepStarted {
117 step_id: 3,
118 step_name: Some("ProcessOrder".into()),
119 },
120 );
121 let json = serde_json::to_string(&event).unwrap();
122 let deserialized: LifecycleEvent = serde_json::from_str(&json).unwrap();
123 assert_eq!(
124 deserialized.event_type,
125 LifecycleEventType::StepStarted {
126 step_id: 3,
127 step_name: Some("ProcessOrder".into()),
128 }
129 );
130 }
131}