wasm_bindgen_utils/
lib.rs

1//! Provides utilities, helpers and macros to easily build and customize [wasm_bindgen] bindings,
2//! such as [impl_wasm_traits] macro that will implement wasm traits for a give type and
3//! [serialize_hashmap_as_object] serializer function to serialize a hashmap as object used with
4//! serde `serialize_with` attribute.
5//! For more details please read the doumentation of the items of this lib.
6//!
7//! Example:
8//! ```ignore
9//! use wasm_bindgen_utils::{prelude::*, impl_wasm_traits, impl_custom_tsify, add_ts_content};
10//!
11//! #[derive(Serialize, Deserialize)]
12//! #[serde(rename_all = "camelCase")]
13//! pub struct SomeType {
14//!     #[cfg_attr(target_family = "wasm", serde(serialize_with = "serialize_as_bytes"))]
15//!     pub field: Vec<u8>,
16//!     #[cfg_attr(target_family = "wasm", serde(serialize_with = "serialize_hashmap_as_object"))]
17//!     pub other_field: HashMap<String, u8>,
18//! }
19//!
20//! // impl wasm traits for SomeType
21//! impl_wasm_traits!(SomeType);
22//!
23//! // impl tsify manually for SomeType (as an alternative to Tsify derive macro)
24//! // the given string literal will become the typescript interface bindings for SomeType
25//! impl_custom_tsify!(
26//!     SomeType,
27//!     "export interface SomeType {
28//!         field: Uint8Array;
29//!         otherField: Record<string, number>;
30//!     }"
31//! );
32//!
33//! // appends a custom section to the .d.ts generated bindings
34//! add_ts_content!("import { Something } from 'some-js-lib'")
35//!
36//! // now someType can be used on functions and methods natively
37//! #[wasm_bindgen]
38//! pub fn some_fn(arg: SomeType) -> String {
39//!     // body
40//! }
41//!
42//! #[wasm_bindgen]
43//! pub async fn some_other_fn(arg1: Vec<u8>, arg2: HashMap<String, u8>) -> Result<SomeType, Error> {
44//!     // body
45//!     Ok(SomeType {
46//!         field: arg1,
47//!         other_field: arg2
48//!     })
49//! }
50//! ```
51
52mod ser;
53pub mod macros;
54pub mod result;
55
56pub use ser::*;
57pub use wasm_bindgen_utils_macros::*;
58
59// prelude exports
60pub mod prelude {
61    pub use paste;
62    pub use js_sys;
63    pub use tsify;
64    pub use wasm_bindgen;
65    pub use serde_wasm_bindgen;
66    pub use wasm_bindgen_futures;
67    pub use tsify::Tsify;
68    pub use wasm_bindgen::prelude::*;
69    pub use serde_wasm_bindgen::{to_value as to_js_value, from_value as from_js_value};
70    pub use wasm_bindgen_utils_macros::*;
71    pub use super::result::*;
72}