wasm_bindgen_utils/macros.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
/// A macro that implements main wasm traits for the given type.
/// These traits are the necessary ones to be able to send/receive
/// the given type through wasm bindgen boundry.
/// The type needs to have [serde::Serialize], [serde::Deserialize]
/// and [tsify::Tsify] traits implemented.
///
/// Example:
/// ```ignore
/// #[derive(Serialize, Deserialize, Tsify)]
/// #[serde(rename_all = "camelCase")]
/// pub struct A {
/// pub field: String,
/// pub other_field: u8,
/// }
/// impl_main_wasm_traits!(A);
///
/// #[wasm_bindgen]
/// pub fn some_fn(arg: A) -> String {
/// // body
/// }
///
/// #[wasm_bindgen]
/// pub fn some_other_fn(arg: String) -> Option<A> {
/// // body
/// }
/// ```
#[macro_export]
macro_rules! impl_main_wasm_traits {
($type_name:path) => {
impl $type_name {
const TYPE_NAME: &'static str = stringify!($type_name);
pub fn try_into_js_value(&self) -> Result<$crate::prelude::JsValue, $crate::prelude::serde_wasm_bindgen::Error> {
$crate::prelude::to_js_value(&self)
}
pub fn try_from_js_value(js: $crate::prelude::JsValue) -> Result<Self, $crate::prelude::serde_wasm_bindgen::Error> {
$crate::prelude::from_js_value(js)
}
}
impl $crate::prelude::wasm_bindgen::describe::WasmDescribe for $type_name {
#[inline]
fn describe() {
<Self as $crate::prelude::Tsify>::JsType::describe()
}
}
impl $crate::prelude::wasm_bindgen::convert::IntoWasmAbi for $type_name {
type Abi = <<Self as $crate::prelude::Tsify>::JsType as $crate::prelude::wasm_bindgen::convert::IntoWasmAbi>::Abi;
#[inline]
fn into_abi(self) -> Self::Abi {
let mut err = String::new();
err.push_str(Self::TYPE_NAME);
err.push_str(": ");
let result = self.try_into_js_value();
$crate::prelude::UnwrapThrowExt::expect_throw(result.inspect_err(|e| err.push_str(&e.to_string())), &err).into_abi()
}
}
impl $crate::prelude::wasm_bindgen::convert::OptionIntoWasmAbi for $type_name {
#[inline]
fn none() -> Self::Abi {
<<Self as $crate::prelude::Tsify>::JsType as $crate::prelude::wasm_bindgen::convert::OptionIntoWasmAbi>::none()
}
}
impl $crate::prelude::wasm_bindgen::convert::FromWasmAbi for $type_name {
type Abi = <<Self as $crate::prelude::Tsify>::JsType as $crate::prelude::wasm_bindgen::convert::FromWasmAbi>::Abi;
#[inline]
unsafe fn from_abi(js: Self::Abi) -> Self {
let mut err = String::new();
err.push_str(Self::TYPE_NAME);
err.push_str(": ");
let result = Self::try_from_js_value(js.into());
$crate::prelude::UnwrapThrowExt::expect_throw(result.inspect_err(|e| err.push_str(&e.to_string())), &err)
}
}
impl $crate::prelude::wasm_bindgen::convert::OptionFromWasmAbi for $type_name {
#[inline]
fn is_none(js: &Self::Abi) -> bool {
<<Self as $crate::prelude::Tsify>::JsType as $crate::prelude::wasm_bindgen::convert::OptionFromWasmAbi>::is_none(js)
}
}
};
}
/// Implements complementary wasm traits for the given type.
/// Needs [impl_main_wasm_traits] to be implemented first.
/// It allows a type to be used on async functions normally or
/// as ref or as Vec<> etc.
/// The type needs to have [serde::Serialize], [serde::Deserialize]
/// and [tsify::Tsify] traits implemented.
///
/// Example:
/// ```ignore
/// #[derive(Serialize, Deserialize, Tsify)]
/// #[serde(rename_all = "camelCase")]
/// pub struct A {
/// pub field: String,
/// pub other_field: u8,
/// }
/// impl_main_wasm_traits!(A);
/// impl_complementary_wasm_traits!(A);
///
/// #[wasm_bindgen]
/// pub async fn some_fn(arg: &A) -> Result<String, Error> {
/// // body
/// }
/// ```
#[macro_export]
macro_rules! impl_complementary_wasm_traits {
($type_name:path) => {
impl $crate::prelude::wasm_bindgen::convert::RefFromWasmAbi for $type_name {
type Abi = <<Self as $crate::prelude::Tsify>::JsType as $crate::prelude::wasm_bindgen::convert::RefFromWasmAbi>::Abi;
type Anchor = Box<$type_name>;
unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor {
Box::new(<$type_name as $crate::prelude::wasm_bindgen::convert::FromWasmAbi>::from_abi(js))
}
}
impl $crate::prelude::wasm_bindgen::convert::LongRefFromWasmAbi for $type_name {
type Abi = <<Self as $crate::prelude::Tsify>::JsType as $crate::prelude::wasm_bindgen::convert::LongRefFromWasmAbi>::Abi;
type Anchor = Box<$type_name>;
unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor {
Box::new(<$type_name as $crate::prelude::wasm_bindgen::convert::FromWasmAbi>::from_abi(js))
}
}
impl $crate::prelude::wasm_bindgen::convert::VectorIntoWasmAbi for $type_name {
type Abi = <Box<[<Self as $crate::prelude::Tsify>::JsType]> as $crate::prelude::wasm_bindgen::convert::IntoWasmAbi>::Abi;
fn vector_into_abi(vector: Box<[Self]>) -> Self::Abi {
$crate::prelude::wasm_bindgen::convert::js_value_vector_into_abi(vector)
}
}
impl $crate::prelude::wasm_bindgen::convert::VectorFromWasmAbi for $type_name {
type Abi = <Box<[<Self as $crate::prelude::Tsify>::JsType]> as $crate::prelude::wasm_bindgen::convert::FromWasmAbi>::Abi;
unsafe fn vector_from_abi(js: Self::Abi) -> Box<[Self]> {
$crate::prelude::wasm_bindgen::convert::js_value_vector_from_abi(js)
}
}
impl $crate::prelude::wasm_bindgen::describe::WasmDescribeVector for $type_name {
fn describe_vector() {
$crate::prelude::wasm_bindgen::describe::inform($crate::prelude::wasm_bindgen::describe::VECTOR);
<$type_name as $crate::prelude::wasm_bindgen::describe::WasmDescribe>::describe();
}
}
impl From<$type_name> for $crate::prelude::wasm_bindgen::JsValue {
fn from(value: $type_name) -> Self {
let mut err = String::new();
err.push_str(<$type_name>::TYPE_NAME);
err.push_str(": ");
let result = value.try_into_js_value();
$crate::prelude::UnwrapThrowExt::expect_throw(
result.inspect_err(|e| err.push_str(&e.to_string())),
&err,
)
}
}
impl $crate::prelude::wasm_bindgen::convert::TryFromJsValue for $type_name {
type Error = $crate::prelude::serde_wasm_bindgen::Error;
fn try_from_js_value(value: $crate::prelude::wasm_bindgen::JsValue) -> Result<Self, Self::Error> {
Self::try_from_js_value(value)
}
}
};
}
/// Implement all wasm traits for the given type.
/// that is [impl_main_wasm_traits] and [impl_complementary_wasm_traits].
/// The type needs to have [serde::Serialize], [serde::Deserialize]
/// and [tsify::Tsify] traits implemented.
///
/// Example:
/// ```ignore
/// #[derive(Serialize, Deserialize, Tsify)]
/// #[serde(rename_all = "camelCase")]
/// pub struct A {
/// pub field: String,
/// pub other_field: u8,
/// }
/// impl_wasm_traits!(A);
///
/// #[wasm_bindgen]
/// pub fn some_fn(arg: Vec<A>) -> String {
/// // body
/// }
/// ```
#[macro_export]
macro_rules! impl_wasm_traits {
($type_name:path) => {
$crate::impl_main_wasm_traits!($type_name);
$crate::impl_complementary_wasm_traits!($type_name);
};
}
/// Implements [tsify::Tsify] with the given type declaration for the given rust
/// type (structs and enums) identifier.
///
/// This is the same as what [tsify::Tsify] derive macro does internally for a
/// given type but with full customization capability, as both are a sugar
/// for [wasm_bindgen] `typescript_custom_section` attr plus `extern C` block
/// defining a wrapped [wasm_bindgen::JsValue] for the given type.
/// Therefore, this macro (unlike tsify derive macro) puts representative
/// [wasm_bindgen::JsValue] of the given type on the current scope identified
/// by prepending "Js" to the orginial type identifier, meaning it would be
/// accessible by for example:
/// `JsSomeType` when original type is `SomeType`.
///
/// This is very usefull for cases where a rust type is not defined in current
/// module (like autogen types) and [tsify::Tsify] trait cannot be implemented
/// for as a result, so this will implement `Tsify` trait for the given type and
/// also allows to manually serialize/deserialize the [wasm_bindgen::JsValue]
/// to/from js side from/to the rust type, for example with custom serializers
/// and deserializers.
///
/// Example:
/// ```ignore
/// #[derive(Serialize, Deserialize)]
/// #[serde(rename_all = "camelCase")]
/// pub struct SomeType {
/// pub field: String,
/// pub other_field: u8,
/// }
/// impl_custom_tsify!(
/// SomeType,
/// // this will become the typescript
/// // interface bindings for SomeType
/// "export interface SomeType {
/// field: string;
/// otherField: number
/// };"
/// );
///
/// #[wasm_bindgen]
/// pub fn some_fn(arg: JsSomeType) -> JsSomeType {
/// // deserialize the arg which is a wrapped `JsValue`
/// // into rust `SomeType` using serde_wasm_bindgen
/// let val = serde_wasm_bindgen::from_value::<SomeType>(arg.obj).unwrap_throw();
///
/// // body
///
/// // serialize to JsValue optionally with serializer available
/// // options and wrap it in JsSomeType for return
/// let ser = serde_wasm_bindgen::Serializer::new().serialize_maps_as_objects(true);
/// JsSomeType { obj: val.serialize(ser).unwrap_throw() }
/// }
/// ```
#[macro_export]
macro_rules! impl_custom_tsify {
($type_name:ident, $decl:literal) => {
$crate::prelude::paste::paste! {
#[$crate::prelude::wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = [<$type_name>])]
pub type [<Js $type_name>];
}
#[$crate::prelude::wasm_bindgen(typescript_custom_section)]
const TYPESCRIPT_CONTENT: &'static str = $decl;
impl $crate::prelude::Tsify for $type_name {
type JsType = [<Js $type_name>];
const DECL: &'static str = $decl;
}
}
};
}
/// Adds/appends the given string literal to wasm bindgen typescript bindings.
/// This is just a sugar for [wasm_bindgen] `typescript_custom_section`, so
/// the given text can be anything, from typescript comment to type declarations
/// or any other valid .d.ts content.
///
/// Example:
/// ```ignore
/// // add some custom type to .d.ts bindings output
/// add_ts_content!("export type SomeType = { field: string; otherField: number };");
///
/// // add some comment to .d.ts bindings output
/// add_ts_content!("// this is some comment");
/// ```
#[macro_export]
macro_rules! add_ts_content {
($decl:literal) => {
$crate::prelude::paste::paste! {
#[$crate::prelude::wasm_bindgen(typescript_custom_section)]
const TYPESCRIPT_CONTENT: &'static str = $decl;
}
};
}
#[cfg(target_family = "wasm")]
#[cfg(test)]
mod tests {
use crate::*;
use js_sys::JsString;
use wasm_bindgen_test::wasm_bindgen_test;
use std::{collections::HashMap, str::FromStr};
#[derive(serde::Deserialize, serde::Serialize, Default)]
pub struct A {
pub field1: String,
#[serde(serialize_with = "serialize_as_bytes")]
pub field2: Vec<u8>,
#[serde(serialize_with = "serialize_hashmap_as_object")]
pub field3: HashMap<String, u64>,
}
// ensures macros validity at compile time
// impl tsify manualy for "A" that needs it
// before being able to impl all wasm traits
impl_custom_tsify!(
A,
"export interface A {
field1: String;
field2: Uint8Array;
field3: Record<string, bigint>;
};"
);
impl_wasm_traits!(A);
add_ts_content!("export type SomeType = string;");
#[wasm_bindgen_test]
fn test_macros() {
let res = A::default().try_into_js_value().unwrap();
// should exist
assert!(JsString::from_str("field1").unwrap().js_in(&res));
assert!(JsString::from_str("field2").unwrap().js_in(&res));
assert!(JsString::from_str("field3").unwrap().js_in(&res));
// should not exist
assert!(!JsString::from_str("field4").unwrap().js_in(&res));
}
}