use predicator_rs::{Map, Value};
pub(crate) const SCXML_EVENT_PROCESSOR: &str = "http://www.w3.org/TR/scxml/#SCXMLEventProcessor";
pub(crate) fn scxml_location(session_id: &str) -> String {
format!("#_scxml_{session_id}")
}
pub(crate) fn initial(session_id: &str, name: Option<&str>, entries: Map) -> Map {
let mut ioprocessors = entries;
let mut scxml = Map::new();
scxml.insert("location", Value::from(scxml_location(session_id)));
ioprocessors.insert(SCXML_EVENT_PROCESSOR, Value::Map(scxml));
let mut variables = Map::new();
variables.insert("_sessionid", Value::from(session_id));
variables.insert("_name", absent(name));
variables.insert("_event", Value::Undefined);
variables.insert("_ioprocessors", Value::Map(ioprocessors));
variables
}
pub(crate) fn seed(host: Map, variables: Map) -> Map {
let mut data = host;
for (root, value) in variables {
data.insert(root, value);
}
data
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum EventType {
Internal,
External,
Platform,
}
impl EventType {
pub(crate) fn as_str(self) -> &'static str {
match self {
EventType::Internal => "internal",
EventType::External => "external",
EventType::Platform => "platform",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct EventFields<'a> {
pub(crate) name: &'a str,
pub(crate) event_type: EventType,
pub(crate) sendid: Option<&'a str>,
pub(crate) origin: Option<&'a str>,
pub(crate) origintype: Option<&'a str>,
pub(crate) invokeid: Option<&'a str>,
pub(crate) data: Value,
}
pub(crate) fn event(fields: &EventFields<'_>) -> Value {
let mut map = Map::new();
map.insert("name", Value::from(fields.name));
map.insert("type", Value::from(fields.event_type.as_str()));
map.insert("sendid", absent(fields.sendid));
map.insert("origin", absent(fields.origin));
map.insert("origintype", absent(fields.origintype));
map.insert("invokeid", absent(fields.invokeid));
map.insert("data", fields.data.clone());
Value::Map(map)
}
pub(crate) fn put_event(data: &Map, event: Value) -> Map {
let mut data = data.clone();
data.insert("_event", event);
data
}
fn absent(value: Option<&str>) -> Value {
value.map_or(Value::Undefined, Value::from)
}