use compact_str::CompactString;
use serde::{Deserialize, Serialize};
use super::action::Action;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
pub struct Transition {
pub event: Option<CompactString>,
#[serde(default)]
pub guard: Option<CompactString>,
#[serde(default)]
pub targets: Vec<CompactString>,
#[serde(default)]
pub transition_type: TransitionType,
#[serde(default)]
pub actions: Vec<Action>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub delay: Option<CompactString>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub quorum: Option<u32>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[serde(rename_all = "snake_case")]
pub enum TransitionType {
#[default]
External,
Internal,
}
impl Transition {
pub fn new(event: impl Into<CompactString>, target: impl Into<CompactString>) -> Self {
Self {
event: Some(event.into()),
guard: None,
targets: vec![target.into()],
transition_type: TransitionType::External,
actions: Vec::new(),
delay: None,
quorum: None,
}
}
pub fn eventless(target: impl Into<CompactString>) -> Self {
Self {
event: None,
guard: None,
targets: vec![target.into()],
transition_type: TransitionType::External,
actions: Vec::new(),
delay: None,
quorum: None,
}
}
pub fn with_guard(mut self, guard: impl Into<CompactString>) -> Self {
self.guard = Some(guard.into());
self
}
pub fn internal(mut self) -> Self {
self.transition_type = TransitionType::Internal;
self
}
pub fn with_action(mut self, action: Action) -> Self {
self.actions.push(action);
self
}
pub fn with_delay(mut self, delay: impl Into<CompactString>) -> Self {
self.delay = Some(delay.into());
self
}
pub fn with_quorum(mut self, quorum: u32) -> Self {
self.quorum = Some(quorum);
self
}
pub fn set_guard(&mut self, guard: impl Into<CompactString>) -> &mut Self {
self.guard = Some(guard.into());
self
}
pub fn set_delay(&mut self, delay: impl Into<CompactString>) -> &mut Self {
self.delay = Some(delay.into());
self
}
pub fn set_quorum(&mut self, quorum: u32) -> &mut Self {
self.quorum = Some(quorum);
self
}
}