Structs in wasm_bindgen are handled via serde, so they have to be serializable/deserializable. I also don't think that you can pass JavaScript objects (web-sys types) through that.
If you really want to do this, you have to take a JsValue and convert it to whatever you want yourself using the reflection API.
Also note that wasm_bindgen currently can't handle optional incoming parameters. If you compile in development mode, it adds a parameter count check that will fail. In release mode, it should work (since the type checks are not included then) and you should get JsValue::UNDEFINED.
Thank you so much! That worked great. It looks like they changed the behavior because if nothing is passed to rust, it no longer throws an error but you can check for undefined.
# [wasm_bindgen]
pub fn greet(ele: &JsValue, options: &JsValue) -> Result<(), JsValue> {
match ele.dyn_ref::<HtmlDivElement>() {
Some(div) => {
if (options.is_undefined()) {
div.set_inner_text("Hello from Rust");
} else {
let message: JsValue = Reflect::get(&options, &JsValue::from_str("message"))?;
let message = message.as_string().unwrap();
div.set_inner_text(&message);
}
Ok(())
}
None => Err(JsValue::from_str("ele must be a div"))
}
}
Also, it's possible I'm not in development mode as I'm not sure how to set that or check it. I'm just using the example wasm_bindgen template in the book and using npm run serve