forge_core/workflow/
traits.rs1use std::future::Future;
2use std::pin::Pin;
3use std::str::FromStr;
4use std::time::Duration;
5
6use serde::{Serialize, de::DeserializeOwned};
7
8use super::context::WorkflowContext;
9use crate::Result;
10
11pub trait ForgeWorkflow: Send + Sync + 'static {
13 type Input: DeserializeOwned + Serialize + Send + Sync;
15 type Output: Serialize + Send;
17
18 fn info() -> WorkflowInfo;
20
21 fn execute(
23 ctx: &WorkflowContext,
24 input: Self::Input,
25 ) -> Pin<Box<dyn Future<Output = Result<Self::Output>> + Send + '_>>;
26}
27
28#[derive(Debug, Clone)]
30pub struct WorkflowInfo {
31 pub name: &'static str,
33 pub version: u32,
35 pub timeout: Duration,
37 pub deprecated: bool,
39 pub is_public: bool,
41 pub required_role: Option<&'static str>,
43}
44
45impl Default for WorkflowInfo {
46 fn default() -> Self {
47 Self {
48 name: "",
49 version: 1,
50 timeout: Duration::from_secs(86400), deprecated: false,
52 is_public: false,
53 required_role: None,
54 }
55 }
56}
57
58#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub enum WorkflowStatus {
61 Created,
63 Running,
65 Waiting,
67 Completed,
69 Compensating,
71 Compensated,
73 Failed,
75}
76
77impl WorkflowStatus {
78 pub fn as_str(&self) -> &'static str {
80 match self {
81 Self::Created => "created",
82 Self::Running => "running",
83 Self::Waiting => "waiting",
84 Self::Completed => "completed",
85 Self::Compensating => "compensating",
86 Self::Compensated => "compensated",
87 Self::Failed => "failed",
88 }
89 }
90
91 pub fn is_terminal(&self) -> bool {
93 matches!(self, Self::Completed | Self::Compensated | Self::Failed)
94 }
95}
96
97impl FromStr for WorkflowStatus {
98 type Err = std::convert::Infallible;
99
100 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
101 Ok(match s {
102 "created" => Self::Created,
103 "running" => Self::Running,
104 "waiting" => Self::Waiting,
105 "completed" => Self::Completed,
106 "compensating" => Self::Compensating,
107 "compensated" => Self::Compensated,
108 "failed" => Self::Failed,
109 _ => Self::Created,
110 })
111 }
112}
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 #[test]
119 fn test_workflow_info_default() {
120 let info = WorkflowInfo::default();
121 assert_eq!(info.name, "");
122 assert_eq!(info.version, 1);
123 assert!(!info.deprecated);
124 }
125
126 #[test]
127 fn test_workflow_status_conversion() {
128 assert_eq!(WorkflowStatus::Running.as_str(), "running");
129 assert_eq!(WorkflowStatus::Completed.as_str(), "completed");
130 assert_eq!(WorkflowStatus::Compensating.as_str(), "compensating");
131
132 assert_eq!(
133 "running".parse::<WorkflowStatus>(),
134 Ok(WorkflowStatus::Running)
135 );
136 assert_eq!(
137 "completed".parse::<WorkflowStatus>(),
138 Ok(WorkflowStatus::Completed)
139 );
140 }
141
142 #[test]
143 fn test_workflow_status_is_terminal() {
144 assert!(!WorkflowStatus::Running.is_terminal());
145 assert!(!WorkflowStatus::Waiting.is_terminal());
146 assert!(WorkflowStatus::Completed.is_terminal());
147 assert!(WorkflowStatus::Failed.is_terminal());
148 assert!(WorkflowStatus::Compensated.is_terminal());
149 }
150}