Skip to main content

sim_lib_scene/
build.rs

1//! Scene construction helpers.
2//!
3//! Re-exports the `sim-value` builders and adds a few common scene node shapes
4//! plus a reserved-key guard. The guard turns the `kind` footgun into an
5//! immediate, clear failure: `kind` is the scene-node tag, so a plain data map
6//! must not carry a `kind` key (use a different field name). [`data_map`]
7//! debug-asserts that.
8
9use sim_kernel::Expr;
10
11pub use sim_value::build::{float, int, list, map, sym, text, vector};
12
13use crate::model::node;
14
15/// Keys reserved for scene-node structure; plain data maps must not use them.
16pub const RESERVED_DATA_KEYS: &[&str] = &["kind"];
17
18/// A `scene/stack` node with a direction and children.
19pub fn stack(dir: &str, children: Vec<Expr>) -> Expr {
20    node(
21        "stack",
22        vec![("dir", sym(dir)), ("children", list(children))],
23    )
24}
25
26/// A `scene/box` node with a role and children.
27pub fn box_(role: &str, children: Vec<Expr>) -> Expr {
28    node(
29        "box",
30        vec![("role", sym(role)), ("children", list(children))],
31    )
32}
33
34/// A `scene/badge` node. Status carries a text token, never color alone.
35pub fn badge(status: &str, label: &str) -> Expr {
36    node(
37        "badge",
38        vec![("status", sym(status)), ("label", text(label))],
39    )
40}
41
42/// A `scene/text` node.
43pub fn text_node(content: impl Into<String>) -> Expr {
44    node("text", vec![("text", text(content.into()))])
45}
46
47/// Build a plain data map, asserting (in debug) that it carries no reserved
48/// scene-node key.
49pub fn data_map(entries: Vec<(&str, Expr)>) -> Expr {
50    debug_assert!(
51        entries
52            .iter()
53            .all(|(key, _)| !RESERVED_DATA_KEYS.contains(key)),
54        "data_map: a plain data map must not use a reserved scene-node key (e.g. 'kind'); \
55         rename the field"
56    );
57    map(entries)
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn scene_shape_helpers_validate() {
66        let scene = stack(
67            "column",
68            vec![box_("summary", vec![text_node("hi"), badge("ok", "done")])],
69        );
70        crate::model::validate_scene(&scene).expect("helper scenes validate");
71    }
72
73    #[test]
74    fn data_map_allows_non_reserved_keys() {
75        let value = data_map(vec![("style", sym("line")), ("at", int(3))]);
76        assert!(matches!(value, Expr::Map(_)));
77    }
78
79    #[test]
80    #[should_panic(expected = "reserved scene-node key")]
81    fn data_map_rejects_a_reserved_key_in_debug() {
82        let _ = data_map(vec![("kind", sym("line"))]);
83    }
84}