Postcard Bindgen

Build status License Crates.io Documentation

Postcard Bindgen allows generating code for other languages to serialize to and deserialize from postcard byte format. This helps to setup a communication between for example a microcontroller and a App using the postcard crate and its lightweight memory format.

As main types structs and enums can be annotated with PostcardBindings to generate code for them. The generated code can be exported to a npm package to import it into a javascript project.

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

Usage

Structs and enums for which bindings should be generated must be annotated with Serialize/Deserialize from the serde crate and the PostcardBindings macro from this crate.

The process is divided into two steps. Firstly the annotation step. This is done mostly in a library crate. Secondly in a extra binary crate the annotated structs and enums must be imported (this means the library crate must be defined as a dependency) and as a main function the generation logic added. To generate the npm package this extra binary crate must be run.

When the postcard-bindgen crate is added as a dependency in the generation binary crate the future generating must be enabled.

Example

This example shows how to easily generate a npm package. For this the struct Test and the generation logic is in the same rust file.

```rust

[derive(Serialize, PostcardBindings)]

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

fn main() { buildnpmpackage( std::env::currentdir().unwrap().aspath(), PackageInfo { name: "test".into(), version: "0.1.0".tryinto().unwrap(), }, generatebindings!(Test), ) .unwrap(); } ```

To now serialize a struct in javascript the following code can be used.

```js const test = { name: "test", other: 23 }

const bytes = serialize("Test", test) ```

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() ```

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Postcard Bindgen by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions