Setup for TypeScript type gen for WASM using Tsify

- Includes breaking changes to the WASM API surface, since it now accepts objects instead of stringified JSON.
This commit is contained in:
Brandon Vandegrift
2023-08-16 10:25:09 -04:00
parent 65826b219b
commit 1b20037053
22 changed files with 323 additions and 109 deletions

14
tsify-async/Cargo.toml Normal file
View File

@@ -0,0 +1,14 @@
[package]
name = "tsify-async"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
proc-macro = true
[dependencies]
syn = "1.0"
quote = "1.0"
serde-wasm-bindgen = "0.5.0"

30
tsify-async/src/lib.rs Normal file
View File

@@ -0,0 +1,30 @@
// Copied from https://github.com/madonoharu/tsify/issues/24#issuecomment-1670228789
// TODO: I think this had more to do with:
// the trait `From<veilid_core::VeilidUpdate>` is not implemented for `wasm_bindgen::JsValue`
// than it does with tsify itself... Maybe rename to something else?
use proc_macro::TokenStream;
use quote::quote;
use syn;
#[proc_macro_derive(TsifyAsync)]
pub fn tsify_async_macro_derive(input: TokenStream) -> TokenStream {
// Construct a representation of Rust code as a syntax tree
// that we can manipulate
let ast = syn::parse(input).unwrap();
// Build the trait implementation
impl_tsify_async_macro(&ast)
}
fn impl_tsify_async_macro(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
impl From<#name> for JsValue {
fn from(value: #name) -> Self {
serde_wasm_bindgen::to_value(&value).unwrap()
}
}
};
gen.into()
}