concinnity-dev 0.19.9

The Concinnity dev tooling library: world authoring, the in-engine editor, the debug server, docs and packaging
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
// src/editor/behavior/outline.rs
//
// Flattening a Behavior's args into the panel's row list. Everything the asset
// declares -- its source, its scope, locals, queries, and the whole node body
// with its expression trees -- becomes one indented outline, so a node graph
// and its operands are browsed and edited the same way. Each row carries the
// path of the value it edits and, when it is a list member, the path the
// delete / reorder buttons act on.

use serde_json::Value;

use super::palette::{self, Shape};
use super::path::{Path, Step, child, field};

// How a row's text is typed back into the args when the value field commits.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Text {
    // A plain string: a variable, local, binding, or component name.
    Str,
    // A string that clears to null when emptied: an optional asset name.
    OptStr,
    // A number.
    Num,
    // Three comma-separated numbers.
    Vec3,
}

// The list a row's palette appends to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum List {
    Nodes,
    Scope,
    Locals,
    Queries,
    // The component names one query matches on.
    Components,
    // The operands of an `all` / `any` expression.
    Operands,
}

// What a row offers: which clicks it answers, and which palette (if any) its
// Pick button opens.
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Kind {
    // The `on` source; clicking steps to the next one.
    Source,
    // A boolean; clicking flips it.
    Flag,
    // A fixed word set; clicking steps to the next option.
    Choice(&'static [&'static str]),
    // Edited through the value field.
    Text(Text),
    // A typed constant: the palette picks the type, the value field the payload.
    Literal,
    // A slot holding one node; the palette replaces it.
    Node,
    // A slot holding one expression; the palette replaces it, and a literal or
    // named expression also takes its payload from the value field. An optional
    // slot can be cleared back to unset.
    Expr { optional: bool },
    // A list the palette appends to.
    List(List),
}

// The fixed word sets a `Choice` row steps through.
pub(crate) const CUE_KINDS: &[&str] = &["sound", "music"];
pub(crate) const TRANSITIONS: &[&str] = &["FadeBlack", "Cut"];
pub(crate) const PLAYBACKS: &[&str] = &["start", "continue"];

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Row {
    pub depth: usize,
    pub label: String,
    pub value: String,
    pub kind: Kind,
    pub path: Path,
    // The list element the delete / reorder buttons act on, when this row is
    // one. A node's is its own path; a local's name row points at the whole
    // local, so deleting from the name deletes the declaration.
    pub element: Option<Path>,
}

impl Row {
    fn new(depth: usize, label: &str, value: impl Into<String>, kind: Kind, path: Path) -> Row {
        Row {
            depth,
            label: label.to_string(),
            value: value.into(),
            kind,
            path,
            element: None,
        }
    }

    fn member(mut self, element: Path) -> Row {
        self.element = Some(element);
        self
    }
}

// The whole behavior as one outline.
pub(crate) fn rows(args: &Value) -> Vec<Row> {
    let mut out = Vec::new();
    source_rows(args, &mut out);
    out.push(Row::new(
        0,
        "once",
        bool_text(args.get("once")),
        Kind::Flag,
        vec![field("once")],
    ));
    for key in ["delay", "cooldown"] {
        out.push(Row::new(
            0,
            key,
            num_text(args.get(key)),
            Kind::Text(Text::Num),
            vec![field(key)],
        ));
    }
    scope_rows(args, &mut out);
    local_rows(args, &mut out);
    query_rows(args, &mut out);
    node_list(args.get("do"), 0, vec![field("do")], "do", &mut out);
    out
}

fn source_rows(args: &Value, out: &mut Vec<Row>) {
    let on = args.get("on");
    let verb = on.map_or("start", palette::verb_of);
    let base = vec![field("on")];
    out.push(Row::new(
        0,
        "on",
        source_summary(verb, on),
        Kind::Source,
        base.clone(),
    ));
    let body = on.and_then(palette::body_of);
    match verb {
        "timer" => {
            let timer = child(&base, field("timer"));
            out.push(Row::new(
                1,
                "interval",
                num_text(body.and_then(|b| b.get("interval"))),
                Kind::Text(Text::Num),
                child(&timer, field("interval")),
            ));
            out.push(Row::new(
                1,
                "repeat",
                bool_text(body.and_then(|b| b.get("repeat"))),
                Kind::Flag,
                child(&timer, field("repeat")),
            ));
        }
        "variable" => out.push(Row::new(
            1,
            "name",
            str_text(body),
            Kind::Text(Text::Str),
            child(&base, field("variable")),
        )),
        "enter" | "exit" => out.push(Row::new(
            1,
            "volume",
            str_text(body),
            Kind::Text(Text::OptStr),
            child(&base, field(verb)),
        )),
        "interact" => out.push(Row::new(
            1,
            "entity",
            str_text(body),
            Kind::Text(Text::OptStr),
            child(&base, field("interact")),
        )),
        _ => {}
    }
}

fn scope_rows(args: &Value, out: &mut Vec<Row>) {
    let base = vec![field("scope")];
    let items = array(args.get("scope"));
    out.push(Row::new(
        0,
        "scope",
        count_text(items.len()),
        Kind::List(List::Scope),
        base.clone(),
    ));
    for (i, item) in items.iter().enumerate() {
        let p = child(&base, Step::Index(i));
        out.push(
            Row::new(
                1,
                "component",
                str_text(Some(item)),
                Kind::Text(Text::Str),
                p.clone(),
            )
            .member(p),
        );
    }
}

fn local_rows(args: &Value, out: &mut Vec<Row>) {
    let base = vec![field("locals")];
    let items = array(args.get("locals"));
    out.push(Row::new(
        0,
        "locals",
        count_text(items.len()),
        Kind::List(List::Locals),
        base.clone(),
    ));
    for (i, item) in items.iter().enumerate() {
        let element = child(&base, Step::Index(i));
        out.push(
            Row::new(
                1,
                "local",
                str_text(item.get("name")),
                Kind::Text(Text::Str),
                child(&element, field("name")),
            )
            .member(element.clone()),
        );
        let value = item.get("value");
        out.push(Row::new(
            2,
            "value",
            value.map_or_else(|| "unset".to_string(), expr_summary),
            Kind::Literal,
            child(&element, field("value")),
        ));
    }
}

fn query_rows(args: &Value, out: &mut Vec<Row>) {
    let base = vec![field("queries")];
    let items = array(args.get("queries"));
    out.push(Row::new(
        0,
        "queries",
        count_text(items.len()),
        Kind::List(List::Queries),
        base.clone(),
    ));
    for (i, item) in items.iter().enumerate() {
        let element = child(&base, Step::Index(i));
        out.push(
            Row::new(
                1,
                "query",
                str_text(item.get("name")),
                Kind::Text(Text::Str),
                child(&element, field("name")),
            )
            .member(element.clone()),
        );
        let has = child(&element, field("has"));
        let components = array(item.get("has"));
        out.push(Row::new(
            2,
            "has",
            count_text(components.len()),
            Kind::List(List::Components),
            has.clone(),
        ));
        for (j, component) in components.iter().enumerate() {
            let p = child(&has, Step::Index(j));
            out.push(
                Row::new(
                    3,
                    "component",
                    str_text(Some(component)),
                    Kind::Text(Text::Str),
                    p.clone(),
                )
                .member(p),
            );
        }
    }
}

// A node list (`do`, an `if` branch, a `for_each` body): its heading row and
// every node under it.
fn node_list(value: Option<&Value>, depth: usize, path: Path, label: &str, out: &mut Vec<Row>) {
    let items = array(value);
    out.push(Row::new(
        depth,
        label,
        count_text(items.len()),
        Kind::List(List::Nodes),
        path.clone(),
    ));
    for (i, node) in items.iter().enumerate() {
        node_rows(node, depth + 1, child(&path, Step::Index(i)), out);
    }
}

// The fields of one node: its body, the path that body sits at, and the depth
// its rows indent to. Every field is keyed by the name it carries in the schema,
// which is also the label its row draws.
struct Fields<'a> {
    body: Option<&'a Value>,
    base: Path,
    depth: usize,
}

impl<'a> Fields<'a> {
    fn at(&self, key: &str) -> Option<&'a Value> {
        self.body.and_then(|v| v.get(key))
    }

    fn path(&self, key: &str) -> Path {
        child(&self.base, field(key))
    }

    fn text(&self, out: &mut Vec<Row>, key: &str, t: Text) {
        let value = self.at(key);
        let text = match t {
            Text::Num => num_text(value),
            Text::Vec3 => vec3_text(value),
            Text::Str | Text::OptStr => str_text(value),
        };
        out.push(Row::new(
            self.depth,
            key,
            text,
            Kind::Text(t),
            self.path(key),
        ));
    }

    fn expr(&self, out: &mut Vec<Row>, key: &str, optional: bool) {
        expr_rows(self.at(key), key, self.depth, self.path(key), optional, out);
    }

    fn nodes(&self, out: &mut Vec<Row>, key: &str) {
        node_list(self.at(key), self.depth, self.path(key), key, out);
    }

    fn flag(&self, out: &mut Vec<Row>, key: &str) {
        let text = bool_text(self.at(key));
        out.push(Row::new(self.depth, key, text, Kind::Flag, self.path(key)));
    }

    fn choice(&self, out: &mut Vec<Row>, key: &str, options: &'static [&'static str]) {
        let text = word_text(self.at(key), options);
        let kind = Kind::Choice(options);
        out.push(Row::new(self.depth, key, text, kind, self.path(key)));
    }
}

fn node_rows(node: &Value, depth: usize, path: Path, out: &mut Vec<Row>) {
    let verb = palette::verb_of(node);
    if !palette::known(palette::NODES, verb) {
        out.push(Row::new(depth, "node", "(unknown)", Kind::Node, path.clone()).member(path));
        return;
    }
    let body = palette::body_of(node);
    let summary = node_summary(verb, body);
    out.push(Row::new(depth, verb, summary, Kind::Node, path.clone()).member(path.clone()));
    let f = Fields {
        body,
        base: child(&path, field(verb)),
        depth: depth + 1,
    };
    match verb {
        "if" => {
            f.expr(out, "cond", false);
            f.nodes(out, "then");
            f.nodes(out, "else");
        }
        "for_each" => {
            f.text(out, "query", Text::Str);
            f.text(out, "bind", Text::Str);
            f.nodes(out, "do");
        }
        "let" => {
            f.text(out, "name", Text::Str);
            f.expr(out, "value", false);
        }
        "set" | "set_local" => {
            f.text(out, if verb == "set" { "var" } else { "local" }, Text::Str);
            f.expr(out, "value", false);
            f.flag(out, "add");
        }
        "set_transform" => {
            f.expr(out, "entity", false);
            for key in ["position", "rotation_deg", "scale"] {
                f.expr(out, key, true);
            }
        }
        "spawn" => {
            f.text(out, "template", Text::OptStr);
            for key in ["position", "rotation_deg", "scale"] {
                f.text(out, key, Text::Vec3);
            }
            f.text(out, "lifetime", Text::Num);
            f.text(out, "bind", Text::OptStr);
        }
        "despawn" | "show" | "hide" => f.expr(out, "target", false),
        "reparent" => {
            f.expr(out, "child", false);
            f.expr(out, "parent", true);
        }
        "sound" => {
            f.text(out, "clip", Text::OptStr);
            f.choice(out, "kind", CUE_KINDS);
            f.text(out, "volume", Text::Num);
        }
        "scene" => {
            f.text(out, "scene", Text::OptStr);
            f.choice(out, "transition", TRANSITIONS);
        }
        "screen" => f.text(out, "screen", Text::OptStr),
        // The playback command is the node's whole body, not a field of it.
        "story" => {
            let text = word_text(body, PLAYBACKS);
            let kind = Kind::Choice(PLAYBACKS);
            out.push(Row::new(f.depth, "playback", text, kind, f.base));
        }
        _ => {}
    }
}

// One expression slot and, when it nests, the operands under it.
fn expr_rows(
    value: Option<&Value>,
    label: &str,
    depth: usize,
    path: Path,
    optional: bool,
    out: &mut Vec<Row>,
) {
    let kind = Kind::Expr { optional };
    let Some(value) = value.filter(|v| !v.is_null()) else {
        out.push(Row::new(depth, label, "unset", kind, path));
        return;
    };
    let verb = palette::verb_of(value);
    out.push(Row::new(
        depth,
        label,
        expr_summary(value),
        kind,
        path.clone(),
    ));
    if !palette::known(palette::EXPRS, verb) {
        return;
    }
    let body = palette::body_of(value);
    let b = child(&path, field(verb));
    let d = depth + 1;
    match palette::shape(verb) {
        Shape::Unary => expr_rows(body, "a", d, b, false, out),
        Shape::Binary => {
            for (i, name) in ["a", "b"].iter().enumerate() {
                let operand = body.and_then(|v| v.get(i));
                expr_rows(operand, name, d, child(&b, Step::Index(i)), false, out);
            }
        }
        Shape::List => {
            let items = array(body);
            out.push(Row::new(
                d,
                "of",
                count_text(items.len()),
                Kind::List(List::Operands),
                b.clone(),
            ));
            for (i, operand) in items.iter().enumerate() {
                let p = child(&b, Step::Index(i));
                let before = out.len();
                expr_rows(Some(operand), &i.to_string(), d + 1, p.clone(), false, out);
                out[before].element = Some(p);
            }
        }
        Shape::Unit | Shape::Literal | Shape::Name => {}
    }
}

// The right-column detail of a source row: the verb plus whatever parameter
// makes one source distinguishable from another of the same kind.
pub(crate) fn source_summary(verb: &str, on: Option<&Value>) -> String {
    let body = on.and_then(palette::body_of);
    match verb {
        "timer" => {
            let interval = num_text(body.and_then(|b| b.get("interval")));
            let repeat = body
                .and_then(|b| b.get("repeat"))
                .and_then(Value::as_bool)
                .unwrap_or(false);
            format!(
                "timer {interval}s{}",
                if repeat { " repeating" } else { "" }
            )
        }
        "variable" | "enter" | "exit" | "interact" => {
            let name = str_text(body);
            if name.is_empty() {
                format!("{verb} (unnamed)")
            } else {
                format!("{verb} {name}")
            }
        }
        other if palette::known(palette::SOURCES, other) => other.to_string(),
        _ => "(unknown)".to_string(),
    }
}

// The right-column detail of a node row: the one field that identifies it at a
// glance, so a collapsed read of the body still says what each node touches.
fn node_summary(verb: &str, body: Option<&Value>) -> String {
    let at = |key: &str| body.and_then(|v| v.get(key));
    let name = |key: &str| str_text(at(key));
    let target = |key: &str| at(key).map(expr_summary).unwrap_or_default();
    match verb {
        "for_each" => format!("{} -> {}", name("query"), name("bind")),
        "let" | "set_local" | "spawn" | "scene" | "screen" | "sound" => name(first_field(verb)),
        "set" => name("var"),
        "set_transform" => target("entity"),
        "despawn" | "show" | "hide" => target("target"),
        "reparent" => target("child"),
        "story" => body.and_then(Value::as_str).unwrap_or("start").to_string(),
        _ => String::new(),
    }
}

// The field a node is summarized by, for the verbs whose summary is one name.
fn first_field(verb: &str) -> &'static str {
    match verb {
        "let" => "name",
        "set_local" => "local",
        "spawn" => "template",
        "scene" => "scene",
        "screen" => "screen",
        _ => "clip",
    }
}

// An expression as one line: the verb, plus its payload for the leaf shapes
// that carry one.
pub(crate) fn expr_summary(value: &Value) -> String {
    let verb = palette::verb_of(value);
    if !palette::known(palette::EXPRS, verb) {
        return "(unknown)".to_string();
    }
    match palette::shape(verb) {
        Shape::Literal => format!("{verb} {}", literal_text(palette::body_of(value))),
        Shape::Name => {
            let name = str_text(palette::body_of(value));
            if name.is_empty() {
                format!("{verb} (unnamed)")
            } else {
                format!("{verb} {name}")
            }
        }
        _ => verb.to_string(),
    }
}

// A literal's payload as text: what the value field seeds with and commits.
pub(crate) fn literal_text(body: Option<&Value>) -> String {
    match body {
        Some(Value::Bool(b)) => b.to_string(),
        Some(Value::Array(_)) => vec3_text(body),
        Some(Value::Number(_)) => num_text(body),
        _ => String::new(),
    }
}

fn array(value: Option<&Value>) -> &[Value] {
    value
        .and_then(Value::as_array)
        .map(Vec::as_slice)
        .unwrap_or(&[])
}

fn count_text(n: usize) -> String {
    format!("({n})")
}

fn bool_text(value: Option<&Value>) -> String {
    value.and_then(Value::as_bool).unwrap_or(false).to_string()
}

fn str_text(value: Option<&Value>) -> String {
    value.and_then(Value::as_str).unwrap_or("").to_string()
}

// A fixed-word field's current value, falling back to the first option so a
// missing or malformed word still shows something the row can step from.
fn word_text(value: Option<&Value>, options: &[&str]) -> String {
    let current = str_text(value);
    if options.contains(&current.as_str()) {
        current
    } else {
        options.first().unwrap_or(&"").to_string()
    }
}

fn num_text(value: Option<&Value>) -> String {
    fmt_num(value.and_then(Value::as_f64).unwrap_or(0.0))
}

fn vec3_text(value: Option<&Value>) -> String {
    let a = array(value);
    let at = |i: usize| fmt_num(a.get(i).and_then(Value::as_f64).unwrap_or(0.0));
    format!("{}, {}, {}", at(0), at(1), at(2))
}

// Numbers read back the way they were typed: a whole value keeps no trailing
// `.0`, so re-committing a row does not churn the authored JSON.
pub(crate) fn fmt_num(n: f64) -> String {
    if n.fract() == 0.0 && n.abs() < 1.0e15 {
        format!("{}", n as i64)
    } else {
        format!("{n}")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn find<'a>(rows: &'a [Row], label: &str) -> &'a Row {
        rows.iter()
            .find(|r| r.label == label)
            .unwrap_or_else(|| panic!("no `{label}` row in {:?}", labels(rows)))
    }

    fn labels(rows: &[Row]) -> Vec<&str> {
        rows.iter().map(|r| r.label.as_str()).collect()
    }

    // Every behavior, however empty, shows the same declaration skeleton, so
    // there is always somewhere to start authoring from.
    #[test]
    fn a_blank_behavior_still_offers_every_section() {
        let rows = rows(&json!({}));
        for label in [
            "on", "once", "delay", "cooldown", "scope", "locals", "queries", "do",
        ] {
            let r = find(&rows, label);
            assert_eq!(r.depth, 0, "`{label}` is a top-level row");
        }
        assert_eq!(find(&rows, "on").value, "start");
        assert_eq!(find(&rows, "do").value, "(0)");
        assert_eq!(find(&rows, "do").kind, Kind::List(List::Nodes));
    }

    #[test]
    fn a_timer_source_discloses_its_interval_and_repeat() {
        let rows = rows(&json!({"on": {"timer": {"interval": 5.0, "repeat": true}}}));
        assert_eq!(find(&rows, "on").value, "timer 5s repeating");
        let interval = find(&rows, "interval");
        assert_eq!(interval.value, "5");
        assert_eq!(interval.kind, Kind::Text(Text::Num));
        assert_eq!(
            interval.path,
            vec![field("on"), field("timer"), field("interval")]
        );
        assert_eq!(find(&rows, "repeat").kind, Kind::Flag);
        // A source with no parameters discloses nothing.
        let tick = super::rows(&json!({"on": "tick"}));
        assert!(!labels(&tick).contains(&"interval"));
    }

    #[test]
    fn declarations_nest_their_parts_under_a_deletable_member() {
        let rows = rows(&json!({
            "scope": ["Prop"],
            "locals": [{"name": "speed", "value": {"float": 3.0}}],
            "queries": [{"name": "player", "has": ["Camera3D"]}],
        }));
        let component = find(&rows, "component");
        assert_eq!(component.value, "Prop");
        assert_eq!(
            component.element,
            Some(vec![field("scope"), Step::Index(0)])
        );

        let local = find(&rows, "local");
        assert_eq!(local.value, "speed");
        assert_eq!(
            local.path,
            vec![field("locals"), Step::Index(0), field("name")]
        );
        assert_eq!(
            local.element,
            Some(vec![field("locals"), Step::Index(0)]),
            "deleting from the name row deletes the whole declaration"
        );
        let value = find(&rows, "value");
        assert_eq!(value.kind, Kind::Literal);
        assert_eq!(value.value, "float 3");

        let query = find(&rows, "query");
        assert_eq!(query.value, "player");
        assert_eq!(find(&rows, "has").kind, Kind::List(List::Components));
    }

    // The body flattens into an indented tree: a node's fields sit under it and
    // an expression's operands under those.
    #[test]
    fn the_body_flattens_into_an_indented_tree() {
        let rows = rows(&json!({"scope": ["Prop"], "do": [
            {"if": {"cond": {"lt": [{"distance": ["self", "self"]}, {"float": 20.0}]},
                    "then": [{"save": null}], "else": []}}]}));
        let body = find(&rows, "do");
        assert_eq!(body.value, "(1)");
        let branch = find(&rows, "if");
        assert_eq!(branch.depth, body.depth + 1);
        assert_eq!(branch.kind, Kind::Node);
        assert_eq!(branch.element, Some(vec![field("do"), Step::Index(0)]));

        let cond = find(&rows, "cond");
        assert_eq!(cond.depth, branch.depth + 1);
        assert_eq!(cond.value, "lt");
        // Both operands are addressed by index under the verb, and the nested
        // comparison's own operands sit a level deeper again.
        let under_cond = |steps: &[Step]| {
            let mut p = vec![field("do"), Step::Index(0), field("if"), field("cond")];
            p.extend_from_slice(steps);
            rows.iter().find(|r| r.path == p).expect("row at that path")
        };
        let lhs = under_cond(&[field("lt"), Step::Index(0)]);
        assert_eq!((lhs.label.as_str(), lhs.value.as_str()), ("a", "distance"));
        assert_eq!(lhs.depth, cond.depth + 1);
        let rhs = under_cond(&[field("lt"), Step::Index(1)]);
        assert_eq!((rhs.label.as_str(), rhs.value.as_str()), ("b", "float 20"));
        let inner = under_cond(&[
            field("lt"),
            Step::Index(0),
            field("distance"),
            Step::Index(1),
        ]);
        assert_eq!(inner.value, "self");
        assert_eq!(inner.depth, lhs.depth + 1);
        // Both branches are listed, the empty one included, so a node can be
        // added to either.
        let branches: Vec<&Row> = rows
            .iter()
            .filter(|r| r.label == "then" || r.label == "else")
            .collect();
        assert_eq!(branches.len(), 2);
        assert_eq!(branches[0].value, "(1)");
        assert_eq!(branches[1].value, "(0)");
    }

    #[test]
    fn optional_transform_fields_read_as_unset() {
        let rows = rows(&json!({"do": [
            {"set_transform": {"entity": "self", "position": {"vec3": [0, 1, 0]}}}]}));
        assert_eq!(find(&rows, "position").value, "vec3 0, 1, 0");
        assert_eq!(find(&rows, "scale").value, "unset");
        assert_eq!(
            find(&rows, "scale").kind,
            Kind::Expr { optional: true },
            "an omitted transform field can still be cleared back to unset"
        );
        assert_eq!(
            find(&rows, "entity").kind,
            Kind::Expr { optional: false },
            "the entity is required"
        );
    }

    #[test]
    fn an_operand_list_is_its_own_appendable_row() {
        let rows = rows(&json!({"do": [
            {"if": {"cond": {"all": [{"bool": true}, {"bool": false}]}}}]}));
        let of = find(&rows, "of");
        assert_eq!(of.kind, Kind::List(List::Operands));
        assert_eq!(of.value, "(2)");
        let first = find(&rows, "0");
        assert_eq!(first.value, "bool true");
        assert_eq!(
            first.element,
            Some(vec![
                field("do"),
                Step::Index(0),
                field("if"),
                field("cond"),
                field("all"),
                Step::Index(0)
            ]),
            "operands are removable"
        );
    }

    #[test]
    fn node_rows_summarize_the_field_that_identifies_them() {
        let rows = rows(&json!({"do": [
            {"set": {"var": "visits", "value": {"int": 1}, "add": true}},
            {"for_each": {"query": "props", "bind": "e", "do": []}},
            {"hide": {"target": {"named": "door"}}},
            {"sound": {"clip": "ping", "kind": "music", "volume": 0.5}},
            {"story": "continue"}]}));
        assert_eq!(find(&rows, "set").value, "visits");
        assert_eq!(find(&rows, "for_each").value, "props -> e");
        assert_eq!(find(&rows, "hide").value, "named door");
        assert_eq!(find(&rows, "sound").value, "ping");
        assert_eq!(find(&rows, "kind").kind, Kind::Choice(CUE_KINDS));
        assert_eq!(find(&rows, "kind").value, "music");
        assert_eq!(find(&rows, "playback").value, "continue");
        assert_eq!(find(&rows, "add").kind, Kind::Flag);
        assert_eq!(find(&rows, "add").value, "true");
    }

    // Malformed authored JSON is browsable rather than fatal: an unknown verb
    // shows as one row with no children invented for it.
    #[test]
    fn unknown_verbs_do_not_grow_a_subtree() {
        let rows = rows(&json!({"do": [{"teleport": {"to": "x"}}]}));
        let node = find(&rows, "node");
        assert_eq!(node.value, "(unknown)");
        assert!(node.element.is_some(), "it can still be deleted");
        assert_eq!(rows.iter().filter(|r| r.depth > node.depth).count(), 0);
    }

    #[test]
    fn numbers_read_back_without_a_trailing_zero() {
        assert_eq!(fmt_num(3.0), "3");
        assert_eq!(fmt_num(-0.5), "-0.5");
        assert_eq!(fmt_num(0.0), "0");
    }

    #[test]
    fn a_missing_choice_word_falls_back_to_the_first_option() {
        assert_eq!(word_text(None, CUE_KINDS), "sound");
        assert_eq!(
            word_text(Some(&json!("nonsense")), TRANSITIONS),
            "FadeBlack"
        );
        assert_eq!(word_text(Some(&json!("Cut")), TRANSITIONS), "Cut");
    }
}