Skip to main content

rustvello_proto/trigger/
mod.rs

1//! Trigger system data types — conditions, contexts, and definitions.
2//!
3//! Mirrors pynenc's `trigger/` subsystem with Rust-idiomatic enums
4//! for polymorphic dispatch instead of class hierarchies.
5
6use serde::{Deserialize, Serialize};
7use std::fmt;
8
9mod condition;
10mod context;
11mod definition;
12pub(crate) mod filter;
13#[cfg(test)]
14mod tests;
15
16pub use condition::*;
17pub use context::*;
18pub use definition::*;
19
20// ---------------------------------------------------------------------------
21// Identifiers
22// ---------------------------------------------------------------------------
23
24/// SHA-256 hash-based identifier for a trigger condition.
25///
26/// Format matches pynenc's human-readable condition IDs exactly:
27/// - Status: `condition#{task_id}#{sorted_statuses}#{filter_id}`
28/// - Cron: `cron_{expression}`
29/// - Event: `event#{event_code}#{filter_id}`
30/// - Result: `{status_cid}_result_{result_filter_id}`
31/// - Exception: `{status_cid}_exception_{types|any}`
32#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
33pub struct ConditionId(pub(crate) String);
34
35impl ConditionId {
36    /// Get the inner string value.
37    pub fn as_str(&self) -> &str {
38        &self.0
39    }
40}
41
42impl fmt::Display for ConditionId {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        write!(f, "{}", self.0)
45    }
46}
47
48impl AsRef<str> for ConditionId {
49    fn as_ref(&self) -> &str {
50        &self.0
51    }
52}
53
54impl From<String> for ConditionId {
55    fn from(s: String) -> Self {
56        Self(s)
57    }
58}
59
60impl From<&str> for ConditionId {
61    fn from(s: &str) -> Self {
62        Self(s.to_owned())
63    }
64}
65
66/// SHA-256 hash-based identifier for a trigger definition.
67#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
68pub struct TriggerDefinitionId(String);
69
70impl TriggerDefinitionId {
71    /// Get the inner string value.
72    pub fn as_str(&self) -> &str {
73        &self.0
74    }
75}
76
77impl fmt::Display for TriggerDefinitionId {
78    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79        write!(f, "{}", self.0)
80    }
81}
82
83impl AsRef<str> for TriggerDefinitionId {
84    fn as_ref(&self) -> &str {
85        &self.0
86    }
87}
88
89impl From<String> for TriggerDefinitionId {
90    fn from(s: String) -> Self {
91        Self(s)
92    }
93}
94
95impl From<&str> for TriggerDefinitionId {
96    fn from(s: &str) -> Self {
97        Self(s.to_owned())
98    }
99}
100
101/// Unique identifier for a trigger execution run (dedup key).
102#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
103pub struct TriggerRunId(String);
104
105impl TriggerRunId {
106    /// Get the inner string value.
107    pub fn as_str(&self) -> &str {
108        &self.0
109    }
110}
111
112impl fmt::Display for TriggerRunId {
113    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114        write!(f, "{}", self.0)
115    }
116}
117
118impl AsRef<str> for TriggerRunId {
119    fn as_ref(&self) -> &str {
120        &self.0
121    }
122}
123
124impl From<String> for TriggerRunId {
125    fn from(s: String) -> Self {
126        Self(s)
127    }
128}
129
130impl From<&str> for TriggerRunId {
131    fn from(s: &str) -> Self {
132        Self(s.to_owned())
133    }
134}
135
136// ---------------------------------------------------------------------------
137// Trigger logic
138// ---------------------------------------------------------------------------
139
140/// Logic for combining multiple conditions in a trigger definition.
141#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
142#[non_exhaustive]
143pub enum TriggerLogic {
144    /// All conditions must be satisfied.
145    And,
146    /// Any condition is sufficient.
147    Or,
148}
149
150impl fmt::Display for TriggerLogic {
151    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152        match self {
153            Self::And => write!(f, "AND"),
154            Self::Or => write!(f, "OR"),
155        }
156    }
157}