forge_core/workflow/
traits.rs

1use 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
11/// Trait for workflow handlers.
12pub trait ForgeWorkflow: Send + Sync + 'static {
13    /// Input type for the workflow.
14    type Input: DeserializeOwned + Serialize + Send + Sync;
15    /// Output type for the workflow.
16    type Output: Serialize + Send;
17
18    /// Get workflow metadata.
19    fn info() -> WorkflowInfo;
20
21    /// Execute the workflow.
22    fn execute(
23        ctx: &WorkflowContext,
24        input: Self::Input,
25    ) -> Pin<Box<dyn Future<Output = Result<Self::Output>> + Send + '_>>;
26}
27
28/// Workflow metadata.
29#[derive(Debug, Clone)]
30pub struct WorkflowInfo {
31    /// Workflow name.
32    pub name: &'static str,
33    /// Workflow version.
34    pub version: u32,
35    /// Default timeout for the entire workflow.
36    pub timeout: Duration,
37    /// Whether the workflow is deprecated.
38    pub deprecated: bool,
39    /// Whether the workflow is public (no auth required).
40    pub is_public: bool,
41    /// Required role for authorization (implies auth required).
42    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), // 24 hours
51            deprecated: false,
52            is_public: false,
53            required_role: None,
54        }
55    }
56}
57
58/// Workflow execution status.
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub enum WorkflowStatus {
61    /// Workflow is created but not started.
62    Created,
63    /// Workflow is actively running.
64    Running,
65    /// Workflow is waiting for an external event.
66    Waiting,
67    /// Workflow completed successfully.
68    Completed,
69    /// Workflow failed and is running compensation.
70    Compensating,
71    /// Workflow compensation completed.
72    Compensated,
73    /// Workflow failed (compensation also failed or not available).
74    Failed,
75}
76
77impl WorkflowStatus {
78    /// Convert to string for database storage.
79    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    /// Check if the workflow is terminal (no longer running).
92    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}