Crates.io | postcard-bindgen |
lib.rs | postcard-bindgen |
version | 0.4.0 |
source | src |
created_at | 2022-11-06 21:23:40.490911 |
updated_at | 2024-10-14 12:36:12.785818 |
description | A crate to generate bindings for the postcard binary format for other languages than Rust. |
homepage | |
repository | https://github.com/teamplayer3/postcard-bindgen.git |
max_upload_size | |
id | 706752 |
size | 72,592 |
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 as a npm package to import it into a JavaScript project or as a pip package for python.
:warning: Run the crate that generates the bindings with rust nightly. This is necessary because this crate depends on genco and this crate uses a nightly feature to detect column changes.
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.
If the
postcard-bindgen
crate is added as a dependency in the generation binary crate the futuregenerating
must be enabled.
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.
#[derive(Serialize, PostcardBindings)]
struct Test {
name: u8,
other: u16,
}
fn main() {
javascript::build_package(
std::env::current_dir().unwrap().as_path(),
PackageInfo {
name: "generation-test".into(),
version: "0.1.0".try_into().unwrap(),
},
javascript::GenerationSettings::enable_all(),
generate_bindings!(Test),
)
.unwrap();
}
The following code can now be used to serialize an object in JavaScript.
import { serialize } from "generation-test";
const test = {
name: "test",
other: 23
}
const bytes = serialize("Test", test)
Type Name | Rust | Js | Python |
Unit Type |
|
|
|
Tuple Struct |
|
|
|
Struct |
|
|
|
Enum |
|
|
|
Option |
|
|
|
Map |
|
|
|
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