determa-state 0.0.5

Determa State — Rust implementation of the Determa statechart engine (implements Determa State spec v0.0.5)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! YAML machine-definition model (SPEC §4) and raw deserialization.
//!
//! These structs mirror `schema/machine.schema.json` closely. Structural validation
//! (unknown fields, required fields, basic shapes) is enforced by serde's
//! `deny_unknown_fields` plus [`crate::validate`]; richer semantic checks (reference
//! resolution, choice defaults/acyclicity, esv scope, contracts) live in the validator.

use crate::value::Value;
use serde::Deserialize;
use std::collections::BTreeMap;

// --- action (parsed from a single-key mapping) -----------------------------

#[derive(Debug, Clone, PartialEq)]
pub enum Action {
    /// `{ assign: { var: "<cel>", ... } }`
    Assign(Vec<(String, String)>),
    /// `{ publish: { event, to?, payload? } }`
    Publish {
        event: String,
        to: Option<String>,
        payload: Vec<(String, String)>,
    },
    /// `{ refresh: {} }` or `{ refresh: { only: [...] } }`
    Refresh { only: Option<Vec<String>> },
    /// `{ spawn: { def, payload?, result? } }`
    Spawn {
        def: String,
        payload: Vec<(String, String)>,
        result: Option<String>,
    },
    /// `{ stop: {} }`
    Stop,
}

/// Parse one action from its raw YAML mapping.
pub fn parse_action(raw: &serde_yaml::Value) -> Result<Action, String> {
    let map = raw
        .as_mapping()
        .ok_or_else(|| "action must be a mapping".to_string())?;
    if map.len() != 1 {
        return Err(format!(
            "action must have exactly one key, found {}",
            map.len()
        ));
    }
    let (k, v) = map.iter().next().unwrap();
    let key = k.as_str().ok_or_else(|| "action key must be a string".to_string())?;
    match key {
        "assign" => {
            let m = v.as_mapping().ok_or("assign value must be a mapping")?;
            let mut pairs = Vec::new();
            for (kk, vv) in m {
                let name = kk.as_str().ok_or("assign key must be a string")?.to_string();
                let expr = vv.as_str().ok_or("assign value must be a CEL string")?.to_string();
                pairs.push((name, expr));
            }
            if pairs.is_empty() {
                return Err("assign must have at least one variable".to_string());
            }
            Ok(Action::Assign(pairs))
        }
        "publish" => {
            let m = v.as_mapping().ok_or("publish value must be a mapping")?;
            let event = m
                .get("event")
                .and_then(|x| x.as_str())
                .ok_or("publish.event is required")?
                .to_string();
            let to = m.get("to").and_then(|x| x.as_str()).map(|s| s.to_string());
            let payload = parse_payload_cel(m.get("payload"))?;
            Ok(Action::Publish { event, to, payload })
        }
        "refresh" => {
            let only = if let Some(m) = v.as_mapping() {
                m.get("only")
                    .and_then(|x| x.as_sequence())
                    .map(|seq| {
                        seq.iter()
                            .filter_map(|x| x.as_str().map(|s| s.to_string()))
                            .collect()
                    })
            } else {
                None
            };
            Ok(Action::Refresh { only })
        }
        "spawn" => {
            let m = v.as_mapping().ok_or("spawn value must be a mapping")?;
            let def = m
                .get("def")
                .and_then(|x| x.as_str())
                .ok_or("spawn.def is required")?
                .to_string();
            let payload = parse_payload_cel(m.get("payload"))?;
            let result = m.get("result").and_then(|x| x.as_str()).map(|s| s.to_string());
            Ok(Action::Spawn {
                def,
                payload,
                result,
            })
        }
        "stop" => Ok(Action::Stop),
        other => Err(format!("unknown action '{other}'")),
    }
}

fn parse_payload_cel(v: Option<&serde_yaml::Value>) -> Result<Vec<(String, String)>, String> {
    match v {
        None => Ok(Vec::new()),
        Some(serde_yaml::Value::Null) => Ok(Vec::new()),
        Some(m) => {
            let m = m.as_mapping().ok_or("payload must be a mapping")?;
            let mut pairs = Vec::new();
            for (kk, vv) in m {
                let name = kk.as_str().ok_or("payload key must be a string")?.to_string();
                let expr = vv.as_str().ok_or("payload value must be a CEL string")?.to_string();
                pairs.push((name, expr));
            }
            Ok(pairs)
        }
    }
}

pub fn parse_actions(raw: Option<&serde_yaml::Value>) -> Result<Vec<Action>, String> {
    match raw {
        None => Ok(Vec::new()),
        Some(serde_yaml::Value::Null) => Ok(Vec::new()),
        Some(seq) => {
            let seq = seq.as_sequence().ok_or("action list must be a sequence")?;
            seq.iter().map(parse_action).collect()
        }
    }
}

// --- raw serde structs -----------------------------------------------------

#[derive(Debug, Clone, Deserialize)]
pub struct Languages {
    #[serde(default)]
    pub guard: Option<String>,
    #[serde(default)]
    pub action: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PayloadField {
    #[serde(rename = "type")]
    pub ty: String,
    #[serde(default)]
    pub required: Option<bool>,
    #[serde(default)]
    pub default: Option<serde_yaml::Value>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EventDecl {
    #[serde(default)]
    pub scope: Option<String>,
    #[serde(default)]
    pub payload: Option<BTreeMap<String, PayloadField>>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EsvDecl {
    #[serde(rename = "type")]
    pub ty: String,
    #[serde(default)]
    pub init: Option<serde_yaml::Value>,
    #[serde(default)]
    pub external: Option<bool>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Transition {
    #[serde(default)]
    pub transition_to: Option<String>,
    #[serde(default)]
    pub guard: Option<String>,
    #[serde(default)]
    pub lang: Option<String>,
    #[serde(default)]
    pub action: Option<serde_yaml::Value>,
    #[serde(default)]
    pub internal: Option<bool>,
    #[serde(default)]
    pub local: Option<bool>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
pub enum TransitionOrList {
    One(Transition),
    Many(Vec<Transition>),
}

#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct InitialTransition {
    pub transition_to: String,
    #[serde(default)]
    pub guard: Option<String>,
    #[serde(default)]
    pub action: Option<serde_yaml::Value>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ChoiceBranch {
    pub transition_to: String,
    #[serde(default)]
    pub guard: Option<String>,
    #[serde(default)]
    pub action: Option<serde_yaml::Value>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AfterTimer {
    pub duration: String,
    #[serde(default)]
    pub transition_to: Option<String>,
    #[serde(default)]
    pub guard: Option<String>,
    #[serde(default)]
    pub action: Option<serde_yaml::Value>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Region {
    pub initial: InitialTransition,
    pub states: BTreeMap<String, StateNode>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct StateNode {
    #[serde(rename = "type", default)]
    pub ty: Option<String>,
    #[serde(default)]
    pub esvs: Option<BTreeMap<String, EsvDecl>>,
    #[serde(default)]
    pub entry: Option<serde_yaml::Value>,
    #[serde(default)]
    pub exit: Option<serde_yaml::Value>,
    #[serde(default)]
    pub initial: Option<InitialTransition>,
    #[serde(default)]
    pub states: Option<BTreeMap<String, StateNode>>,
    #[serde(default)]
    pub regions: Option<Vec<Region>>,
    #[serde(default)]
    pub on_events: Option<BTreeMap<String, TransitionOrList>>,
    #[serde(default)]
    pub after: Option<Vec<AfterTimer>>,
    #[serde(default)]
    pub defer: Option<Vec<String>>,
    #[serde(default)]
    pub history: Option<String>,
    #[serde(default)]
    pub choice: Option<Vec<ChoiceBranch>>,
    /// `meta`: opaque state annotations for host layers (SPEC §4.5). Purely
    /// informative — no effect on dispatch/validation/snapshots.
    #[serde(default)]
    pub meta: Option<serde_yaml::Value>,
    /// `submachine: <id>` inlines another definition synchronously (SPEC §5.6.1).
    #[serde(default)]
    pub submachine: Option<String>,
    /// `with: { <externalEsv>: <CEL> }` seeds the submachine's external esvs.
    #[serde(default)]
    pub with: Option<BTreeMap<String, String>>,
    /// Engine-set: this node is the inlined `top` of a submachine (esv-scope
    /// boundary). Never appears in YAML.
    #[serde(skip)]
    pub is_sm_boundary: bool,
    #[serde(skip)]
    pub sm_with: BTreeMap<String, String>,
}

/// Inline `submachine:` references into a nested state tree (SPEC §5.6.1).
///
/// A state with `submachine: <id>` becomes a composite whose single child (named
/// `<id>`) is the referenced definition's `top` (recursively inlined), marked as an
/// esv-scope boundary and carrying the `with:` seeding. `registry` maps definition
/// id -> its raw `top`. Returns errors for unknown or cyclic references.
pub fn inline_submachines(
    node: &StateNode,
    registry: &std::collections::HashMap<String, StateNode>,
    stack: &std::collections::HashSet<String>,
) -> Result<StateNode, Vec<crate::loader::LoadError>> {
    use crate::loader::LoadError;
    if let Some(sub_id) = &node.submachine {
        let reg_top = match registry.get(sub_id) {
            Some(t) => t,
            None => {
                return Err(vec![LoadError {
                    path: "/submachine".into(),
                    message: format!("unknown submachine '{sub_id}'"),
                }]);
            }
        };
        if stack.contains(sub_id) {
            return Err(vec![LoadError {
                path: "/submachine".into(),
                message: format!("cyclic submachine '{sub_id}'"),
            }]);
        }
        let mut new_stack = stack.clone();
        new_stack.insert(sub_id.clone());
        let mut child = inline_submachines(reg_top, registry, &new_stack)?;
        child.is_sm_boundary = true;
        child.sm_with = node.with.clone().unwrap_or_default();
        let mut out = node.clone();
        out.submachine = None;
        out.with = None;
        out.is_sm_boundary = false;
        out.sm_with = BTreeMap::new();
        let mut states = BTreeMap::new();
        states.insert(sub_id.clone(), child);
        out.states = Some(states);
        out.initial = Some(InitialTransition {
            transition_to: sub_id.clone(),
            guard: None,
            action: None,
        });
        return Ok(out);
    }
    let mut out = node.clone();
    if let Some(states) = &node.states {
        let mut new_states = BTreeMap::new();
        for (k, v) in states {
            new_states.insert(k.clone(), inline_submachines(v, registry, stack)?);
        }
        out.states = Some(new_states);
    }
    if let Some(regions) = &node.regions {
        let mut new_regions = Vec::new();
        for r in regions {
            let mut nr = r.clone();
            let mut ns = BTreeMap::new();
            for (k, v) in &r.states {
                ns.insert(k.clone(), inline_submachines(v, registry, stack)?);
            }
            nr.states = ns;
            new_regions.push(nr);
        }
        out.regions = Some(new_regions);
    }
    Ok(out)
}

#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Migration {
    pub from: i64,
    pub to: i64,
    #[serde(default)]
    pub when: Option<String>,
    #[serde(default)]
    pub state_map: Option<BTreeMap<String, String>>,
    #[serde(default)]
    pub esvs: Option<serde_yaml::Value>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RawMachine {
    #[serde(default)]
    pub format: Option<i64>,
    pub id: String,
    #[serde(default)]
    pub version: Option<i64>,
    #[serde(default)]
    pub contracts: Vec<String>,
    #[serde(default)]
    pub subscribe: Vec<String>,
    #[serde(default)]
    pub languages: Option<Languages>,
    #[serde(default)]
    pub events: Option<BTreeMap<String, EventDecl>>,
    #[serde(default)]
    pub migrations: Option<Vec<Migration>>,
    /// `meta`: opaque machine-level annotations for host layers (SPEC §4.1).
    /// Purely informative — no effect on dispatch/validation/snapshots.
    #[serde(default)]
    pub meta: Option<serde_yaml::Value>,
    pub top: StateNode,
}

/// Convert a raw esv init literal (YAML) into a Value, checking it matches the type.
pub fn esv_init_value(ty: &str, raw: &serde_yaml::Value) -> Result<Value, String> {
    let v = Value::from_yaml(raw);
    if v.matches_type(ty) {
        Ok(v)
    } else {
        Err(format!(
            "esv init {:?} does not match declared type '{}'",
            v, ty
        ))
    }
}