Postcard Bindgen

Build status

The postcard crate serializes and deserializes rust structs by using the serde crate to a byte format. The resulting byte size is minimal. This is very useful if serialization and deserialization is done in rust and share the same structures.

This crate can generate bindings from the rust structures for other languages than rust. This allows to use the postcard crate from other languages.

Crate is work in progress. By now it can't be used for productions.

Supported languages

Usage

The structs for which bindings should be generated must be annotated with the PostcardBindings macro. This macro understands serde annotation. This means renaming fields and other functionality by serde is supported.

Example

```rust

[derive(Serialize, PostcardBindings)]

struct Test { name: u8, other: u16, }

fn main() { exportbindings( Path::new("./jsexport.js"), generate_bindings!(Test), // register container for generating bindings ) .unwrap(); } ```

JavaScript Type mapping

Type Name Rust Js
Unit Type ```rust struct UnitStruct; ``` ```javascript {} ```
New Type ```rust struct NewType(u8); ``` ```javascript [123] ```
Tuple Struct ```rust struct TupleStruct(u8, u16, u32); ``` ```javascript [123, 1234, 12345] ```
Struct ```rust struct Struct { a: u8, b: u16 }; ``` ```javascript { a: 123, b: 1234 } ```
Enum ```rust enum Enum { A, B(u8), C { a: u8 } }; ``` ```javascript { key: "A", }, { key: "B", value: 123 }, { key: "C", value: { a: 123 } } ```
Option ```rust struct OptionTuple(Option); struct OptionStruct { a: Option } ``` ```javascript // OptionTuple(Some(123)) [123] // OptionTuple(None) [undefined] // OptionStruct { a: Some(123) } { a: 123 } // OptionStruct { a: None } {} // or { a: undefined } ```
Map ```rust let map_string_key = HashMap::::new(); let map_any_key = HashMap::::new(); ``` ```javascript // map_string_key { key: value } // map_any_key new Map() ```