xmachines-workflow 6.0.0

The record protocol of a statechart run, as serde types: chunks of events, time and work results in, events, snapshots and rejections out, faults as data
Documentation
//! What a run waits for the host to do ([`Work`]): invoked work
//! ([`Invoke`]), which the host settles `resolved` or `rejected`, and host
//! actions ([`Action`]), which it settles `done`. `kind` is always written.

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::serde_util::present;

/// Work the machine waits for, in every snapshot, told apart by `kind`:
/// `{ "kind": "invoke", "id", "actor", "src", "input"? }` or
/// `{ "kind": "action", "id", "actor", "type", "params"? }`.
///
/// `I` and `A` are the shapes of each kind: the loose [`Invoke`] and
/// [`Action`] by default, or the enums `xmachines-build` generates of a
/// machine's bundle, one variant for each `src` and each host action, tagged
/// by `src` and by `type`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum Work<I = Invoke, A = Action> {
    /// Async work, as an invoked promise: a work result of `resolved` or
    /// `rejected` settles it.
    Invoke(I),
    /// A host action: an action the machine took that the host does, then
    /// confirms by a work result of `done`. It stays until then, so the host
    /// sees it again after a crash: at least once.
    Action(A),
}

/// Invoked work of any `src`: the loose shape of [`Work::Invoke`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Invoke {
    /// What a work result names to settle it.
    pub id: String,
    /// The address of the actor whose work it is, in XState's format: the
    /// root's id, then `/` and the id of each invocation down to it.
    pub actor: String,
    /// The logic the host runs.
    pub src: String,
    /// What the logic takes, when it takes anything: `null` is kept.
    #[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "present")]
    pub input: Option<Value>,
}

/// A host action of any `type`: the loose shape of [`Work::Action`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Action {
    /// What a work result names to settle it.
    pub id: String,
    /// The address of the actor that took the action.
    pub actor: String,
    /// The action's name.
    #[serde(rename = "type")]
    pub ty: String,
    /// The action's resolved params, when it has any: `null` is kept.
    #[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "present")]
    pub params: Option<Value>,
}

/// The id of a piece of work, which its result names: what every shape of
/// work has.
pub trait WorkId {
    /// The work's id.
    fn id(&self) -> &str;
}

impl WorkId for Invoke {
    fn id(&self) -> &str {
        &self.id
    }
}

impl WorkId for Action {
    fn id(&self) -> &str {
        &self.id
    }
}

impl<I: WorkId, A: WorkId> WorkId for Work<I, A> {
    fn id(&self) -> &str {
        match self {
            Self::Invoke(work) => work.id(),
            Self::Action(work) => work.id(),
        }
    }
}