MCPcopy Index your code
hub / github.com/bnjjj/witgen

github.com/bnjjj/witgen @0.10.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.10.0 ↗ · + Follow
50 symbols 67 edges 10 files 8 documented · 16% updated 3y ago★ 447 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

witgen

Rust Rust Version Docs.rs

witgen is a library and a CLI that helps you generate wit definitions in a wit file for WebAssembly. Using this lib in addition to wit-bindgen will help you to import/export types and functions from/to wasm module.

Getting started

Goal: Generate a .wit file writing only Rust.

You will need both the library and the CLI.

Preliminaries

  • Create a new library project and move to it.
$ cargo new my_wit
$ cd my_wit
  • Add witgen as a dependency in your Cargo.toml. Note: must have cargo-edit installed to add dependencies from CLI, e.g. cargo install cargo-edit.
$ cargo add witgen
  • Install cargo witgen CLI.
$ cargo install cargo-witgen

Writing code

  • Replace the content of your lib.rs by:
use witgen::witgen;

#[witgen]
struct TestStruct {
    inner: String,
}

#[witgen]
enum TestEnum {
    Unit,
    Number(u64),
    StringVariant(String),
}

#[witgen]
fn test(other: Vec<u8>, test_struct: TestStruct, other_enum: TestEnum) -> Result<(String, i64), String> {
    // The following code is not part of the generated `.wit` file.
    // You may add an example implementation or just satisfy the compiler with a `todo!()`.
    Ok((String::from("test"), 0i64))
}
  • Then you can launch the CLI (at the root of your package):
$ cargo witgen generate
  • It will generate a witgen.wit file at the root of your package:
record test-struct {
    inner: string
}

variant test-enum {
    unit,
    number(u64),
    string-variant(string),
}

test : function(other: list <u8>, test-struct: test-struct, other-enum: test-enum) -> expected<tuple<string, s64>>
  • You can find more complete examples here

Limitations

For now using #[witgen] have some limitations:

  • You can use the proc macro #[witgen] only on struct, enum, type alias, function
  • Generic parameters or lifetime anotations are not supported
  • Type &str is not supported (but you can use String)
  • Named struct variants in enum are not already supported (examples enum Test { NamedVariant: { inner: String } } but this one is supported enum Test { UnNamedVariant(String, usize) })
  • References, Box, Rc, Arc and all types of smart pointers are not supported
  • Methods are not supported
  • There is no semantic analysis, which means if your function, struct or enum uses a non scalar type, you have to add #[witgen] where this type is declared (it won't fail at the compile time)

Development

It's a very minimal version, it doesn't already support all kinds of types but the main ones are supported. I made it to easily generate .wit files for my need. Feel free to create issues or pull-requests if you need something. I will be happy to help you!

Extension points exported contracts — how you extend this code

ToWitType (Interface)
(no doc) [1 implementers]
crates/witgen_macro_helper/src/lib.rs

Core symbols most depended-on inside this repo

is_known_keyword
called by 8
crates/witgen_macro_helper/src/lib.rs
get_doc_comment
called by 7
crates/witgen_macro_helper/src/generator.rs
to_wit
called by 5
crates/witgen_macro_helper/src/lib.rs
run_generate
called by 1
crates/cargo_witgen/src/main.rs
get_or_init_target_dir
called by 1
crates/witgen_macro_helper/src/lib.rs
hash_string
called by 1
crates/witgen_macro_helper/src/lib.rs
write_to_file
called by 1
crates/witgen_macro_helper/src/lib.rs
main
called by 0
tests/files/success.rs

Shape

Function 30
Class 11
Enum 7
Interface 1
Method 1

Languages

Rust100%

Modules by API surface

examples/my_witgen_example/src/lib.rs16 symbols
tests/files/success.rs14 symbols
crates/witgen_macro_helper/src/lib.rs7 symbols
crates/witgen_macro_helper/src/generator.rs6 symbols
crates/cargo_witgen/src/main.rs3 symbols
crates/cargo_witgen/src/opt.rs2 symbols
tests/test.rs1 symbols
crates/witgen_macro/src/lib.rs1 symbols

For agents

$ claude mcp add witgen \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page