mod types;
pub use types::{Command, Interrupt, NodeResult};
use std::sync::atomic::{AtomicU64, Ordering};
use crate::harness::ids::NodeId;
static INTERRUPT_SEQ: AtomicU64 = AtomicU64::new(0);
impl<Update> Command<Update> {
pub fn new() -> Self {
Self {
update: None,
goto: Vec::new(),
resume: None,
}
}
pub fn goto(targets: impl IntoIterator<Item = impl Into<NodeId>>) -> Self {
Self {
update: None,
goto: targets.into_iter().map(Into::into).collect(),
resume: None,
}
}
pub fn update(update: Update) -> Self {
Self {
update: Some(update),
goto: Vec::new(),
resume: None,
}
}
pub fn resume(value: serde_json::Value) -> Self {
Self {
update: None,
goto: Vec::new(),
resume: Some(value),
}
}
pub fn with_update(mut self, update: Update) -> Self {
self.update = Some(update);
self
}
pub fn with_goto(mut self, targets: impl IntoIterator<Item = impl Into<NodeId>>) -> Self {
self.goto.extend(targets.into_iter().map(Into::into));
self
}
pub fn with_resume(mut self, value: serde_json::Value) -> Self {
self.resume = Some(value);
self
}
}
impl<Update> Default for Command<Update> {
fn default() -> Self {
Self::new()
}
}
impl Interrupt {
pub fn new(node: impl Into<NodeId>, payload: serde_json::Value) -> Self {
let node = node.into();
let seq = INTERRUPT_SEQ.fetch_add(1, Ordering::Relaxed);
Self {
id: format!("interrupt-{node}-{seq}"),
node,
payload,
}
}
pub fn with_id(
id: impl Into<String>,
node: impl Into<NodeId>,
payload: serde_json::Value,
) -> Self {
Self {
id: id.into(),
node: node.into(),
payload,
}
}
}
#[cfg(test)]
mod test;