diff --git a/.github/test-suite-registry.json b/.github/test-suite-registry.json index c021d69e00..43e902b57a 100644 --- a/.github/test-suite-registry.json +++ b/.github/test-suite-registry.json @@ -177,6 +177,7 @@ "pkgs/id/examples/roc-chat/**", "pkgs/id/examples/roc-counter/**", "pkgs/id/examples/roc-grid/**", + "pkgs/id/examples/roc-mind-map/**", "pkgs/id/examples/roc-screen/**", "pkgs/id/examples/roc-text-counter/**", "pkgs/id/examples/territory/**", diff --git a/pkgs/id/book/design/first-apps.md b/pkgs/id/book/design/first-apps.md index a0e5d76af1..5893d92036 100644 --- a/pkgs/id/book/design/first-apps.md +++ b/pkgs/id/book/design/first-apps.md @@ -18,9 +18,9 @@ The did-it data model has its own investigation: `2026-10-29T13-04-50Z_investiga - Early surfaces. `2026-03-20T05-30-00Z_feature_collaborative_cursor_enhancements` (2026-03-20) and `2026-03-23T00:00:00Z_feature_file_list_ui` (2026-03-23) are the earliest records in this theme — collaborative cursors and the file list UI of the pre-worlds editor generation. `2026-04-01T00-00-00Z_feature_multi_instance` covers multi-instance support for `id`. `2026-10-17T09-00-00Z_feature_world_fixtures` designs session recording to replayable fixtures, the testing base the app designs rely on. - Games and assets. `2026-11-08T09-14-46Z_research_id5988_prior_art_bot_games_programmable_worlds` is the prior-art research on scripted-bot battle games and user-programmable worlds. `2026-11-06T00-50-21Z_design_dynamic_world_tick_cost` designs the dynamic-world per-tick cost model and the bot-battle benchmark. `2026-11-08T00-57-08Z_design_shader_asset_format` designs the shader asset format v1. `2026-10-16T15-00-00Z_feature_roc_grid` is the RocGrid record — a bounds-checked grid library for world programs, the library the grid guest app demonstrates. -## The shipped first apps: the 9 Roc guest apps +## The shipped first apps: the 10 Roc guest apps -The platform's working first apps are the 9 deployable Roc guest apps in `pkgs/id/examples/`, plus the one packaged app. They run on the shared Plaza platform (`examples/roc-world/`), whose guest shape is fixed: every app provides `init`, `update`, `view`, `layout`, `records`, `wants`, `snapshot` and `restore`; guests are compiled to import-free Wasm modules with no WASI, no filesystem, network, clock or randomness imports; all effects go through the capability protocol, with requests answered as participant-0 events and refusals returned as events with stable error codes (`cap_denied`, `unknown_cap`, `bad_args`, `failed`, `actor_denied`, `outside_ceiling`, `result_too_large`). The full platform treatment is in [Design: worlds and the guest runtime](worlds-runtime.md). +The platform's working first apps are the 10 deployable Roc guest apps in `pkgs/id/examples/`, plus the one packaged app. They run on the shared Plaza platform (`examples/roc-world/`), whose guest shape is fixed: every app provides `init`, `update`, `view`, `layout`, `records`, `wants`, `snapshot` and `restore`; guests are compiled to import-free Wasm modules with no WASI, no filesystem, network, clock or randomness imports; all effects go through the capability protocol, with requests answered as participant-0 events and refusals returned as events with stable error codes (`cap_denied`, `unknown_cap`, `bad_args`, `failed`, `actor_denied`, `outside_ceiling`, `result_too_large`). The full platform treatment is in [Design: worlds and the guest runtime](worlds-runtime.md). | App | Location | What it is | |---|---|---| @@ -30,6 +30,7 @@ The platform's working first apps are the 9 deployable Roc guest apps in `pkgs/i | roc-text-counter | `examples/roc-text-counter/` | The no-Screen legacy-text twin of roc-counter; keeps the plain-text presentation path covered (#3181). | | roc-chat | `examples/roc-chat/` | Minimal chat client on `RocCap` with `chat.say` and `chat.history`; typing `hist` shows the newest 8 lines. | | roc-grid | `examples/roc-grid/` | RocGrid demo: a 4×3 byte grid, `set` claims the centre cell, bounds-checked reads with fallback. | +| roc-mind-map | `examples/roc-mind-map/` | Mind map for `mind-map/same-label-distinct-nodes` (#13609): stable node IDs, a typed `related` cross-link that survives reparent and rename, refused single-history cycles, preserved positions. | | roc-screen | `examples/roc-screen/` | Screen and Key demo: a bordered-box counter screen document; presenters render the structured grid. | | territory | `examples/territory/` | Multiplayer territory game: at most 8 seated players on a 16×8 grid, adjacency-only claiming, seats persist across leave, rejoin and restart. | | roc-cap-demo | `examples/roc-cap-demo/` | Library-import worked example (#5957): a capability-dependent Roc library over `records.read`, with two compile-must-fail negative tests. | @@ -62,7 +63,7 @@ An example with invented data, following the v1 design: a creator exports the pr Grounding, from the library survey for this book (checkout `1ccb4a19457`, 2026-11-23): -- Shipped: the 9 Roc guest apps and the packaged tic-tac-toe app listed in the table above, with checked-in Wasm artifacts beside most apps so host conformance suites run without a Roc toolchain. These are the first apps in the literal sense: working guest programs on the shipped Plaza platform. +- Shipped: the 10 Roc guest apps and the packaged tic-tac-toe app listed in the table above, with checked-in Wasm artifacts beside most apps so host conformance suites run without a Roc toolchain. These are the first apps in the literal sense: working guest programs on the shipped Plaza platform. - Shipped: the program-side machinery the ecosystem designs build on — the program registry and gallery surface named in the CLI (`gallery`, `publish`, `unpublish`, `publications` command families exist in the shipped CLI table), and the bundle record path the #10662 listing design distinguishes itself from: author-signed bundle records with author, name, version and payload hash, fetched by content hash and verified on the bundle path. - Shipped as a proof of concept only: the first app of #2031 — sticky notes plus did-it — exists as the integration test `tests/first_app_poc.rs` and a README in `examples/first-app-poc/`. It is not a packaged application. - Still design: the ecosystem ladder as designed — signed bundle export/import/fork as the v1 user flow (#6291 is a design record; the record states no implementation code is in it), the gallery as a registry with update channels (#6293, design only), the hosted public gallery as an operated instance (#6294, design only), and creator tooling (#6308, design only). The chat v1, boards and match-attestation designs are likewise design records; the shipped chat and boards functionality in the `id` program is the earlier application surface documented in the reference chapters, not an implementation of these v1 designs — the records themselves make no completion claim, and this chapter makes none for them. diff --git a/pkgs/id/book/development/add-a-guest.md b/pkgs/id/book/development/add-a-guest.md index 3ae396c624..c948cb372a 100644 --- a/pkgs/id/book/development/add-a-guest.md +++ b/pkgs/id/book/development/add-a-guest.md @@ -14,6 +14,7 @@ Worked examples live in `pkgs/id/examples/`. Each app is a directory holding its | `examples/roc-text-counter/` | The same counter on the legacy plain-text presentation, importing no platform modules. | | `examples/roc-chat/` | Chat over the capability protocol: `chat.say` and `chat.history` through `RocCap`. | | `examples/roc-grid/` | The `RocGrid` library: a 4×3 byte grid with bounds-checked reads. | +| `examples/roc-mind-map/` | Stable IDs with duplicate labels, a typed cross-link, refused cycles, preserved positions. | | `examples/roc-screen/` | The `Screen` and `Key` libraries: a structured screen document instead of text. | | `examples/lounge/` | The capability system end to end: subscriptions, arrival greetings through `chat.say`, and refusal counting. | | `examples/territory/` | A complete multiplayer game: at most 8 seated players on a 16×8 grid, adjacency-only claiming, seats that persist across leave, rejoin and restart. | diff --git a/pkgs/id/book/library/guests-and-shells.md b/pkgs/id/book/library/guests-and-shells.md index 7d1661f44d..37f6405c0f 100644 --- a/pkgs/id/book/library/guests-and-shells.md +++ b/pkgs/id/book/library/guests-and-shells.md @@ -37,9 +37,9 @@ The runtime provider set named in `ABI.md` is `time.now`, `time.tick` (subscribe The Roc client library is `RocCap`: a session issuing request ids `r0`, `r1`, and so on, with `issue`, `attach`, `requests`, and `route`, and builders `chat_request`, `now_request`, `players_request`, `world_request`, `history_request`, `records_request`, `schedule_at_request`, and `schedule_cancel_request`. -## The nine Roc guest apps +## The ten Roc guest apps -Checked-in `.wasm` artefacts sit beside most apps, so the host conformance suites run without a Roc toolchain. `pkgs/id/apps/tic-tac-toe/` is the sole packaged app; the other eight live in `pkgs/id/examples/`. +Checked-in `.wasm` artefacts sit beside most apps, so the host conformance suites run without a Roc toolchain. `pkgs/id/apps/tic-tac-toe/` is the sole packaged app; the other nine live in `pkgs/id/examples/`. | App | Location | What it does | |---|---|---| @@ -49,6 +49,7 @@ Checked-in `.wasm` artefacts sit beside most apps, so the host conformance suite | roc-text-counter | `examples/roc-text-counter/` | The no-`Screen` legacy-text twin of roc-counter: imports no platform modules and keeps the plain-text presentation path covered (#3181). | | roc-chat | `examples/roc-chat/` | Minimal chat client on `RocCap` with `chat.say` and `chat.history`; typing `hist` shows the newest 8 lines. | | roc-grid | `examples/roc-grid/` | `RocGrid` demo: a 4×3 byte grid where `set` claims the centre cell; bounds-checked reads with a fallback. | +| roc-mind-map | `examples/roc-mind-map/` | Mind map for `mind-map/same-label-distinct-nodes` (#13609): nodes distinguished by stable ID with duplicate `Archive` labels, a typed `related` cross-link through reparent and rename, refused single-history cycles, preserved positions; `records` and `snapshot` publish the same domain document. | | roc-screen | `examples/roc-screen/` | `Screen` and `Key` demo: a bordered-box counter screen document, `+` increments; presenters (SSH ANSI, browser DOM) render the structured grid. | | territory | `examples/territory/` | Multiplayer territory game: at most 8 seated players on a 16×8 grid, adjacency-only claiming, seats persist across leave, rejoin, and restart. | | roc-cap-demo | `examples/roc-cap-demo/` | Library-import worked example (#5957): a capability-dependent Roc library (`lib/Greeter.roc` over `records.read`) plus its app, with two compile-must-fail negative tests — a missing capability on `platform-no-caps.roc`, and a wrong `RocCap` signature. | diff --git a/pkgs/id/crates/id-worlds/src/sandbox.rs b/pkgs/id/crates/id-worlds/src/sandbox.rs index 2f24523d11..e788d78762 100644 --- a/pkgs/id/crates/id-worlds/src/sandbox.rs +++ b/pkgs/id/crates/id-worlds/src/sandbox.rs @@ -2106,6 +2106,80 @@ mod tests { assert!(text.contains("cells=11 at11=65 out=63"), "{text}"); } + #[test] + fn checked_in_mind_map_replays_same_label_distinct_nodes() { + // Oracle: mind-map/same-label-distinct-nodes in app-vectors.json + // (revision b9d8177). Inputs carry the event tag stripped; the after + // states below are literal copies of the vector projections. + let wasm = include_bytes!("../../../examples/roc-mind-map/mind-map.wasm"); + let runner = Sandbox::compile(wasm, SandboxLimits::default()).unwrap(); + let mut live = runner.instantiate(7).unwrap(); + let records = |world: &mut WorldInstance| -> serde_json::Value { + serde_json::from_slice(&world.records().unwrap().unwrap()).unwrap() + }; + let nodes = |u_name: &str, u_parent: &str| -> serde_json::Value { + serde_json::json!([ + {"id": "P", "name": "North", "parent": "R", "position": [0, 1]}, + {"id": "Q", "name": "South", "parent": "R", "position": [2, 1]}, + {"id": "R", "name": "World", "parent": null, "position": [1, 0]}, + {"id": "U", "name": u_name, "parent": u_parent, "position": [0, 2]}, + {"id": "V", "name": "Archive", "parent": "Q", "position": [2, 2]} + ]) + }; + // (input, expected outcome, expected after-state) + let steps = [ + ( + r#"{"kind":"AddCrossLink","source":"U","target":"V"}"#, + "linked", + serde_json::json!({ + "root": "R", + "nodes": nodes("Archive", "P"), + "cross_links": [{"from": "U", "to": "V"}] + }), + ), + ( + r#"{"kind":"Reparent","id":"U","new_parent":"Q"}"#, + "moved", + serde_json::json!({ + "root": "R", + "nodes": nodes("Archive", "Q"), + "cross_links": [{"from": "U", "to": "V"}] + }), + ), + ( + r#"{"kind":"Rename","id":"U","name":"Inbox"}"#, + "renamed", + serde_json::json!({ + "root": "R", + "nodes": nodes("Inbox", "Q"), + "cross_links": [{"from": "U", "to": "V"}] + }), + ), + ( + r#"{"kind":"Reparent","id":"Q","new_parent":"U"}"#, + "refused-cycle", + serde_json::json!({ + "root": "R", + "nodes": nodes("Inbox", "Q"), + "cross_links": [{"from": "U", "to": "V"}] + }), + ), + ]; + assert_eq!(records(&mut live)["nodes"], nodes("Archive", "P")); + for (input, outcome, after) in steps { + live.update(1, input.as_bytes()).unwrap(); + let doc = records(&mut live); + assert_eq!(doc["outcome"], outcome, "{input}"); + assert_eq!(doc["root"], after["root"], "{input}"); + assert_eq!(doc["nodes"], after["nodes"], "{input}"); + assert_eq!(doc["cross_links"], after["cross_links"], "{input}"); + } + let snapshot = live.snapshot().unwrap().unwrap(); + let mut restored = runner.instantiate(7).unwrap(); + restored.restore(&snapshot).unwrap(); + assert_eq!(records(&mut restored), records(&mut live)); + } + #[test] fn checked_in_chat_example_sends_and_reads_history() { use crate::world::{WorldEvent, WorldEventKind}; diff --git a/pkgs/id/examples/roc-mind-map/README.md b/pkgs/id/examples/roc-mind-map/README.md new file mode 100644 index 0000000000..cc4095aab9 --- /dev/null +++ b/pkgs/id/examples/roc-mind-map/README.md @@ -0,0 +1,40 @@ +# Roc Mind Map Example + +A playable mind-map world for `mind-map/same-label-distinct-nodes` (#13609). +Nodes are distinguished by stable ID, so two nodes can share the label +"Archive". A typed cross-link (`related`) keeps pointing at its node through +reparent and rename. A reparent that would put a node under its own descendant +is refused and leaves the tree unchanged. Positions never change. + +Inputs are JSON commands: + +- `{"kind":"AddCrossLink","source":"U","target":"V"}` links U to V (`linked`). +- `{"kind":"Reparent","id":"U","new_parent":"Q"}` moves U under Q (`moved`). +- `{"kind":"Rename","id":"U","name":"Inbox"}` relabels U (`renamed`). +- `{"kind":"Reparent","id":"Q","new_parent":"U"}` is refused (`refused-cycle`): + Q would land under its own descendant U, so the tree is unchanged. + +`records` publishes the domain state (`root`, `nodes` with `id`/`name`/`parent`/`position`, +`cross_links`, and the last `outcome`); `snapshot` is the same document, so a +snapshot restored into a fresh instance compares byte-identical through the +regular host. The view draws the nodes on a portable screen with the last +outcome and the link list. + +## Build + +```bash +./build.sh +``` + +Produces `mind-map.wasm`, validated as an import-free Wasm module. + +## Run + +```bash +id serve --data-dir ./data --world --world-admin-token "$ADMIN" \ + --world-module "$PWD/examples/roc-mind-map/mind-map.wasm" \ + --world-ssh-port 2223 --web --port 8787 +``` + +Join with SSH or the web page and send an input such as +`{"kind":"Rename","id":"U","name":"Inbox"}` to relabel U. diff --git a/pkgs/id/examples/roc-mind-map/build.sh b/pkgs/id/examples/roc-mind-map/build.sh new file mode 100755 index 0000000000..c79b5e956d --- /dev/null +++ b/pkgs/id/examples/roc-mind-map/build.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash +set -euo pipefail +cd "$(dirname "${BASH_SOURCE[0]}")" +export ROC="${ROC:-roc}" +source ../roc-toolchain.sh + +if [ -z "${WASM_TOOLS:-}" ]; then + if command -v wasm-tools >/dev/null 2>&1; then + WASM_TOOLS="wasm-tools" + else + STORE_TOOL=$(ls -d /nix/store/*-wasm-tools-*/bin/wasm-tools 2>/dev/null | head -n1 || true) + if [ -n "$STORE_TOOL" ] && [ -x "$STORE_TOOL" ]; then + WASM_TOOLS="$STORE_TOOL" + else + WASM_TOOLS="nix run nixpkgs#wasm-tools --" + fi + fi +fi + +if [ -z "${WASM_OPT:-}" ]; then + if command -v wasm-opt >/dev/null 2>&1; then + WASM_OPT="wasm-opt" + else + STORE_OPT=$(ls -d /nix/store/*-binaryen-*/bin/wasm-opt 2>/dev/null | head -n1 || true) + if [ -n "$STORE_OPT" ] && [ -x "$STORE_OPT" ]; then + WASM_OPT="$STORE_OPT" + else + WASM_OPT="nix shell nixpkgs#binaryen --command wasm-opt" + fi + fi +fi + +(cd ../roc-world && ./build-host.sh >/dev/null) +"$ROC" build main.roc --target=wasm32 --wasm-memory=8454144 --output=mind-map.wasm + +# Same size discipline as roc-grid: optimize, strip DWARF, keep the name section +# so Wasmtime traps still resolve, then validate the import-free module. +$WASM_OPT -Os --all-features mind-map.wasm -o mind-map.wasm.opt +mv mind-map.wasm.opt mind-map.wasm +$WASM_TOOLS strip mind-map.wasm -o mind-map.wasm.stripped +mv mind-map.wasm.stripped mind-map.wasm +$WASM_TOOLS validate mind-map.wasm +echo "Built mind-map.wasm (import-free Roc world module)" diff --git a/pkgs/id/examples/roc-mind-map/main.roc b/pkgs/id/examples/roc-mind-map/main.roc new file mode 100644 index 0000000000..10dc7703a3 --- /dev/null +++ b/pkgs/id/examples/roc-mind-map/main.roc @@ -0,0 +1,226 @@ +app [program] { pf: platform "../roc-world/platform.roc" } + +import pf.RocJson +import pf.Screen + +# A mind map for mind-map/same-label-distinct-nodes: nodes are identified by +# stable IDs, so two nodes can share the label "Archive". A typed cross-link +# keeps pointing at its node through reparent and rename. A reparent that +# would put a node under its own descendant is refused and leaves the tree +# unchanged. Positions are never changed by an operation. +# +# Each player event is one JSON command. Host events (participant 0) are ignored. +# `records` publishes the domain state, and `snapshot` is the same document. + +Node : { id : Str, name : Str, parent : Str, x : U64, y : U64 } +Link : { from : Str, to : Str } +Model : { root : Str, nodes : List(Node), links : List(Link), outcome : Str } + +program = { init, update, view, layout, records, wants, snapshot, restore } + +init : U64 -> Model +init = |_seed| initial + +initial : Model +initial = { + root: "R", + nodes: [ + { id: "P", name: "North", parent: "R", x: 0, y: 1 }, + { id: "Q", name: "South", parent: "R", x: 2, y: 1 }, + { id: "R", name: "World", parent: "", x: 1, y: 0 }, + { id: "U", name: "Archive", parent: "P", x: 0, y: 2 }, + { id: "V", name: "Archive", parent: "Q", x: 2, y: 2 }, + ], + links: [], + outcome: "", +} + +update : Model, U64, Str -> Model +update = |model, pid, ev| + if pid == 0 { + model + } else { + command(model, ev) + } + +command : Model, Str -> Model +command = |model, text| match RocJson.decode(text) { + Err(_) => { ..model, outcome: "invalid" } + Ok(cmd) => { + kind = string_field(cmd, "kind") + if kind == "AddCrossLink" { + add_link(model, string_field(cmd, "source"), string_field(cmd, "target")) + } else if kind == "Reparent" { + reparent(model, string_field(cmd, "id"), string_field(cmd, "new_parent")) + } else if kind == "Rename" { + rename(model, string_field(cmd, "id"), string_field(cmd, "name")) + } else { + { ..model, outcome: "invalid" } + } + } +} + +add_link : Model, Str, Str -> Model +add_link = |model, source, target| + if source != target and has_node(model.nodes, source) and has_node(model.nodes, target) and !has_link(model.links, source, target) { + { ..model, links: List.append(model.links, { from: source, to: target }), outcome: "linked" } + } else { + { ..model, outcome: "refused" } + } + +reparent : Model, Str, Str -> Model +reparent = |model, id, new_parent| + if id == model.root or !has_node(model.nodes, id) or !has_node(model.nodes, new_parent) { + { ..model, outcome: "refused" } + } else if reaches(model.nodes, new_parent, id, List.len(model.nodes)) { + { ..model, outcome: "refused-cycle" } + } else { + { ..model, nodes: List.map(model.nodes, |n| if n.id == id { { ..n, parent: new_parent } } else { n }), outcome: "moved" } + } + +rename : Model, Str, Str -> Model +rename = |model, id, name| + if has_node(model.nodes, id) { + { ..model, nodes: List.map(model.nodes, |n| if n.id == id { { ..n, name: name } } else { n }), outcome: "renamed" } + } else { + { ..model, outcome: "refused" } + } + +# Walks up from `start`; true when `target` is reached. `fuel` bounds the walk. +reaches : List(Node), Str, Str, U64 -> Bool +reaches = |nodes, start, target, fuel| + if start == target { + Bool.True + } else if fuel == 0 { + Bool.False + } else match find_node(nodes, start) { + Ok(found) => reaches(nodes, found.parent, target, fuel - 1) + Err(_) => Bool.False + } + +has_node : List(Node), Str -> Bool +has_node = |nodes, id| List.any(nodes, |n| n.id == id) + +has_link : List(Link), Str, Str -> Bool +has_link = |links, from, to| List.any(links, |l| l.from == from and l.to == to) + +find_node : List(Node), Str -> Try(Node, [NotFound]) +find_node = |nodes, id| match List.find_first(nodes, |n| n.id == id) { + Ok(found) => Ok(found) + Err(_) => Err(NotFound) +} + +plain = { fg: 0, bg: 0, bold: Bool.False } + +view : Model -> Str +view = |model| { + frame = Screen.boxed(Screen.new(40, 10), 0, 0, 40, 10, "mind map") + drawn = List.fold(model.nodes, frame, |screen, n| + Screen.put_text(screen, n.x * 14 + 2, n.y * 2 + 1, "${n.id} ${n.name}", plain)) + links = Str.join_with(List.map(model.links, |l| "${l.from}-${l.to}"), " ") + Screen.render_json( + Screen.put_text(Screen.put_text(drawn, 2, 7, "last=${model.outcome}", plain), 2, 8, "links=${links}", plain), + ) +} + +## This program renders the same layout at any viewport. +layout : Model, U64, U64 -> Str +layout = |model, _width, _height| view(model) + +records : Model -> Str +records = |model| encode_or_crash(document(model)) + +## No subscriptions and no requests. +wants : Model -> Str +wants = |_model| "{}" + +snapshot : Model -> Str +snapshot = |model| encode_or_crash(document(model)) + +restore : Str -> Model +restore = |text| match RocJson.decode(text) { + Err(_) => initial + Ok(doc) => + if string_field(doc, "root") == "" { + initial + } else { + { + root: string_field(doc, "root"), + nodes: List.map(array_field(doc, "nodes"), node_of), + links: List.map(array_field(doc, "cross_links"), link_of), + outcome: string_field(doc, "outcome"), + } + } +} + +# --- helpers --------------------------------------------------------------- + +document : Model -> RocJson +document = |model| RocJson.Object([ + { key: "root", value: RocJson.String(model.root) }, + { key: "nodes", value: RocJson.Array(List.map(model.nodes, node_json)) }, + { key: "cross_links", value: RocJson.Array(List.map(model.links, link_json)) }, + { key: "outcome", value: RocJson.String(model.outcome) }, +]) + +node_json : Node -> RocJson +node_json = |n| RocJson.Object([ + { key: "id", value: RocJson.String(n.id) }, + { key: "name", value: RocJson.String(n.name) }, + { key: "parent", value: if n.parent == "" { RocJson.Null } else { RocJson.String(n.parent) } }, + { key: "position", value: RocJson.Array([RocJson.Number(n.x.to_str()), RocJson.Number(n.y.to_str())]) }, +]) + +link_json : Link -> RocJson +link_json = |l| RocJson.Object([ + { key: "from", value: RocJson.String(l.from) }, + { key: "to", value: RocJson.String(l.to) }, +]) + +node_of : RocJson -> Node +node_of = |item| { + id: string_field(item, "id"), + name: string_field(item, "name"), + parent: string_field(item, "parent"), + x: position_at(item, 0), + y: position_at(item, 1), +} + +link_of : RocJson -> Link +link_of = |item| { from: string_field(item, "from"), to: string_field(item, "to") } + +encode_or_crash = |value| match RocJson.encode(value) { + Ok(text) => text + Err(_) => crash "mind-map attempted to encode an invalid or oversized JSON value" +} + +field = |value, key| match RocJson.get(value, key) { + Ok(found) => found + Err(_) => RocJson.Null +} + +string_field = |value, key| match RocJson.as_string(field(value, key)) { + Ok(text) => text + Err(_) => "" +} + +array_field = |value, key| match RocJson.as_array(field(value, key)) { + Ok(items) => items + Err(_) => [] +} + +position_at = |item, index| match RocJson.as_array(field(item, "position")) { + Ok(pos) => match List.get(pos, index) { + Ok(found) => number(found) + Err(_) => 0 + } + Err(_) => 0 +} + +number = |value| match RocJson.as_number(value) { + Ok(text) => match U64.from_str(text) { + Ok(found) => found + Err(_) => 0 + } + Err(_) => 0 +} diff --git a/pkgs/id/examples/roc-mind-map/mind-map.wasm b/pkgs/id/examples/roc-mind-map/mind-map.wasm new file mode 100644 index 0000000000..e345491862 Binary files /dev/null and b/pkgs/id/examples/roc-mind-map/mind-map.wasm differ diff --git a/pkgs/id/scripts/rebuild-roc-examples.sh b/pkgs/id/scripts/rebuild-roc-examples.sh index 0d70ae9a5f..fa4014cdc3 100644 --- a/pkgs/id/scripts/rebuild-roc-examples.sh +++ b/pkgs/id/scripts/rebuild-roc-examples.sh @@ -21,6 +21,7 @@ examples=( examples/roc-chat examples/roc-counter examples/roc-grid + examples/roc-mind-map examples/roc-screen examples/roc-text-counter examples/territory diff --git a/pkgs/id/tests/toolchain_pin.rs b/pkgs/id/tests/toolchain_pin.rs index 7ed25b94a6..3981b3f759 100644 --- a/pkgs/id/tests/toolchain_pin.rs +++ b/pkgs/id/tests/toolchain_pin.rs @@ -10,11 +10,12 @@ use std::fs; use std::path::Path; use std::process::Command; -const ROC_EXAMPLE_BUILDS: [&str; 8] = [ +const ROC_EXAMPLE_BUILDS: [&str; 9] = [ "examples/lounge/build.sh", "examples/roc-chat/build.sh", "examples/roc-counter/build.sh", "examples/roc-grid/build.sh", + "examples/roc-mind-map/build.sh", "examples/roc-screen/build.sh", "examples/roc-text-counter/build.sh", "examples/territory/build.sh",