Skip to content

Clipi-12/rust_xlsx_wasm_example

Repository files navigation

Limitations

  • This is supposed to be a minimal example of using rust_xlsxwriter, but it is quite a large project to be considered minimal. This will be difficult to fix, though, since most files are present to replace unimplemented-std-methods with browser/javascript-vm functions.
  • Node and Deno crash javascript-side whenever an io error occurs (instead of returning a Result that we could catch in Rust).

Setup

This repo uses cargo-make to manage all the different compilation targets. It is by no means neccessary to create a WASM application, but using it reduces complexity, since most targets are built by repeating similar commands.

All rules can be found in Makefile.toml, and each contains a profile called release that selects the rustc compilation flag and calls wasm-opt when enabled.
Remember that profiles in cargo-make must be selected before the rule
(e.g. cargo make -p release all instead of cargo make all -p release).

cargo install cargo-make

Dependencies needed to build against a browser, Node.js, or Deno

rustup target add wasm32-unknown-unknown
cargo install wasm-bindgen-cli wasm-opt

Warning

The wasm-bindgen command may fail with a linkage error if the wasm-bindgen-cli cargo-installed-package doesn't match the wasm-bindgen cargo-added-package. If this happens to you, just cargo update or read the error thrown in stderr.

Dependencies needed to build against WASI

rustup target add wasm32-wasip1

You can also use the experimental wasm32-wasip1-threads target, or the stabilized-yet-experimental wasm32-wasip2 target. Both of them implement the std fully, meaning they can use std::thread::spawn, which wasm32-wasip1 can't.
If you end up choosing one of the above instead of wasm32-wasip1, change the variable WASM_WASI in Makefile.toml accordingly. Also, since they are experimental, you may need to use nightly instead of stable, so change the value in rust-toolchain.toml accordingly.

rustup target add wasm32-wasip1-threads
rustup target add wasm32-wasip2

Building and Running

Running in browser

  1. cargo make browser
  2. Open a localhost server in pkg/browser. For example, with npm i -g http-server then
    http-server -c 1 -p 8080 pkg/browser
  3. Open a browser and go to http://localhost:8080/index.html

Running in Node.js

cargo make nodejs
node pkg/nodejs/rust_xlsx_wasm_example.js

Running in deno

cargo make deno
deno run --allow-write --allow-read pkg/deno/rust_xlsx_wasm_example.js

Running natively in WASI

In order to run the .wasm file, you will need to have Wasmtime installed.
--dir=. allows WASM to preopen the current directory (essentially giving it access to it, which it normally can't since WASM is sandboxed).

wasm32-wasip1 target triple
cargo make wasi
wasmtime --dir=. pkg/wasi/rust_xlsx_wasm_example.wasm
wasm32-wasip1-threads target triple
cargo make wasi
wasmtime --wasi threads --dir=. pkg/wasi/rust_xlsx_wasm_example.wasm

Utilities

To build all targets:

cargo make all

To run both cargo clean and rm -rf pkg (but platform independent):

cargo make clean

About

Minimal example of using rust_xlsxwriter in Wasm. See https://github.com/jmcnamara/rust_xlsxwriter/issues/38

Resources

License

LGPL-3.0, GPL-3.0 licenses found

Licenses found

LGPL-3.0
COPYING.LESSER
GPL-3.0
COPYING

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published