Skip to main content

wfe_core/models/
execution_pointer.rs

1use std::collections::HashMap;
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6use super::status::PointerStatus;
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
9/// Executionpointer.
10pub struct ExecutionPointer {
11    /// Id.
12    pub id: String,
13    /// Step id.
14    pub step_id: usize,
15    /// Active.
16    pub active: bool,
17    /// Status.
18    pub status: PointerStatus,
19    /// Sleep until.
20    pub sleep_until: Option<DateTime<Utc>>,
21    /// Persistence data.
22    pub persistence_data: Option<serde_json::Value>,
23    /// Start time.
24    pub start_time: Option<DateTime<Utc>>,
25    /// End time.
26    pub end_time: Option<DateTime<Utc>>,
27    /// Event name.
28    pub event_name: Option<String>,
29    /// Event key.
30    pub event_key: Option<String>,
31    /// Event published.
32    pub event_published: bool,
33    /// Event data.
34    pub event_data: Option<serde_json::Value>,
35    /// Step name.
36    pub step_name: Option<String>,
37    /// Retry count.
38    pub retry_count: u32,
39    /// Children.
40    pub children: Vec<String>,
41    /// Context item.
42    pub context_item: Option<serde_json::Value>,
43    /// Predecessor id.
44    pub predecessor_id: Option<String>,
45    /// Outcome.
46    pub outcome: Option<serde_json::Value>,
47    /// Scope.
48    pub scope: Vec<String>,
49    /// Extension attributes.
50    pub extension_attributes: HashMap<String, serde_json::Value>,
51}
52
53impl ExecutionPointer {
54    pub fn new(step_id: usize) -> Self {
55        Self {
56            id: uuid::Uuid::new_v4().to_string(),
57            step_id,
58            active: true,
59            status: PointerStatus::Pending,
60            sleep_until: None,
61            persistence_data: None,
62            start_time: None,
63            end_time: None,
64            event_name: None,
65            event_key: None,
66            event_published: false,
67            event_data: None,
68            step_name: None,
69            retry_count: 0,
70            children: Vec::new(),
71            context_item: None,
72            predecessor_id: None,
73            outcome: None,
74            scope: Vec::new(),
75            extension_attributes: HashMap::new(),
76        }
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83    use pretty_assertions::assert_eq;
84
85    #[test]
86    fn new_pointer_has_correct_defaults() {
87        let pointer = ExecutionPointer::new(0);
88        assert_eq!(pointer.step_id, 0);
89        assert!(pointer.active);
90        assert_eq!(pointer.status, PointerStatus::Pending);
91        assert_eq!(pointer.retry_count, 0);
92        assert!(pointer.children.is_empty());
93        assert!(pointer.scope.is_empty());
94        assert!(!pointer.event_published);
95    }
96
97    #[test]
98    fn new_pointer_generates_unique_ids() {
99        let p1 = ExecutionPointer::new(0);
100        let p2 = ExecutionPointer::new(0);
101        assert_ne!(p1.id, p2.id);
102    }
103
104    #[test]
105    fn serde_round_trip() {
106        let mut pointer = ExecutionPointer::new(3);
107        pointer.status = PointerStatus::Running;
108        pointer.retry_count = 2;
109        pointer.persistence_data = Some(serde_json::json!({"step_state": true}));
110        pointer.children = vec!["child-1".into(), "child-2".into()];
111
112        let json = serde_json::to_string(&pointer).unwrap();
113        let deserialized: ExecutionPointer = serde_json::from_str(&json).unwrap();
114
115        assert_eq!(pointer.id, deserialized.id);
116        assert_eq!(pointer.step_id, deserialized.step_id);
117        assert_eq!(pointer.status, deserialized.status);
118        assert_eq!(pointer.retry_count, deserialized.retry_count);
119        assert_eq!(pointer.persistence_data, deserialized.persistence_data);
120        assert_eq!(pointer.children, deserialized.children);
121    }
122}