Skip to main content

xmachines_workflow/
work.rs

1//! What a run waits for the host to do ([`Work`]): invoked work
2//! ([`Invoke`]), which the host settles `resolved` or `rejected`, and host
3//! actions ([`Action`]), which it settles `done`. `kind` is always written.
4
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7
8use crate::serde_util::present;
9
10/// Work the machine waits for, in every snapshot, told apart by `kind`:
11/// `{ "kind": "invoke", "id", "actor", "src", "input"? }` or
12/// `{ "kind": "action", "id", "actor", "type", "params"? }`.
13///
14/// `I` and `A` are the shapes of each kind: the loose [`Invoke`] and
15/// [`Action`] by default, or the enums `xmachines-build` generates of a
16/// machine's bundle, one variant for each `src` and each host action, tagged
17/// by `src` and by `type`.
18#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
19#[serde(tag = "kind", rename_all = "lowercase")]
20pub enum Work<I = Invoke, A = Action> {
21    /// Async work, as an invoked promise: a work result of `resolved` or
22    /// `rejected` settles it.
23    Invoke(I),
24    /// A host action: an action the machine took that the host does, then
25    /// confirms by a work result of `done`. It stays until then, so the host
26    /// sees it again after a crash: at least once.
27    Action(A),
28}
29
30/// Invoked work of any `src`: the loose shape of [`Work::Invoke`].
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
32pub struct Invoke {
33    /// What a work result names to settle it.
34    pub id: String,
35    /// The address of the actor whose work it is, in XState's format: the
36    /// root's id, then `/` and the id of each invocation down to it.
37    pub actor: String,
38    /// The logic the host runs.
39    pub src: String,
40    /// What the logic takes, when it takes anything: `null` is kept.
41    #[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "present")]
42    pub input: Option<Value>,
43}
44
45/// A host action of any `type`: the loose shape of [`Work::Action`].
46#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
47pub struct Action {
48    /// What a work result names to settle it.
49    pub id: String,
50    /// The address of the actor that took the action.
51    pub actor: String,
52    /// The action's name.
53    #[serde(rename = "type")]
54    pub ty: String,
55    /// The action's resolved params, when it has any: `null` is kept.
56    #[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "present")]
57    pub params: Option<Value>,
58}
59
60/// The id of a piece of work, which its result names: what every shape of
61/// work has.
62pub trait WorkId {
63    /// The work's id.
64    fn id(&self) -> &str;
65}
66
67impl WorkId for Invoke {
68    fn id(&self) -> &str {
69        &self.id
70    }
71}
72
73impl WorkId for Action {
74    fn id(&self) -> &str {
75        &self.id
76    }
77}
78
79impl<I: WorkId, A: WorkId> WorkId for Work<I, A> {
80    fn id(&self) -> &str {
81        match self {
82            Self::Invoke(work) => work.id(),
83            Self::Action(work) => work.id(),
84        }
85    }
86}