{"id":15908132,"url":"https://github.com/bnjjj/witgen","last_synced_at":"2025-03-22T00:31:30.950Z","repository":{"id":37924015,"uuid":"435640015","full_name":"bnjjj/witgen","owner":"bnjjj","description":"witgen is a library to generate .wit files for WebAssembly in Rust","archived":false,"fork":false,"pushed_at":"2023-03-13T20:14:29.000Z","size":289,"stargazers_count":41,"open_issues_count":8,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-18T06:34:55.104Z","etag":null,"topics":["cargo-subcommand","rust-lang","wasm","wasm-bindgen","webassembly"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bnjjj.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-06T20:36:47.000Z","updated_at":"2025-03-14T23:11:09.000Z","dependencies_parsed_at":"2024-10-28T11:49:57.906Z","dependency_job_id":"400ceeb1-0581-476d-b893-a9491edc804a","html_url":"https://github.com/bnjjj/witgen","commit_stats":{"total_commits":57,"total_committers":4,"mean_commits":14.25,"dds":"0.49122807017543857","last_synced_commit":"26ea584eb52b394dd4bfe47a0f77c9ee35bd93c8"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bnjjj%2Fwitgen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bnjjj%2Fwitgen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bnjjj%2Fwitgen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bnjjj%2Fwitgen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bnjjj","download_url":"https://codeload.github.com/bnjjj/witgen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244890102,"owners_count":20527030,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cargo-subcommand","rust-lang","wasm","wasm-bindgen","webassembly"],"created_at":"2024-10-06T14:10:25.420Z","updated_at":"2025-03-22T00:31:30.650Z","avatar_url":"https://github.com/bnjjj.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# witgen\n\n![Rust](https://img.shields.io/badge/rust-stable-brightgreen.svg)\n![Rust](https://github.com/bnjjj/witgen/workflows/Rust/badge.svg)\n[![Version](https://img.shields.io/crates/v/witgen.svg)](https://crates.io/crates/witgen)\n[![Docs.rs](https://docs.rs/witgen/badge.svg)](https://docs.rs/witgen)\n\nwitgen is a library and a CLI that helps you generate [wit definitions](https://github.com/bytecodealliance/wit-bindgen/blob/main/WIT.md) in a wit file for WebAssembly. Using this lib in addition to [wit-bindgen](https://github.com/bytecodealliance/wit-bindgen) will help you to import/export types and functions from/to wasm module. This project currently supports the wit parser at version `0.2.0` see [here](https://github.com/ahalabs/wit-bindgen).\n\n## Getting started\n\n_Goal:_ Generate a `.wit` file writing only Rust.\n\nYou will need both the library and the CLI.\n\n### Preliminaries\n\n- Create a new library project and move to it.\n\n```bash\n$ cargo new my_wit\n$ cd my_wit\n```\n\n- 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`.\n\n```bash\n$ cargo add witgen\n```\n\n- Install `cargo witgen` CLI.\n\n```bash\n$ cargo install cargo-witgen\n```\n\n### Writing code\n\n- Replace the content of your `lib.rs` by:\n\n```rust,ignore\nuse witgen::witgen;\n\n#[witgen]\nuse other_crate::*;\n\n/// Doc strings are supported!\n#[witgen]\nstruct TestStruct {\n    /// Even for fields!\n    inner: String,\n}\n\n#[witgen]\nenum TestEnum {\n    Unit,\n    Number(u64),\n    StringVariant(String),\n}\n\n#[witgen]\nfn test(other: Vec\u003cu8\u003e, test_struct: TestStruct, other_enum: TestEnum) -\u003e Result\u003c(String, i64), String\u003e {\n    // The following code is not part of the generated `.wit` file.\n    // You may add an example implementation or just satisfy the compiler with a `todo!()`.\n    Ok((String::from(\"test\"), 0i64))\n}\n\n#[witgen]\nimpl AResource {\n  /// Can convert custom attributes to doc strings\n  #[custom_attribute]\n  pub fn foo(\u0026self) {}\n  /// Have special mutable attribute\n  pub fn faa(\u0026mut self) {}\n\n  pub fn fee() {}\n}\n```\n\n- Then you can launch the CLI (at the root of your package):\n\n```bash\ncargo witgen generate\n```\n\n- It will generate a `index.wit` file at the root of your package:\n\n```wit\n\nuse * from other-crate\n\n/// Doc strings are supported!\nrecord test-struct {\n  /// Even for fields!\n  inner: string\n}\n\nvariant test-enum {\n  unit,\n  number(u64),\n  string-variant(string),\n}\n\ntest : func(other: list \u003cu8\u003e, test-struct: test-struct, other-enum: test-enum) -\u003e expected\u003ctuple\u003cstring, s64\u003e\u003e\n\nresource a-resource {\n  /// Can convert custom attributes to doc strings\n  /// @custom_attribute\n  foo: func()\n  /// Have special mutable attribute\n  /// @mutable\n  faa: func()\n  static fee: func()\n}\n```\n\n- You can find more complete examples [here](./examples)\n\n## Limitations\n\nFor now using `#[witgen]` have some limitations:\n\n- You can use the proc macro `#[witgen]` only on `struct`, `enum`, `type alias`, `function`,  `impl`, and `use`\n- Generic parameters or lifetime annotations are not supported, except for `HashMap`, which is interpreted as `list\u003ctuple\u003ckey, value\u003e\u003e`.\n- Type `\u0026str` is not supported (but you can use `String`)\n- References, `Box`, `Rc`, `Arc` and all types of smart pointers are not supported\n- 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)\n\n## Development\n\nIt'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!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbnjjj%2Fwitgen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbnjjj%2Fwitgen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbnjjj%2Fwitgen/lists"}