| Crates.io | bomboni_wasm_derive |
| lib.rs | bomboni_wasm_derive |
| version | 0.2.0 |
| created_at | 2023-12-28 04:43:55.45667+00 |
| updated_at | 2025-11-10 12:52:29.946279+00 |
| description | Internal WASM derive macros for Bomboni library. |
| homepage | https://github.com/tinrab/bomboni |
| repository | https://github.com/tinrab/bomboni |
| max_upload_size | |
| id | 1082144 |
| size | 38,368 |
wasm_deriveProvides derive macros for generating TypeScript WASM bindings for Rust types.
This crate offers the Wasm derive macro that generates code to make Rust types
usable in JavaScript/TypeScript environments through wasm-bindgen, with automatic
TypeScript type generation.
JsValue conversion supportuse bomboni_wasm_derive::Wasm;
#[derive(Wasm)]
#[wasm(rename = "User", into_wasm_abi = true, from_wasm_abi = true)]
struct User {
#[wasm(rename = "userName")]
name: String,
#[wasm(override_type = "Date")]
created_at: /* DateTime<Utc> */,
#[wasm(always_some = true)]
optional_field: Option<String>,
}
The macro generates:
FromWasmAbi, IntoWasmAbi, etc.)The #[wasm(...)] attribute can be applied to structs to control overall behavior:
rename = "Name" - Custom name in TypeScriptinto_wasm_abi = bool - Generate IntoWasmAbi implementationfrom_wasm_abi = bool - Generate FromWasmAbi implementationwasm_abi = bool - Generate both IntoWasmAbi and FromWasmAbienum_value = bool - Generate enum value object (for enums)js_value(...) - Custom JsValue conversionproxy = Type - Use proxy type for conversionsrename_all = "rule" - Rename rule for all fieldsoverride_type = "string" - Override generated TypeScript typechange_ref = "mapping" - Reference type mapping*_crate = "path" - Custom crate pathsThe #[wasm(...)] attribute can be applied to fields:
rename = "name" - Custom field name in TypeScriptoverride_type = "type" - Override field typerename_wrapper = bool - Control wrapper type renamingalways_some = bool - Force field to be required in TypeScriptchange_ref = "mapping" - Reference type mapping for fieldJsValue Conversions#[derive(Wasm)]
#[wasm(js_value(
into = MyType::to_js_value,
try_from = MyType::from_js_value,
))]
struct MyType {
value: String,
}
impl MyType {
fn to_js_value(self) -> /* JsValue */ {
// Custom conversion logic
}
fn from_js_value(value: &/* JsValue */) -> Result<Self, /* JsValue */> {
// Custom conversion logic
}
}
#[derive(Wasm)]
#[wasm(js_value(convert_string))]
struct UserId(String);
#[derive(Wasm)]
#[wasm(js_value(convert_string))]
struct Email(String);
#[derive(Wasm)]
#[wasm(proxy = UserProxy)]
struct User {
id: u32,
name: String,
}
// With custom conversions
#[derive(Wasm)]
#[wasm(proxy(
source = UserProxy,
into = User::to_proxy,
try_from = User::from_proxy,
))]
struct User2 {
id: u32,
name: String,
}
#[derive(Wasm)]
#[wasm(enum_value = true)]
enum Status {
#[wasm(rename = "active")]
Active,
#[wasm(rename = "inactive")]
Inactive,
}
#[derive(Wasm)]
#[wasm(change_ref = "&str -> string")]
struct MyStruct {
#[wasm(change_ref = "&[u8] -> Uint8Array")]
data: Vec<u8>,
}
#[derive(Wasm)]
#[wasm(
wasm_bindgen_crate = "crate::wasm",
js_sys_crate = "crate::js",
bomboni_crate = "crate::internal::bomboni",
)]
struct MyStruct {
field: String,
}