rustvello_proto/trigger/
context.rs1use serde::{Deserialize, Serialize};
4use sha2::{Digest, Sha256};
5use std::collections::BTreeMap;
6
7use crate::identifiers::TaskId;
8use crate::status::InvocationStatus;
9
10#[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#[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 #[serde(default)]
25 pub arguments: BTreeMap<String, String>,
26}
27
28#[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#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct ResultContext {
39 pub invocation_id: crate::identifiers::InvocationId,
40 pub task_id: TaskId,
41 pub result: serde_json::Value,
43 #[serde(default)]
45 pub arguments: BTreeMap<String, String>,
46}
47
48#[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 #[serde(default)]
57 pub arguments: BTreeMap<String, String>,
58}
59
60#[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 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}