use serde::{de::DeserializeOwned, Serialize};
use std::future::Future;
use std::pin::Pin;
use crate::error::CoreError;
use crate::ids::WorkflowId;
pub trait WorkflowDefinition: Send + Sync {
type Input: Serialize + DeserializeOwned + Send + Sync;
type Output: Serialize + DeserializeOwned + Send + Sync;
fn workflow_type() -> &'static str;
fn run(
&self,
ctx: WorkflowContext,
input: Self::Input,
) -> Pin<Box<dyn Future<Output = Result<Self::Output, CoreError>> + Send + '_>>;
}
#[derive(Debug, Clone)]
pub struct WorkflowContext {
pub workflow_id: WorkflowId,
pub is_replaying: bool,
pub attempt: u32,
}
impl WorkflowContext {
#[must_use]
pub fn new(workflow_id: WorkflowId) -> Self {
Self {
workflow_id,
is_replaying: false,
attempt: 1,
}
}
#[must_use]
pub fn is_replaying(&self) -> bool {
self.is_replaying
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_workflow_context_creation() {
let ctx = WorkflowContext::new(WorkflowId::generate());
assert!(!ctx.is_replaying());
assert_eq!(ctx.attempt, 1);
}
}