use astrs_wire::{
DataId, DurationMs, Metadata, NodeEvent, ParamKey, ParamScope, Parameter, PortRef,
RouteCloseReason, StopCause,
};
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum OpEvent {
Input {
id: DataId,
source: PortRef,
metadata: Metadata,
payload: Vec<u8>,
},
InputClosed {
id: DataId,
source: PortRef,
reason: RouteCloseReason,
},
Stop {
cause: StopCause,
grace: Option<DurationMs>,
},
Reload,
ParamUpdate {
scope: ParamScope,
key: ParamKey,
value: Parameter,
},
}
impl OpEvent {
#[must_use]
pub const fn input(&self) -> Option<&DataId> {
match self {
Self::Input { id, .. } | Self::InputClosed { id, .. } => Some(id),
_ => None,
}
}
#[must_use]
pub const fn is_input(&self) -> bool {
matches!(self, Self::Input { .. })
}
#[must_use]
pub const fn is_terminal(&self) -> bool {
matches!(self, Self::Stop { .. })
}
#[must_use]
pub fn payload_len(&self) -> usize {
match self {
Self::Input { payload, .. } => payload.len(),
_ => 0,
}
}
#[must_use]
pub fn from_node_event(event: &NodeEvent) -> Option<Self> {
match event {
NodeEvent::Input {
id,
source,
metadata,
payload,
} => Some(Self::Input {
id: id.clone(),
source: source.clone(),
metadata: metadata.clone(),
payload: payload.clone(),
}),
NodeEvent::InputClosed { id, source, reason } => Some(Self::InputClosed {
id: id.clone(),
source: source.clone(),
reason: reason.clone(),
}),
NodeEvent::Stop { cause, grace } => Some(Self::Stop {
cause: cause.clone(),
grace: *grace,
}),
NodeEvent::Reload { .. } => Some(Self::Reload),
NodeEvent::ParamUpdate { scope, key, value } => Some(Self::ParamUpdate {
scope: scope.clone(),
key: key.clone(),
value: value.clone(),
}),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
use astrs_time::HlcTimestamp;
fn sample_input() -> OpEvent {
OpEvent::Input {
id: DataId::new("frames").unwrap(),
source: "camera/image".parse().unwrap(),
metadata: Metadata::new(HlcTimestamp::EPOCH),
payload: vec![1, 2, 3, 4],
}
}
#[test]
fn input_accessors() {
let event = sample_input();
assert!(event.is_input());
assert_eq!(event.input().map(DataId::as_str), Some("frames"));
assert_eq!(event.payload_len(), 4);
assert!(!event.is_terminal());
}
#[test]
fn stop_is_terminal_and_carries_no_input() {
let event = OpEvent::Stop {
cause: StopCause::Requested,
grace: None,
};
assert!(event.is_terminal());
assert!(event.input().is_none());
assert!(!event.is_input());
assert_eq!(event.payload_len(), 0);
}
#[test]
fn input_closed_names_its_input_but_is_not_data() {
let event = OpEvent::InputClosed {
id: DataId::new("frames").unwrap(),
source: "camera/image".parse().unwrap(),
reason: RouteCloseReason::ProducerFinished,
};
assert_eq!(event.input().map(DataId::as_str), Some("frames"));
assert!(!event.is_input());
}
#[test]
fn from_node_event_narrows_the_five_operator_relevant_variants() {
let input = NodeEvent::Input {
id: DataId::new("frames").unwrap(),
source: "camera/image".parse().unwrap(),
metadata: Metadata::new(HlcTimestamp::EPOCH),
payload: vec![9],
};
assert_eq!(
OpEvent::from_node_event(&input),
Some(OpEvent::Input {
id: DataId::new("frames").unwrap(),
source: "camera/image".parse().unwrap(),
metadata: Metadata::new(HlcTimestamp::EPOCH),
payload: vec![9],
})
);
let closed = NodeEvent::InputClosed {
id: DataId::new("frames").unwrap(),
source: "camera/image".parse().unwrap(),
reason: RouteCloseReason::ProducerFinished,
};
assert!(matches!(
OpEvent::from_node_event(&closed),
Some(OpEvent::InputClosed { .. })
));
let stop = NodeEvent::Stop {
cause: StopCause::Requested,
grace: None,
};
assert_eq!(
OpEvent::from_node_event(&stop),
Some(OpEvent::Stop {
cause: StopCause::Requested,
grace: None
})
);
let reload = NodeEvent::Reload {
operator: None,
path: None,
};
assert_eq!(OpEvent::from_node_event(&reload), Some(OpEvent::Reload));
let param = NodeEvent::ParamUpdate {
scope: ParamScope::Global,
key: ParamKey::new("gain").unwrap(),
value: Parameter::Float(1.5),
};
assert_eq!(
OpEvent::from_node_event(¶m),
Some(OpEvent::ParamUpdate {
scope: ParamScope::Global,
key: ParamKey::new("gain").unwrap(),
value: Parameter::Float(1.5),
})
);
}
#[test]
fn from_node_event_drops_events_with_no_operator_level_meaning() {
for event in [
NodeEvent::AllInputsClosed,
NodeEvent::InputRecovered {
id: DataId::new("frames").unwrap(),
source: "camera/image".parse().unwrap(),
generation: 2,
},
NodeEvent::NodeFailed {
peer: astrs_wire::NodeId::new("camera").unwrap(),
cause: astrs_wire::NodeExitCause::Success,
},
NodeEvent::Restarted {
peer: astrs_wire::NodeId::new("camera").unwrap(),
generation: 2,
},
NodeEvent::ParamDeleted {
scope: ParamScope::Global,
key: ParamKey::new("gain").unwrap(),
},
] {
assert!(
OpEvent::from_node_event(&event).is_none(),
"{event:?} should have no operator-level meaning"
);
}
}
}