use time::OffsetDateTime;
use uuid::Uuid;
use crate::WorkflowRef;
#[derive(Debug, Clone)]
pub struct EffectContext {
pub effect_id: Uuid,
pub workflow: WorkflowRef,
pub attempt: u32,
pub created_at: OffsetDateTime,
}
impl EffectContext {
pub fn new(
effect_id: Uuid,
workflow: WorkflowRef,
attempt: u32,
created_at: OffsetDateTime,
) -> Self {
Self {
effect_id,
workflow,
attempt,
created_at,
}
}
pub fn idempotency_key(&self) -> String {
format!("{}:{}", self.workflow, self.effect_id)
}
pub fn is_retry(&self) -> bool {
self.attempt > 1
}
}
#[cfg(test)]
mod tests {
use super::*;
fn test_context() -> EffectContext {
EffectContext::new(
Uuid::nil(),
WorkflowRef::new("order", "ord-123"),
1,
OffsetDateTime::UNIX_EPOCH,
)
}
#[test]
fn idempotency_key_format() {
let ctx = test_context();
let key = ctx.idempotency_key();
assert!(key.starts_with("order:ord-123:"));
assert!(key.contains(&Uuid::nil().to_string()));
}
#[test]
fn is_retry() {
let mut ctx = test_context();
assert!(!ctx.is_retry());
ctx.attempt = 2;
assert!(ctx.is_retry());
}
}