#![allow(missing_docs)]
use compact_str::CompactString;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "rkyv",
rkyv(
serialize_bounds(__S: rkyv::ser::Writer + rkyv::ser::Allocator, <__S as rkyv::rancor::Fallible>::Error: rkyv::rancor::Source),
deserialize_bounds(__D::Error: rkyv::rancor::Source),
bytecheck(bounds(
__C: rkyv::validation::ArchiveContext,
<__C as rkyv::rancor::Fallible>::Error: rkyv::rancor::Source,
)),
)
)]
#[allow(missing_docs)] pub struct Action {
#[cfg_attr(feature = "rkyv", rkyv(omit_bounds))]
pub kind: ActionKind,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "rkyv",
rkyv(
serialize_bounds(__S: rkyv::ser::Writer + rkyv::ser::Allocator, <__S as rkyv::rancor::Fallible>::Error: rkyv::rancor::Source),
deserialize_bounds(__D::Error: rkyv::rancor::Source),
bytecheck(bounds(
__C: rkyv::validation::ArchiveContext,
<__C as rkyv::rancor::Fallible>::Error: rkyv::rancor::Source,
)),
)
)]
#[serde(tag = "type", rename_all = "snake_case")]
#[allow(missing_docs)] pub enum ActionKind {
Raise {
event: CompactString,
},
Send {
event: CompactString,
target: Option<CompactString>,
delay: Option<CompactString>,
},
Assign {
location: CompactString,
expr: CompactString,
},
Log {
label: Option<CompactString>,
expr: Option<CompactString>,
},
Cancel {
sendid: CompactString,
},
If {
branches: Vec<IfBranch>,
#[cfg_attr(feature = "rkyv", rkyv(omit_bounds))]
actions: Vec<Action>,
},
Foreach {
array: CompactString,
item: CompactString,
index: Option<CompactString>,
#[cfg_attr(feature = "rkyv", rkyv(omit_bounds))]
actions: Vec<Action>,
},
Script {
content: CompactString,
},
Invoke {
invoke_type: Option<CompactString>,
src: Option<CompactString>,
id: Option<CompactString>,
},
Custom {
name: CompactString,
#[serde(default)]
params: Vec<(CompactString, CompactString)>,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
pub struct IfBranch {
pub guard: Option<CompactString>,
pub action_count: usize,
}
impl Action {
pub fn raise(event: impl Into<CompactString>) -> Self {
Self {
kind: ActionKind::Raise {
event: event.into(),
},
}
}
pub fn send(event: impl Into<CompactString>) -> Self {
Self {
kind: ActionKind::Send {
event: event.into(),
target: None,
delay: None,
},
}
}
pub fn custom(name: impl Into<CompactString>) -> Self {
Self {
kind: ActionKind::Custom {
name: name.into(),
params: Vec::new(),
},
}
}
pub fn assign(location: impl Into<CompactString>, expr: impl Into<CompactString>) -> Self {
Self {
kind: ActionKind::Assign {
location: location.into(),
expr: expr.into(),
},
}
}
pub fn log(label: Option<CompactString>, expr: Option<CompactString>) -> Self {
Self {
kind: ActionKind::Log { label, expr },
}
}
pub fn send_to(event: impl Into<CompactString>, target: impl Into<CompactString>) -> Self {
Self {
kind: ActionKind::Send {
event: event.into(),
target: Some(target.into()),
delay: None,
},
}
}
pub fn cancel(sendid: impl Into<CompactString>) -> Self {
Self {
kind: ActionKind::Cancel {
sendid: sendid.into(),
},
}
}
pub fn script(content: impl Into<CompactString>) -> Self {
Self {
kind: ActionKind::Script {
content: content.into(),
},
}
}
pub fn invoke(
invoke_type: Option<CompactString>,
src: Option<CompactString>,
id: Option<CompactString>,
) -> Self {
Self {
kind: ActionKind::Invoke {
invoke_type,
src,
id,
},
}
}
}