Crates.io | into-jsvalue-derive |
lib.rs | into-jsvalue-derive |
version | 0.2.0 |
source | src |
created_at | 2024-06-26 04:13:13.946608 |
updated_at | 2024-07-08 19:32:16.931183 |
description | derive into-jsvalue for enum return values in async wasm_bindgen functions |
homepage | |
repository | https://github.com/samankittani/into-jsvalue-derive |
max_upload_size | |
id | 1284150 |
size | 6,224 |
This crate provides a derive macro IntoJsValue
for Rust that simplifies the implementation of From<T> for wasm_bindgen::JsValue
.
This crate is for a particular use case.
wasm_bindgen
only supports c-style enums.
tsify
allows us to get around this using serde-wasm-bindgen, but it only works for synchronous functions
This crate adds a simple derive macro that will implement the neccassary From
implementation
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
use into_jsvalue_derive::IntoJsValue;
use tsify::Tsify;
#[wasm_bindgen]
pub async fn test() -> Test { // this works because of the tsify macro above our enum
Test::One(String::new())
}
#[wasm_bindgen]
pub async fn async_test() -> Test { // this does not work. It need From<Test> for JsValue to be implemented
Test::One(String::new())
}
#[derive(Serialize, Deserialize, Tsify)]
#[wasm_bindgen] // This does not work because wasm_bindgen only supports c-style enums
#[tsify(into_wasm_abi, from_wasm_abi)] // this allows test() to work, but not async test. It does not impl the required trait
pub enum Test {
One(String),
Two(String, String),
}
[dependencies]
into-jsvalue-derive = "0.1.0" # Replace with the actual version
# include serde, wasm-bindgen and serde-wasm-bindgen
#[derive(IntoJsValue)]
attribute on your struct definition:
Note:: the struct must impl serde::Serialize#[derive(Serialize, IntoJsValue)]
struct MyStruct {
field1: i32,
field2: String,
}
The macro will generate the following code.
// generated by the macro
impl From<MyStruct> for JsValue{
fn from(value: MyStruct) -> Self {
let res = serde_wasm_bindgen::to_value(&value);
wasm_bindgen::UnwrapThrowExt::expect_throw(
res,
format!("failed to convert MyStruct to a JsValue"),
)
}
}
This crate requires serde, wasm_bindgen, and serde_wasm_bindgen crates to be present in your project.
This project is licensed under the MIT License - see the LICENSE file for details.