Skip to main content

Crate xmachines_machine

Crate xmachines_machine 

Source
Expand description

§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.

§License

MIT

Structs§

Info
The machine a guest was built for, as the typed world’s info reports it.
JsonActor
An actor of a TypedMachine behind the typed world’s actor API: chunks, steps and results go in and out as JSON text, and errors are messages, or the contract’s error of a refused chunk: a rejection, { "rejected": { "message", "data"? } }, or a fault, { "code", "message" }. Each guest wraps it in its own actor type.
JsonMachine
The compiled machine of a TypedMachine with its load options, as the typed world’s machine resource holds it: every actor started or restored from it shares both.
Run
A run of a machine on xmachines-runtime, one chunk at a time: the Stepper of the workflow contract. It applies a chunk with one call of the runtime, which puts the run back itself when it refuses the chunk: the machine is shared and never compiled again.

Traits§

TypedMachine
The machine a guest is built for: its types, its definition and schemas as codegen emits them, and how to compile it with its implementations.

Functions§

compiled
The machine of T, compiled by the first call and shared by every actor of the guest instance after it (a failure is kept too: the definition and the implementations are compiled in, so it would fail again).
info
The id and version of T’s machine, from its definition.
open_run
A run of T’s compiled machine, as the typed world’s machine-runs opens one: run options of JSON text in (workflow-options.schema.json), the run and its opening records out, each JSON text. The run takes the events of T’s Event type alone. An error is the record protocol’s fault as JSON text, { "code", "message", "seq" }: a machine that does not compile is WORKFLOW_INVALID_MACHINE.

Type Aliases§

Compiled
A compiled machine, or why it did not compile.
TypedRun
A run of a TypedMachine’s machine, of its context and event types.