Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ jobs:
- uses: dtolnay/rust-toolchain@5b842231ba77f5c045dba54ac5560fed2db780e2 # nightly
with:
toolchain: nightly
components: rustfmt

- uses: Swatinem/rust-cache@e18b497796c12c097a38f9edb9d0641fb99eee32 # v2

Expand All @@ -89,6 +90,12 @@ jobs:
- name: Run codegen
run: ./scripts/codegen.sh

- name: Check generated Rust output is committed
run: git diff --exit-code -- rust/crates/truapi-server/src/generated

- name: Check Rust/TS wire table parity
run: TRUAPI_REQUIRE_GENERATED_TS=1 cargo test -p truapi-server --test wire_table_ts_parity

- name: Upload codegen output
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/deploy-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
- uses: dtolnay/rust-toolchain@5b842231ba77f5c045dba54ac5560fed2db780e2 # nightly
with:
toolchain: nightly
components: rustfmt

- name: Install workspace dependencies
run: npm ci
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/deploy-playground.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
- uses: dtolnay/rust-toolchain@5b842231ba77f5c045dba54ac5560fed2db780e2 # nightly
with:
toolchain: nightly
components: rustfmt

- uses: Swatinem/rust-cache@e18b497796c12c097a38f9edb9d0641fb99eee32 # v2

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ jobs:
uses: dtolnay/rust-toolchain@5b842231ba77f5c045dba54ac5560fed2db780e2 # nightly
with:
toolchain: nightly
components: rustfmt

- if: steps.version.outputs.proceed == 'true'
uses: dtolnay/rust-toolchain@5b842231ba77f5c045dba54ac5560fed2db780e2 # stable
Expand Down
8 changes: 5 additions & 3 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ rust/crates/
truapi/ Rust trait + type definitions for protocol versions v0.1 and v0.2
truapi-codegen/ rustdoc JSON → TypeScript client + Rust dispatcher
truapi-macros/ #[wire(id = N)] proc-macro
truapi-server/ host runtime: frames, dispatcher, subscriptions
js/packages/
truapi/ @parity/truapi TS package; generated TS lives under ignored paths
playground/ Next.js interactive playground; deploys to truapi-playground.dot
hosts/dotli/ dotli submodule
docs/ design docs, RFCs, feature proposals
scripts/codegen.sh regenerate the TS client from the Rust crate
scripts/codegen.sh regenerate TS client + Rust runtime tables from the Rust crate
```

## Code style
Expand Down Expand Up @@ -45,7 +46,7 @@ git submodule update --init --recursive
( cd playground && yarn install --frozen-lockfile )
```

## Regenerating the TS client
## Regenerating generated sources

When the Rust trait surface changes, rerun:

Expand All @@ -54,7 +55,8 @@ When the Rust trait surface changes, rerun:
```

That will repopulate the ignored generated TS under `js/packages/truapi/src/generated/`,
`js/packages/truapi/src/playground/codegen/`, and `js/packages/truapi/test/generated/examples/`.
`js/packages/truapi/src/playground/codegen/`, `js/packages/truapi/test/generated/examples/`,
and the checked-in Rust dispatcher under `rust/crates/truapi-server/src/generated/`.
After regenerating, rebuild the client and refresh the playground's link copy:

```bash
Expand Down
99 changes: 99 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ build: ## Build the Rust workspace and the TypeScript client.
cargo build --workspace
cd $(TRUAPI_PKG) && npm run build

codegen: ## Regenerate the TypeScript client from the Rust crate.
codegen: ## Regenerate the TypeScript client and Rust runtime tables from the Rust crate.
./scripts/codegen.sh
cd $(PLAYGROUND) && rm -rf node_modules/@parity && yarn install

Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,19 @@ rust/crates/
truapi/ Rust trait and type definitions (v01, v02)
truapi-codegen/ rustdoc JSON to TypeScript client + Rust dispatcher
truapi-macros/ #[wire(id = N)] proc-macro
truapi-server/ host runtime: frames, dispatcher, subscriptions
js/packages/
truapi/ @parity/truapi TypeScript client
playground/ Interactive Next.js playground (truapi-playground.dot)
hosts/dotli/ dotli host, vendored as a submodule
docs/ Design docs, RFCs, feature proposals
scripts/codegen.sh Regenerate the TS client from the Rust source
scripts/codegen.sh Regenerate TS client + Rust runtime tables from Rust source
```

## How it works

1. The protocol is defined as Rust traits in [`rust/crates/truapi/`](rust/crates/truapi/), with each method tagged `#[wire(id = N)]` for a stable byte-level dispatch table. Every method's doc comment must carry a ` ```ts ` example, which codegen extracts into the playground's EXAMPLE tab; the build fails if any method is missing one.
2. `truapi-codegen` reads rustdoc JSON for that crate and generates the TypeScript client under git-ignored paths in `js/packages/truapi/`.
2. `truapi-codegen` reads rustdoc JSON for that crate and generates the TypeScript client under git-ignored paths in `js/packages/truapi/`, plus the Rust dispatcher and wire table consumed by `truapi-server`.
3. Higher-level SDKs wrap the typed client; the transport encodes SCALE frames and ships them over `MessagePort` (or `postMessage` in iframe mode) to the host.
4. The host decodes the frame, dispatches to the matching trait method, encodes the response, and ships it back.

Expand All @@ -94,16 +95,18 @@ yarn dev

Open `https://dot.li/localhost:3000` inside the Polkadot Desktop Host. See [`playground/README.md`](playground/README.md) for deployment.

## Regenerate the TypeScript client
## Regenerate generated sources

When the Rust trait surface changes:

```bash
make codegen # regenerate the TS client and refresh the playground snapshot
make codegen # regenerate the TS client, Rust runtime tables, and playground snapshot
make playground # rebuild the playground against the refreshed snapshot
```

This repopulates the ignored generated TS under `js/packages/truapi/`, including the playground metadata.
This repopulates the ignored generated TS under `js/packages/truapi/`, including
the playground metadata, and refreshes the checked-in Rust dispatcher and wire
table under `rust/crates/truapi-server/src/generated/`.

## Protocol versions

Expand All @@ -129,4 +132,3 @@ See [`CONTRIBUTING.md`](CONTRIBUTING.md) for issue reports, feature proposals, a
## License

[MIT](./LICENSE)

3 changes: 3 additions & 0 deletions rust/crates/truapi-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ anyhow = "1"
clap = { version = "4", features = ["derive"] }
indoc = "2"
convert_case = "0.6"

[dev-dependencies]
tempfile = "3"
13 changes: 8 additions & 5 deletions rust/crates/truapi-codegen/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# truapi-codegen

_Reads rustdoc JSON for the `truapi` crate and generates the TypeScript client._
_Reads rustdoc JSON for the `truapi` crate and generates client and runtime code._

[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](../../../LICENSE)

`truapi-codegen` keeps the generated client aligned with the Rust protocol definition. It reads rustdoc JSON, extracts the TrUAPI API surface, and writes:
`truapi-codegen` keeps generated code aligned with the Rust protocol definition. It reads rustdoc JSON, extracts the TrUAPI API surface, and writes:

- TypeScript types for every protocol type in `truapi`.
- TypeScript domain client classes for every unified trait.
- The TypeScript wire dispatch table.
- The Rust host dispatcher and wire dispatch table consumed by `truapi-server`.

## Generated output

Expand Down Expand Up @@ -50,7 +51,7 @@ The generator runs in three stages:

1. **Parse**: read JSON emitted by nightly rustdoc.
2. **Normalize**: extract the API model, including each method's `#[wire(id = N)]`.
3. **Emit**: generators write TypeScript output.
3. **Emit**: generators write TypeScript client output and, when requested, Rust runtime output.

Missing or duplicate wire ids fail generation. Subscription methods reserve four consecutive ids for `_start`, `_stop`, `_interrupt`, and `_receive`.

Expand All @@ -60,7 +61,8 @@ Missing or duplicate wire ids fail generation. Subscription methods reserve four
cargo run -p truapi-codegen -- \
--input target/doc/truapi.json \
--output js/packages/truapi/src/generated \
--version V2 \
--rust-output rust/crates/truapi-server/src/generated \
--client-version V2 \
--codec-version 1
```

Expand All @@ -71,7 +73,8 @@ cargo +nightly rustdoc -p truapi -- -Z unstable-options --output-format json
cargo run -p truapi-codegen -- \
--input target/doc/truapi.json \
--output js/packages/truapi/src/generated \
--version V2 \
--rust-output rust/crates/truapi-server/src/generated \
--client-version V2 \
--codec-version 1
```

Expand Down
Loading