xmachines-machine 6.0.0

The contract of a typed XState v6 machine compiled into a guest of its own (a WASI component, a Node.js addon), and the JSON actor API the guests share
Documentation

xmachines-machine

The contract of a typed machine: one XState v6 machine, usually generated by xmachines-build, whose guards, actions, delays and invoked actors are Rust compiled into a guest of its own, a WASI component (xmachines-machine-wasi) or a Node.js addon (xmachines-machine-node).

When to use it

A machine crate implements TypedMachine once; each guest crate's export_machine! turns it into that guest's exports, so the same implementation builds a WASI component and a Node.js addon. Use this crate directly when you write that implementation, or a guest for another target.

What the guests share does not depend on the target, so it is here too: the machine compiled once per guest (compiled), its id and version (info), and JsonMachine and JsonActor, the machine and actor resources of the typed world xmachines:machine with values as JSON text. Event inputs are deserialized into the machine's Event type at the boundary; a chunk with any other is refused (WORKFLOW_INVALID_INPUT).

Example

use serde_json::{json, Value};
use xmachines_machine::{info, JsonMachine, TypedMachine};
use xmachines_runtime::{Machine, Registry};

struct Toggle;

impl TypedMachine for Toggle {
    type Context = Value;
    type Event = Value;
    const DEFINITION: &'static str = r#"{
        "id": "toggle", "initial": "off", "context": {},
        "states": { "off": { "on": { "toggle": { "target": "on" } } }, "on": {} }
    }"#;
    const SCHEMAS: &'static str = "{}";

    fn machine() -> Result<Machine<Value, Value>, String> {
        let definition: Value = serde_json::from_str(Self::DEFINITION).map_err(|e| e.to_string())?;
        Machine::from_json(&definition, &Registry::new()).map_err(|e| e.to_string())
    }
}

fn main() -> Result<(), String> {
    assert_eq!(info::<Toggle>().id, "toggle");
    let mut actor = JsonMachine::<Toggle>::load("{}")?.start("{}", 0)?;
    let step: Value = serde_json::from_str(&actor.apply(r#"[{ "kind": "time", "now": 1 }, { "type": "toggle" }]"#)?).unwrap();
    assert_eq!(step["root"]["value"], json!("on"));
    Ok(())
}

With codegen, Context and Event are the generated types, and DEFINITION, SCHEMAS and machine() are the generated module's (examples/checkout-wasi).

Contract

TypedMachine is the Rust side of the WIT package xmachines:machine@0.3.0 (wit/machine.wit of xmachines-machine-wasi): its info interface reports id, version, DEFINITION and SCHEMAS, JsonMachine implements its machine resource and JsonActor its actor resource. JsonMachine::load takes load options { "maxSteps"?, "maxTimers"?, "observeSystemEmitted"? } (a typed machine compiles its children and actions in, so it takes no children or hostActions; options that do not parse are the contract's WORKFLOW_INVALID_INPUT, a machine that does not compile WORKFLOW_INVALID_MACHINE), start takes { "input"?, "id"? } (the root's id root unless it names one; a root that fails as it starts is the contract's WORKFLOW_MACHINE_FAILED, and no actor) and restore the snapshot alone (one that does not restore is WORKFLOW_INVALID_SNAPSHOT); JsonActor::apply takes the contract's chunks and gives its steps and apply errors, a rejection { "rejected": { "message", "data"? } } or a fault { "code", "message" }, the JSON that the packages' JSON Schemas describe.

A typed machine's host actions are its bundle's hostActions: the code xmachines-build generates names them in the registry and has no method of them, so they are work of kind action the host settles done, and every other custom action runs inside the guest.

Related crates

License

MIT