wasm_bindgen_utils/
lib.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
//! Provides utilities, helpers and macros to easily build and customize [wasm_bindgen] bindings,
//! such as [impl_wasm_traits] macro that will implement wasm traits for a give type and
//! [serialize_hashmap_as_object] serializer function to serialize a hashmap as object used with
//! serde `serialize_with` attribute.
//! For more details please read the doumentation of the items of this lib.
//!
//! Example:
//! ```ignore
//! use wasm_bindgen_utils::{prelude::*, impl_wasm_traits, impl_custom_tsify, add_ts_content};
//!
//! #[derive(Serialize, Deserialize)]
//! #[serde(rename_all = "camelCase")]
//! pub struct SomeType {
//!     #[cfg_attr(target_family = "wasm", serde(serialize_with = "serialize_as_bytes"))]
//!     pub field: Vec<u8>,
//!     #[cfg_attr(target_family = "wasm", serde(serialize_with = "serialize_hashmap_as_object"))]
//!     pub other_field: HashMap<String, u8>,
//! }
//!
//! // impl wasm traits for SomeType
//! impl_wasm_traits!(SomeType);
//!
//! // impl tsify manually for SomeType (as an alternative to Tsify derive macro)
//! // the given string literal will become the typescript interface bindings for SomeType
//! impl_custom_tsify!(
//!     SomeType,
//!     "export interface SomeType {
//!         field: Uint8Array;
//!         otherField: Record<string, number>;
//!     }"
//! );
//!
//! // appends a custom section to the .d.ts generated bindings
//! add_ts_content!("import { Something } from 'some-js-lib'")
//!
//! // now someType can be used on functions and methods natively
//! #[wasm_bindgen]
//! pub fn some_fn(arg: SomeType) -> String {
//!     // body
//! }
//!
//! #[wasm_bindgen]
//! pub async fn some_other_fn(arg1: Vec<u8>, arg2: HashMap<String, u8>) -> Result<SomeType, Error> {
//!     // body
//!     Ok(SomeType {
//!         field: arg1,
//!         other_field: arg2
//!     })
//! }
//! ```

mod ser;
pub mod macros;

pub use ser::*;

// prelude exports
pub mod prelude {
    pub use paste;
    pub use js_sys;
    pub use tsify;
    pub use wasm_bindgen;
    pub use serde_wasm_bindgen;
    pub use wasm_bindgen_futures;
    pub use tsify::Tsify;
    pub use wasm_bindgen::prelude::*;
    pub use serde_wasm_bindgen::{to_value as to_js_value, from_value as from_js_value};
}