mitsuhiko / similar

A high level diffing library for rust based on diffs
https://insta.rs/similar
Apache License 2.0
974 stars 32 forks source link

WebAssembly Bindings #42

Open Michael-F-Bryan opened 2 years ago

Michael-F-Bryan commented 2 years ago

This PR fixes #39 by introducing a wasm crate which exposes a simplified version of similar's API to WebAssembly. The simplified API is defined in wasm/similar.wit and can be used by wit-bindgen to generate bindings for various other languages.

The code is fairly straightforward, although we needed to pull in ouroboros so the text being diffed and its results could be stored in the same 'static struct (TextDiff).

For now, I've published the package to wapm.dev under Michael-F-Bryan/similar, but that can be changed by updating the namespace = "..." bit in wasm/Cargo.toml.

I also added a CI job to automatically publish a new version to WAPM whenever a tagged commit is pushed to this repo. It might be useful if forgetting to publish to WAPM is a concern, otherwise we can easily remove .github/workflows/releases.yml.

mitsuhiko commented 1 year ago

Thank you! I will have a look at this over the weekend. The package itself and the code changes are fine, I'm super unsure about how to best publish this. I think if this ends up in the package I might as well bite the bullet and publish this to wapm.

Michael-F-Bryan commented 1 year ago

I'm super unsure about how to best publish this

The easiest way to publish Rust packages to WAPM is with the cargo wapm subcommand. It's a thin wrapper around the wapm CLI which uses information from your Cargo.toml to figure out how to build your crate and generate the wapm.toml file.

Otherwise, you can publish to WAPM directly by following these instructions.

I also wrote up an explanatory blog post about how publishing to WAPM via the wapm CLI and cargo wapm works.