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.
Goal: Generate a .wit
file writing only Rust.
You will need both the library and the CLI.
bash
$ cargo new my_wit
$ cd my_wit
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
.bash
$ cargo add witgen
cargo witgen
CLI.bash
$ cargo install cargo-witgen
lib.rs
by:```rust use witgen::witgen;
struct TestStruct { inner: String, }
enum TestEnum { Unit, Number(u64), StringVariant(String), }
fn test(other: Vec.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
witgen.wit
file at the root of your package:```wit record test-struct { inner: string }
variant test-enum { unit, number(u64), string-variant(string), }
test : function(other: list
For now using #[witgen]
have some limitations:
#[witgen]
only on struct
, enum
, type alias
, function
&str
is not supported (but you can use String
)enum
are not already supported (examples enum Test { NamedVariant: { inner: String } }
but this one is supported enum Test { UnNamedVariant(String, usize) }
)Box
, Rc
, Arc
and all types of smart pointers are not supportedfunction
, 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)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!