Crate postcard_bindgen
source ·Expand description
Postcard Bindgen
This crate allows generating javascript bindings automatically to serialize javascript objects to postcard format and vice versa.
Example
This example shows how to generate a npm package out of the rust
structures. A new folder with the package name will be created. A
javascript file and typescript typings as well as a package.json
will be paced in it.
#[derive(Serialize, PostcardBindings)]
struct A(u8);
#[derive(Serialize, PostcardBindings)]
struct B {
a: u8
}
#[derive(Serialize, PostcardBindings)]
enum C {
A,
B(u8),
C(A, B),
D { a: &'static str, b: B },
}
fn main() {
build_npm_package(
std::env::current_dir().unwrap().as_path(),
PackageInfo {
name: "test-bindings".into(),
version: "0.1.0".try_into().unwrap(),
},
generate_bindings!(A, B),
)
.unwrap();
}// JavaScript
import { serialize } form "test-bindings"
const c = {
tag: "C",
value: [
123,
{
a: 234
}
]
}
const bytes = serialize("C", c)
Macros
generate_bindings
generatingMacro to generate javascript and typescript binding strings which
can be exported into files.
Structs
ExportStrings
generatingHelper struct to pass the generated language strings to an export function.
PackageInfo
generatingWraps more infos for the npm package.
Version
generatingDefines a package version with major, minor, patch version numbers.
VersionFromStrError
generatingError type indicates that supplied string is not a version.
Traits
Functions
build_npm_package
generatingBuilds a npm package from create language binding strings.
export_bindings
generatingExport the generated binding string to a file with passed file name at passed path.
Derive Macros
Macro to annotate structs or enums for which bindings should be generated.