plonk_wasm/wasm_ocaml_serde/
mod.rs

1//! This module constructs a serde serializer (and deserializer) to convert Rust structures to (and from) Js types expected by js-of-ocaml.
2//! js-of-ocaml expects arrays of values instead of objects, so a Rust structure like:
3//!
4//! ```ignore
5//! { a: F, b: Vec<F>, c: SomeType }
6//! ```
7//!
8//! must be converted to an array that looks like this:
9//!
10//! ```ignore
11//! // notice the leading 0, which is an artifact of OCaml's memory layout and how js-of-ocaml is implemented.
12//! [0, a, b, c]
13//! ```
14//! See the following example on how to use it:
15//!
16//! ```
17//! #[derive(serde::Serialize, serde::Deserialize)]
18//! struct Thing { a: usize, b: u32 }
19//!
20//! let serializer = crate::wasm_ocaml_serde::ser::Serializer::new();
21//! let thing = Thing { a: 5, b: 6 };
22//! let js_value = serde::Serialize::serialize(thing, &mut serializer).unwrap();
23//! assert_eq!(format!("{}", js_value), "[0, 5, 6]");
24//! ```
25
26use wasm_bindgen::prelude::*;
27
28pub mod de;
29pub mod ser;
30
31pub use serde_wasm_bindgen::Error;
32
33pub type Result<T = JsValue> = core::result::Result<T, Error>;