Skip to content

feat(graph-wasm): cognicode-graph-wasm bindgen shim (PR #3)#23

Open
Rubentxu wants to merge 3 commits into
mainfrom
feat/graph-wasm-shim
Open

feat(graph-wasm): cognicode-graph-wasm bindgen shim (PR #3)#23
Rubentxu wants to merge 3 commits into
mainfrom
feat/graph-wasm-shim

Conversation

@Rubentxu

Copy link
Copy Markdown
Owner

Summary

Create cognicode-graph-wasm — a thin wasm-bindgen shim that wraps cognicode-graph-algos pure functions (pagerank, god_nodes) for browser consumption.

Scope

Files created:

  • crates/cognicode-graph-wasm/Cargo.toml
  • crates/cognicode-graph-wasm/src/lib.rs — 2 wasm-bindgen exports
  • crates/cognicode-graph-wasm/src/protocol.rs — serde request/response types
  • crates/cognicode-graph-wasm/src/wasm_bind.rs — JsValue<->Rust translation (wasm32)
  • crates/cognicode-graph-wasm/tests/wire_format.rs — 9 wire format tests

Files modified:

  • Cargo.toml — added wasm-bindgen 0.2, serde-wasm-bindgen 0.6, cognicode-graph-wasm member

Key Design Decisions

  1. Single-source rule maintained: pagerank and god_nodes are delegates to cognicode_graph_algos::{page_rank, god_nodes}. No algorithm logic in this crate (ADR-047 §1 + ADR-048).
  2. Reuses JsonGraph adapter from cognicode-graph-algos (single-source, not a duplicate implementation).
  3. wasm32-gated deps: wasm-bindgen, serde-wasm-bindgen only compiled for wasm32 target; native builds stay clean.
  4. serde as unconditional dep: needed by protocol types on both targets (tests + wasm32).

Validation

Check Result
cargo check -p cognicode-graph-wasm (native)
cargo test -p cognicode-graph-wasm ✅ 9 passed
cargo check --target wasm32-unknown-unknown
wasm-pack build --target web --release ✅ (106KB .wasm)
cargo fmt --check
cargo clippy -p cognicode-graph-wasm -p cognicode-graph-algos

wasm-pack output

pkg/
├── cognicode_graph_wasm_bg.wasm  106KB
├── cognicode_graph_wasm.d.ts     TypeScript types
├── cognicode_graph_wasm.js       JS bindings
└── package.json

PR chain

Merge

Admin bypass — CI workflows removed per user decision in PR #21.

Rubentxu and others added 3 commits June 24, 2026 16:42
Add the wasm-bindgen shim crate to the workspace. This crate
exposes PageRank and god_nodes from cognicode-graph-algos to
JavaScript via wasm-bindgen.

- Added wasm-bindgen 0.2 and serde-wasm-bindgen 0.6 to
  workspace.dependencies (required for WASM target).
- Added cognicode-graph-wasm as workspace member and dependency.
protocol.rs defines the serde-serializable request/response types
shared between wasm32 and native targets:
- Graph (wraps JsonGraph from cognicode-graph-algos)
- PageRankOptions / PageRankOutput
- GodNodesOptions / GodNodesOutput / GodNodeEntry

The Graph wrapper delegates to the existing JsonGraph adapter
in cognicode-graph-algos (single-source rule, ADR-047/048).
lib.rs — thin translation layer that:
- Accepts JsValue from JavaScript (nodes, edges, options)
- Converts to Graph via wasm_bind::graph_from_js
- Calls cognicode_graph_algos::{page_rank, god_nodes}
- Translates usize-indexed results back to node-id keys
- Returns JsValue via wasm_bind::to_value

wasm_bind.rs — JsValue<->Rust conversion (wasm32 only):
- from_value, graph_from_js, to_value, *_from_js helpers
- Uses serde-wasm-bindgen for direct JsValue conversion

tests/wire_format.rs — 9 native tests covering:
- PageRankOutput round-trip (BTreeMap ordering)
- GodNodesOutput round-trip
- Default values for PageRankOptions and GodNodesOptions
- JsonNode label serialization

All wasm32-only deps (wasm-bindgen, serde-wasm-bindgen) are gated
behind target_arch=wasm32 to keep native builds clean.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants