Skip to main content

rustvello_proto/trigger/
context.rs

1//! Condition context types for trigger evaluation.
2
3use serde::{Deserialize, Serialize};
4use sha2::{Digest, Sha256};
5use std::collections::BTreeMap;
6
7use crate::identifiers::TaskId;
8use crate::status::InvocationStatus;
9
10/// Context data for a cron evaluation.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct CronContext {
13    pub timestamp: chrono::DateTime<chrono::Utc>,
14    pub last_execution: Option<chrono::DateTime<chrono::Utc>>,
15}
16
17/// Context for an invocation status change.
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct StatusContext {
20    pub invocation_id: crate::identifiers::InvocationId,
21    pub task_id: TaskId,
22    pub status: InvocationStatus,
23    /// Serialized invocation arguments (kwargs as key→JSON-string).
24    #[serde(default)]
25    pub arguments: BTreeMap<String, String>,
26}
27
28/// Context for a custom application event.
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct EventContext {
31    pub event_id: String,
32    pub event_code: String,
33    pub payload: serde_json::Value,
34}
35
36/// Context for a successful task result.
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct ResultContext {
39    pub invocation_id: crate::identifiers::InvocationId,
40    pub task_id: TaskId,
41    /// The task result as a JSON value.
42    pub result: serde_json::Value,
43    /// Serialized invocation arguments (kwargs as key→JSON-string).
44    #[serde(default)]
45    pub arguments: BTreeMap<String, String>,
46}
47
48/// Context for a task failure.
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct ExceptionContext {
51    pub invocation_id: crate::identifiers::InvocationId,
52    pub task_id: TaskId,
53    pub error_type: String,
54    pub error_message: String,
55    /// Serialized invocation arguments (kwargs as key→JSON-string).
56    #[serde(default)]
57    pub arguments: BTreeMap<String, String>,
58}
59
60/// Polymorphic condition context — matches condition variants 1:1.
61#[derive(Debug, Clone, Serialize, Deserialize)]
62#[non_exhaustive]
63pub enum ConditionContext {
64    Cron(CronContext),
65    Status(StatusContext),
66    Event(EventContext),
67    Result(ResultContext),
68    Exception(ExceptionContext),
69}
70
71impl std::fmt::Display for ConditionContext {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        match self {
74            Self::Cron(c) => write!(f, "CronCtx({})", c.timestamp),
75            Self::Status(c) => write!(f, "StatusCtx({}, {})", c.invocation_id, c.status),
76            Self::Event(c) => write!(f, "EventCtx({})", c.event_code),
77            Self::Result(c) => write!(f, "ResultCtx({})", c.invocation_id),
78            Self::Exception(c) => write!(f, "ExceptionCtx({})", c.invocation_id),
79        }
80    }
81}
82
83impl ConditionContext {
84    /// Compute a deterministic context ID for dedup.
85    pub fn context_id(&self) -> String {
86        let mut hasher = Sha256::new();
87        match self {
88            Self::Cron(c) => {
89                hasher.update(b"cron_ctx:");
90                hasher.update(c.timestamp.to_rfc3339().as_bytes());
91            }
92            Self::Status(c) => {
93                hasher.update(b"status_ctx:");
94                hasher.update(c.invocation_id.as_str().as_bytes());
95                hasher.update(b":");
96                hasher.update(c.status.to_string().as_bytes());
97            }
98            Self::Event(c) => {
99                hasher.update(b"event_ctx:");
100                hasher.update(c.event_id.as_bytes());
101            }
102            Self::Result(c) => {
103                hasher.update(b"result_ctx:");
104                hasher.update(c.invocation_id.as_str().as_bytes());
105            }
106            Self::Exception(c) => {
107                hasher.update(b"exception_ctx:");
108                hasher.update(c.invocation_id.as_str().as_bytes());
109            }
110        }
111        format!("{:x}", hasher.finalize())
112    }
113}