chain-module-sdk-rust/README.md
flemming-it 4b4de3374c
Some checks failed
CI / Linux x86_64 (Forgejo) (push) Failing after 2s
feat(sdk): rename attribute to #[chain_module], keep #[fai_module] alias
The public macro name was the last product-scoped 'fai' identifier
in the SDK surface. #[chain_module] is now the primary attribute
(README + examples updated); #[fai_module] stays as a delegating
alias so modules written before the rename keep compiling. 0.2.1.

Signed-off-by: flemming-it <sf@flemming.it>
2026-07-11 19:02:40 +02:00

60 lines
2.2 KiB
Markdown

# chain-module-sdk
Stable, ergonomic Rust surface for writing Ch∆In modules.
A module written against this crate looks like:
```rust
use chain_module_sdk::prelude::*;
#[chain_module]
fn invoke(_ctx: Context, inputs: Inputs) -> Result<Outputs, ModuleError> {
let text = inputs.require_text("input")?;
Ok(Outputs::new().with_text("output", format!("echo: {text}")))
}
```
That single function — plus a `Cargo.toml` listing
`chain-module-sdk` as a dependency — is the entire module. The
`#[chain_module]` macro generates the `wit_bindgen` invocation, the
`Guest` implementation, the type conversions, and the
`wit_bindgen::export!` glue behind the scenes.
## What the SDK gives you
| You get | Instead of |
|---------|-----------|
| `Inputs::require_text("name")` | manual `match payload { Payload::Text(s) => ..., _ => ... }` |
| `Outputs::new().with_json(...)?` | building `Vec<(String, Payload)>` by hand |
| `ModuleError::invalid_input("...")` | constructing WIT `invocation-error` variants |
| `ctx.emit("progress", ...)` | hand-rolling the `host.emit-event` import (WIT 1.1) |
| `#[chain_module]` | `wit_bindgen::generate!` + `Guest` impl + `export!` + `#![allow(unsafe_op_in_unsafe_fn)]` |
## Stability
The SDK insulates module code from the underlying WIT contract
(`chain:platform`, currently v1.1 — frozen base v1.0 plus the
additive 1.1 `emit-event` import). The WIT may evolve in
additive minor versions or, eventually, a coordinated v2.0; the
SDK absorbs that change so existing module code keeps compiling.
The SDK ships its own copy of `wit/world.wit` so that the
proc-macro can embed the contract via `include_str!` at compile
time. A snapshot test (`crates/chain-module-sdk/tests/wit_freeze.rs`)
asserts the SHA-256 matches the platform's frozen hash — if the
two ever drift, CI fails.
## Versioning
| Track | Stability |
|-------|-----------|
| WIT contract (`chain:platform`) | v1.1; base frozen at v1.0, minors are additive |
| `chain-module-sdk` Rust API | Semver; v0.x while ergonomics evolve |
| Per-module versions | Each module repo's own concern |
## License
Apache-2.0. See `LICENSE`.
Author: Dr. Stefan Flemming, Flemming.AI <chain@flemming.ai>
Repository: https://git.flemming.ai/fai/chain-module-sdk-rust