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

bash $ cargo new my_wit $ cd my_wit

bash $ cargo add witgen

bash $ cargo install cargo-witgen

Writing code

```rust use witgen::witgen;

[witgen]

struct TestStruct { inner: String, }

[witgen]

enum TestEnum { Unit, Number(u64), String(String), }

[witgen]

fn test(other: Vec, teststruct: TestStruct, otherenum: 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)) } ```

bash $ cargo witgen generate

```wit record TestStruct { inner: string }

variant TestEnum { Unit, Number(u64), String(string), }

test : function(other: list , teststruct: TestStruct, otherenum: TestEnum) -> expected> ```

Limitations

For now using #[witgen] have some limitations:

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!