本文代码
查看效果
依赖工具
cargo install wasm-pack
Cargo.toml
[package]
name = "wasm-bindgen-record"
version = "0.1.0"
authors = ["Li Lei <this.lilei@gmail.com>"]
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
anyhow = "1.0.56"
wasm-bindgen = "0.2.79"
serde_json = "1.0.79"
src/lib.rs
use anyhow::Result;
use serde_json::{
from_str, to_string, to_string_pretty, Value};
use wasm_bindgen::prelude::wasm_bindgen;
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[wasm_bindgen]
pub fn expand(s: &str) -> String {
expand_json(s).unwrap()
}
#[wasm_bindgen]
pub fn collapse