Using wasm_bindgen
with serde
, I'm trying to return a JsValue
for a complex structure using idiomatic rust. I've created an isolated example to illustrate the error that I'm seeing.
struct declaration:
#[derive(Serialize)]
pub struct BookStoreData {
pub h: HashMap<String, String>,
pub name: String,
}
function definition:
#[wasm_bindgen]
pub fn hello_hash(count: i32) -> Result<JsValue, JsValue> {
set_panic_hook();
let mut book_reviews = HashMap::new();
book_reviews.insert(
"Grimms' Fairy Tales".to_string(),
"Masterpiece.".to_string(),
);
let data = BookStoreData {
h: book_reviews,
name: "My Book Store".to_string(),
};
let js_result: JsValue = JsValue::from_serde(&data).unwrap();
OK(js_result)
}
I get this compilation error:
error[E0425]: cannot find function `OK` in this scope
--> src/hello_whatever.rs:46:5
|
46 | OK(js_result)
| ^^ help: a tuple variant with a similar name exists: `Ok`
You can see thefull example based on rust-parcel-template
To reproduce the the error, from the root of the repo, run npm run start
or cd crate && cargo build
Ok
instead ofOK
smallk