postcard-bindgen-core 0.1.1

A crate to generate bindings for the postcard binary format for other languages than Rust - Core Crate
Documentation

Postcard Bindgen

Build status License Crates.io Documentation

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

  • JavaScript
  • Python

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

#[derive(Serialize, PostcardBindings)]
struct Test {
    name: u8,
    other: u16,
}

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

JavaScript Type mapping

struct UnitStruct;
{}
struct NewType(u8);
[123]
struct TupleStruct(u8, u16, u32);
[123, 1234, 12345]
struct Struct {
    a: u8,
    b: u16
};
{
    a: 123,
    b: 1234
}
enum Enum {
    A,
    B(u8),
    C {
        a: u8
    }
};
{
    key: "A",
},
{
    key: "B",
    value: 123
},
{
    key: "C",
    value: {
        a: 123
    }
}
struct OptionTuple(Option<u8>);

struct OptionStruct {
    a: Option<u8>
}
// OptionTuple(Some(123))
[123]
// OptionTuple(None)
[undefined]

// OptionStruct { a: Some(123) }
{
    a: 123
}
// OptionStruct { a: None }
{}
// or
{
    a: undefined
}
let map_string_key = HashMap::<string, u8>::new();

let map_any_key = HashMap::<u16, u8>::new();
// map_string_key
{
    key: value
}

// map_any_key
new Map()