witforge

WIT-first contract compiler for stable Rust, protobuf, JSON Schema, and versioned adapters

View the Project on GitHub asamarts/witforge

Wiring a Rust crate and installing witforge

This guide takes an empty Rust crate to a compiling plain-Rust ↔ protobuf round trip, explains how to install and verify prebuilt binaries, and covers the --lock, --released, and released configuration options.

Install and verify

From crates.io

cargo install witforge-cli --version 0.7.0 --locked
witforge --version

Prebuilt archives

Prebuilt archives are published on GitHub Releases for five native targets (each archive contains the executable beside LICENSE-APACHE, LICENSE-MIT, and README.md, plus a sibling .sha256 file and, from 0.7.0, a SLSA build provenance attestation):

Platform Target Notes
Linux x86_64 x86_64-unknown-linux-gnu glibc >= 2.39 (Ubuntu 24.04 runner)
Linux arm64 aarch64-unknown-linux-gnu glibc >= 2.39, published from 0.7.0
macOS Intel x86_64-apple-darwin —
macOS Apple silicon aarch64-apple-darwin —
Windows x86_64 x86_64-pc-windows-msvc —

Static Linux musl builds and Windows arm64 builds are not provided; install from crates.io with cargo install witforge-cli --locked on those targets. cargo binstall witforge-cli is also configured via [package.metadata.binstall].

Download and verify a prebuilt archive before extracting it:

VERSION=0.7.0
TARGET=x86_64-unknown-linux-gnu
BASE="https://github.com/asamarts/witforge/releases/download/v${VERSION}"
curl -sSfLO "${BASE}/witforge-${VERSION}-${TARGET}.tar.gz"
curl -sSfLO "${BASE}/witforge-${VERSION}-${TARGET}.tar.gz.sha256"
sha256sum -c "witforge-${VERSION}-${TARGET}.tar.gz.sha256"
# Releases 0.7.0 and later also carry a GitHub build provenance attestation:
gh attestation verify "witforge-${VERSION}-${TARGET}.tar.gz" \
  --repo asamarts/witforge \
  --signer-workflow asamarts/witforge/.github/workflows/release.yml
tar -xzf "witforge-${VERSION}-${TARGET}.tar.gz"
./witforge --version

On macOS, use shasum -a 256 -c if sha256sum is unavailable.

Layout: committed output vs. build.rs

Two layouts are possible:

  1. Commit witforge output under src/generated/ (recommended): witforge generate writes plain Rust, .proto, JSON Schema, and glue into committed directories, and build.rs only compiles the committed .proto files with prost-build (0.14) and protox (0.10, so no system protoc binary is required). CI runs witforge generate --check to catch drift, code review shows exact API changes in pull requests, and downstream crates build with plain cargo without installing witforge.
  2. Generate during build.rs: invokes witforge into OUT_DIR. This avoids committing generated files, but witforge --check cannot guard committed per-version modules in CI and every contributor needs the matching witforge binary during cargo build.

The walkthrough below uses the committed layout. Generated glue targets prost / prost-build 0.14 (and optionally pbjson / pbjson-build 0.9 for proto3 JSON).

Empty crate to compiling round trip

Create the crate layout:

my-contract-crate/
  Cargo.toml
  build.rs
  witforge.toml
  wit/
    v0.1/
      contract.wit
      wire.lock
  src/
    lib.rs
    main.rs
    generated/
      rust/
      proto/
      schema/
      glue/
mkdir -p my-contract-crate/wit/v0.1 my-contract-crate/src

Save my-contract-crate/Cargo.toml:

[package]
name = "my-contract-crate"
version = "0.1.0"
edition = "2024"

[dependencies]
prost = "0.14"

[build-dependencies]
prost-build = "0.14"
protox = "0.10"

Save my-contract-crate/wit/v0.1/contract.wit:

package example:greeter@0.1.0;

interface api {
    record greeting {
        text: string,
        repeat: option<u32>,
    }

    greet: func(name: string) -> greeting;
}

world hello {
    export api;
}

Save my-contract-crate/witforge.toml:

version = 1

[project.greeter]
root = "wit"

[project.greeter.rust]
out = "src/generated/rust"

[project.greeter.proto]
out = "src/generated/proto"

[project.greeter.schema]
out = "src/generated/schema"

[project.greeter.glue]
out = "src/generated/glue"
plain-root = "crate::plain"
proto-root = "crate::proto"

Lock the contract, validate it, and generate all four outputs:

witforge lock my-contract-crate/wit/v0.1 --first
witforge check my-contract-crate/wit
witforge generate --config my-contract-crate/witforge.toml
witforge generate --config my-contract-crate/witforge.toml --check

Save my-contract-crate/build.rs, which compiles the committed .proto schema using protox (pure Rust, no external protoc) and prost-build:

use std::path::PathBuf;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let proto_dir = PathBuf::from("src/generated/proto");
    println!("cargo:rerun-if-changed={}", proto_dir.display());
    let fds = protox::compile([proto_dir.join("v0.proto")], [&proto_dir])?;
    prost_build::Config::new().compile_fds(fds)?;
    Ok(())
}

Save my-contract-crate/src/lib.rs, exposing plain, proto, and glue at the exact module paths configured by plain-root = "crate::plain" and proto-root = "crate::proto":

#![deny(missing_docs)]

//! Contract crate wiring witforge's generated plain Rust, prost protobuf types, and glue.

/// Generated plain Rust contract types, traits, and worlds.
#[path = "generated/rust/mod.rs"]
pub mod plain;

/// Prost-generated protobuf messages compiled by `build.rs`.
pub mod proto {
    /// Wire schema for major version 0.
    #[allow(clippy::all, missing_docs)]
    pub mod v0 {
        include!(concat!(env!("OUT_DIR"), "/example.greeter.v0.rs"));
    }
}

/// Generated plain-Rust <-> prost protobuf glue.
#[path = "generated/glue/mod.rs"]
pub mod glue;

Save my-contract-crate/src/main.rs to round-trip a plain Rust value through generated glue and protobuf binary encoding:

use my_contract_crate::{glue, plain, proto};
use prost::Message as _;

fn main() {
    let original = plain::v0_1::api::Greeting {
        text: "hello".to_string(),
        repeat: Some(3),
    };
    let wire = glue::v0::api::greeting::to_proto(&original);
    let encoded = wire.encode_to_vec();
    let decoded_wire = proto::v0::ApiGreeting::decode(encoded.as_slice()).expect("decode proto");
    let roundtripped = glue::v0::api::greeting::from_proto(decoded_wire).expect("glue from_proto");
    assert_eq!(roundtripped, original);
}

Run cargo run inside my-contract-crate/ (the repository test suite builds and runs these exact files verbatim):

cargo run --manifest-path my-contract-crate/Cargo.toml

Module roots, dependencies, rustfmt, and lints

Released-directory options (--lock, --released, and released)