1use astrs_wire::{
12 DataId, DurationMs, Metadata, NodeEvent, ParamKey, ParamScope, Parameter, PortRef,
13 RouteCloseReason, StopCause,
14};
15
16#[derive(Debug, Clone, PartialEq)]
21#[non_exhaustive]
22pub enum OpEvent {
23 Input {
29 id: DataId,
31 source: PortRef,
33 metadata: Metadata,
35 payload: Vec<u8>,
37 },
38 InputClosed {
40 id: DataId,
42 source: PortRef,
44 reason: RouteCloseReason,
46 },
47 Stop {
49 cause: StopCause,
51 grace: Option<DurationMs>,
53 },
54 Reload,
63 ParamUpdate {
66 scope: ParamScope,
68 key: ParamKey,
70 value: Parameter,
72 },
73}
74
75impl OpEvent {
76 #[must_use]
78 pub const fn input(&self) -> Option<&DataId> {
79 match self {
80 Self::Input { id, .. } | Self::InputClosed { id, .. } => Some(id),
81 _ => None,
82 }
83 }
84
85 #[must_use]
87 pub const fn is_input(&self) -> bool {
88 matches!(self, Self::Input { .. })
89 }
90
91 #[must_use]
93 pub const fn is_terminal(&self) -> bool {
94 matches!(self, Self::Stop { .. })
95 }
96
97 #[must_use]
99 pub fn payload_len(&self) -> usize {
100 match self {
101 Self::Input { payload, .. } => payload.len(),
102 _ => 0,
103 }
104 }
105
106 #[must_use]
133 pub fn from_node_event(event: &NodeEvent) -> Option<Self> {
134 match event {
135 NodeEvent::Input {
136 id,
137 source,
138 metadata,
139 payload,
140 } => Some(Self::Input {
141 id: id.clone(),
142 source: source.clone(),
143 metadata: metadata.clone(),
144 payload: payload.clone(),
145 }),
146 NodeEvent::InputClosed { id, source, reason } => Some(Self::InputClosed {
147 id: id.clone(),
148 source: source.clone(),
149 reason: reason.clone(),
150 }),
151 NodeEvent::Stop { cause, grace } => Some(Self::Stop {
152 cause: cause.clone(),
153 grace: *grace,
154 }),
155 NodeEvent::Reload { .. } => Some(Self::Reload),
156 NodeEvent::ParamUpdate { scope, key, value } => Some(Self::ParamUpdate {
157 scope: scope.clone(),
158 key: key.clone(),
159 value: value.clone(),
160 }),
161 _ => None,
162 }
163 }
164}
165
166#[cfg(test)]
167mod tests {
168 #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
169
170 use super::*;
171 use astrs_time::HlcTimestamp;
172
173 fn sample_input() -> OpEvent {
174 OpEvent::Input {
175 id: DataId::new("frames").unwrap(),
176 source: "camera/image".parse().unwrap(),
177 metadata: Metadata::new(HlcTimestamp::EPOCH),
178 payload: vec![1, 2, 3, 4],
179 }
180 }
181
182 #[test]
183 fn input_accessors() {
184 let event = sample_input();
185 assert!(event.is_input());
186 assert_eq!(event.input().map(DataId::as_str), Some("frames"));
187 assert_eq!(event.payload_len(), 4);
188 assert!(!event.is_terminal());
189 }
190
191 #[test]
192 fn stop_is_terminal_and_carries_no_input() {
193 let event = OpEvent::Stop {
194 cause: StopCause::Requested,
195 grace: None,
196 };
197 assert!(event.is_terminal());
198 assert!(event.input().is_none());
199 assert!(!event.is_input());
200 assert_eq!(event.payload_len(), 0);
201 }
202
203 #[test]
204 fn input_closed_names_its_input_but_is_not_data() {
205 let event = OpEvent::InputClosed {
206 id: DataId::new("frames").unwrap(),
207 source: "camera/image".parse().unwrap(),
208 reason: RouteCloseReason::ProducerFinished,
209 };
210 assert_eq!(event.input().map(DataId::as_str), Some("frames"));
211 assert!(!event.is_input());
212 }
213
214 #[test]
215 fn from_node_event_narrows_the_five_operator_relevant_variants() {
216 let input = NodeEvent::Input {
217 id: DataId::new("frames").unwrap(),
218 source: "camera/image".parse().unwrap(),
219 metadata: Metadata::new(HlcTimestamp::EPOCH),
220 payload: vec![9],
221 };
222 assert_eq!(
223 OpEvent::from_node_event(&input),
224 Some(OpEvent::Input {
225 id: DataId::new("frames").unwrap(),
226 source: "camera/image".parse().unwrap(),
227 metadata: Metadata::new(HlcTimestamp::EPOCH),
228 payload: vec![9],
229 })
230 );
231
232 let closed = NodeEvent::InputClosed {
233 id: DataId::new("frames").unwrap(),
234 source: "camera/image".parse().unwrap(),
235 reason: RouteCloseReason::ProducerFinished,
236 };
237 assert!(matches!(
238 OpEvent::from_node_event(&closed),
239 Some(OpEvent::InputClosed { .. })
240 ));
241
242 let stop = NodeEvent::Stop {
243 cause: StopCause::Requested,
244 grace: None,
245 };
246 assert_eq!(
247 OpEvent::from_node_event(&stop),
248 Some(OpEvent::Stop {
249 cause: StopCause::Requested,
250 grace: None
251 })
252 );
253
254 let reload = NodeEvent::Reload {
255 operator: None,
256 path: None,
257 };
258 assert_eq!(OpEvent::from_node_event(&reload), Some(OpEvent::Reload));
259
260 let param = NodeEvent::ParamUpdate {
261 scope: ParamScope::Global,
262 key: ParamKey::new("gain").unwrap(),
263 value: Parameter::Float(1.5),
264 };
265 assert_eq!(
266 OpEvent::from_node_event(¶m),
267 Some(OpEvent::ParamUpdate {
268 scope: ParamScope::Global,
269 key: ParamKey::new("gain").unwrap(),
270 value: Parameter::Float(1.5),
271 })
272 );
273 }
274
275 #[test]
276 fn from_node_event_drops_events_with_no_operator_level_meaning() {
277 for event in [
278 NodeEvent::AllInputsClosed,
279 NodeEvent::InputRecovered {
280 id: DataId::new("frames").unwrap(),
281 source: "camera/image".parse().unwrap(),
282 generation: 2,
283 },
284 NodeEvent::NodeFailed {
285 peer: astrs_wire::NodeId::new("camera").unwrap(),
286 cause: astrs_wire::NodeExitCause::Success,
287 },
288 NodeEvent::Restarted {
289 peer: astrs_wire::NodeId::new("camera").unwrap(),
290 generation: 2,
291 },
292 NodeEvent::ParamDeleted {
293 scope: ParamScope::Global,
294 key: ParamKey::new("gain").unwrap(),
295 },
296 ] {
297 assert!(
298 OpEvent::from_node_event(&event).is_none(),
299 "{event:?} should have no operator-level meaning"
300 );
301 }
302 }
303}