Skip to main content

sidebyside_core/
workflow.rs

1//! Workflow definition traits
2//!
3//! This module defines the traits for workflow definitions.
4
5use serde::{de::DeserializeOwned, Serialize};
6use std::future::Future;
7use std::pin::Pin;
8
9use crate::error::CoreError;
10use crate::ids::WorkflowId;
11
12/// A trait for defining workflow types
13///
14/// Workflows are the main unit of execution in Sidebyside. They define
15/// the orchestration logic that coordinates activities and decision points.
16pub trait WorkflowDefinition: Send + Sync {
17    /// The input type for this workflow
18    type Input: Serialize + DeserializeOwned + Send + Sync;
19    /// The output type for this workflow
20    type Output: Serialize + DeserializeOwned + Send + Sync;
21
22    /// Get the workflow type name
23    fn workflow_type() -> &'static str;
24
25    /// Run the workflow with the given context and input
26    fn run(
27        &self,
28        ctx: WorkflowContext,
29        input: Self::Input,
30    ) -> Pin<Box<dyn Future<Output = Result<Self::Output, CoreError>> + Send + '_>>;
31}
32
33/// Context provided to workflows during execution
34#[derive(Debug, Clone)]
35pub struct WorkflowContext {
36    /// The workflow ID
37    pub workflow_id: WorkflowId,
38    /// Whether this is a replay of a previous execution
39    pub is_replaying: bool,
40    /// Current attempt number
41    pub attempt: u32,
42}
43
44impl WorkflowContext {
45    /// Create a new workflow context
46    #[must_use]
47    pub fn new(workflow_id: WorkflowId) -> Self {
48        Self {
49            workflow_id,
50            is_replaying: false,
51            attempt: 1,
52        }
53    }
54
55    /// Check if the workflow is replaying
56    #[must_use]
57    pub fn is_replaying(&self) -> bool {
58        self.is_replaying
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_workflow_context_creation() {
68        let ctx = WorkflowContext::new(WorkflowId::generate());
69        assert!(!ctx.is_replaying());
70        assert_eq!(ctx.attempt, 1);
71    }
72}