Clone in a terminal:
Commit 0bc8a2b
Johan Mattsson - 2026-06-01T18:02:59+00:00
0bc8a2bf94e8b8757876c0f66d39f83660e3f6dfjohan.mattsson.m@gmail.com

Use serde for all types

Changed files (1)

Diff

diff --git a/src/lib.rs b/src/lib.rsindex 1fd1a89..61944d8 100644--- a/src/lib.rs+++ b/src/lib.rs@@ -117,89 +117,17 @@ fn generate_callback_closure(             .zip(rs_arg_idents.iter())             .zip(js_arg_idents.iter().zip(rust_arg_types.iter()))         {-            // attempt simple conversions for common primitive types to avoid requiring-            // serde_wasm_bindgen in the consumer crate. Fall back to serde for other types.-            let conv = if let syn::Type::Path(type_path) = ty {-                if let Some(seg) = type_path.path.segments.last() {-                    let ident_str = seg.ident.to_string();-                    match ident_str.as_str() {-                        "u32" | "i32" | "usize" | "f64" => {-                            quote! {-                                let #res_ident: Option<#ty> = #val_ident.as_f64().map(|v| v as #ty);-                                if #res_ident.is_none() {-                                    gloo::console::log!(-                                        concat!("Callback error: ", stringify!(#fn_name), ". Wrong argument")-                                    );-                                    return;-                                }-                                let #rs_ident = #res_ident.unwrap();-                            }-                        }-                        "bool" => {-                            quote! {-                                let #res_ident: Option<#ty> = #val_ident.as_bool();-                                if #res_ident.is_none() {-                                    gloo::console::log!(-                                        concat!("Callback error: ", stringify!(#fn_name), ". Wrong argument")-                                    );-                                    return;-                                }-                                let #rs_ident = #res_ident.unwrap();-                            }-                        }-                        "String" => {-                            quote! {-                                let #res_ident: Option<#ty> = #val_ident.as_string();-                                if #res_ident.is_none() {-                                    gloo::console::log!(-                                        concat!("Callback error: ", stringify!(#fn_name), ". Wrong argument")-                                    );-                                    return;-                                }-                                let #rs_ident = #res_ident.unwrap();-                            }-                        }-                        _ => {-                            quote! {-                                let #res_ident = serde_wasm_bindgen::from_value::<#ty>(#val_ident.clone());-                                if #res_ident.is_err() {-                                    gloo::console::log!(-                                        concat!("Callback error: ", stringify!(#fn_name), ". Wrong argument"),-                                        #res_ident.err().unwrap()-                                    );-                                    return;-                                }-                                let #rs_ident = #res_ident.unwrap();-                            }-                        }-                    }-                } else {-                    // fallback-                    quote! {-                        let #res_ident = serde_wasm_bindgen::from_value::<#ty>(#val_ident.clone());-                        if #res_ident.is_err() {-                            gloo::console::log!(-                                concat!("Callback error: ", stringify!(#fn_name), ". Wrong argument"),-                                #res_ident.err().unwrap()-                            );-                            return;-                        }-                        let #rs_ident = #res_ident.unwrap();-                    }-                }-            } else {-                // not a path type; fallback to serde-                quote! {-                    let #res_ident = serde_wasm_bindgen::from_value::<#ty>(#val_ident.clone());-                    if #res_ident.is_err() {-                        gloo::console::log!(-                            concat!("Callback error: ", stringify!(#fn_name), ". Wrong argument"),-                            #res_ident.err().unwrap()-                        );-                        return;-                    }-                    let #rs_ident = #res_ident.unwrap();+            // always use serde_wasm_bindgen::from_value for all types+            let conv = quote! {+                let #res_ident = serde_wasm_bindgen::from_value::<#ty>(#val_ident.clone());+                if #res_ident.is_err() {+                    gloo::console::log!(+                        concat!("Callback error: ", stringify!(#fn_name), ". Wrong argument"),+                        #res_ident.err().unwrap()+                    );+                    return;                 }+                let #rs_ident = #res_ident.unwrap();             };              conversions.push(conv);

Contact

Email: johan.mattsson.m@gmail.com
Mastodon: typo.social/@birdfont
GitHub: johanmattssonm
LinkedIn: johan-mattsson-2ab324112