#protobuf #json #jsonrpc

pbjsonrpc-build

Generates jsonrpsee trait for prost message types

6 releases

0.2.0 Sep 8, 2022
0.1.1 Sep 6, 2022
0.1.0 Aug 3, 2022
0.0.3 Jun 4, 2022
0.0.2 May 23, 2022

#82 in Build Utils

Download history 71/week @ 2022-06-02 7/week @ 2022-06-09 16/week @ 2022-06-16 13/week @ 2022-06-23 3/week @ 2022-06-30 3/week @ 2022-07-07 4/week @ 2022-07-14 21/week @ 2022-07-21 32/week @ 2022-07-28 20/week @ 2022-08-04 11/week @ 2022-08-11 6/week @ 2022-08-18 14/week @ 2022-08-25 32/week @ 2022-09-01 42/week @ 2022-09-08 9/week @ 2022-09-15

97 downloads per month

MIT license

41KB
991 lines

Pbjsonrpc-build

Automatically generate jsonrpsee Trait for auto-generated prost types.

Usage

[dependencies]
pbjson = "0.3"
pbjson-types = "0.3"
prost = "0.10"
prost-types = "0.10"

[build-dependencies]
pbjson-build = "0.3"
pbjsonrpc-build = "0"
prost-build = "0.10"

Next create a build.rs containing the following

let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("protos");
let proto_files = vec![root.join("myproto.proto")];

// Tell cargo to recompile if any of these proto files are changed
for proto_file in &proto_files {
    println!("cargo:rerun-if-changed={}", proto_file.display());
}

let descriptor_path = PathBuf::from(env::var("OUT_DIR").unwrap())
    .join("proto_descriptor.bin");

prost_build::Config::new()
    // Save descriptors to file
    .file_descriptor_set_path(&descriptor_path)
    // Override prost-types with pbjson-types
    .compile_well_known_types()
    .extern_path(".google.protobuf", "::pbjson_types")
    // Generate prost structs
    .compile_protos(&proto_files, &[root])?;

let descriptor_set = std::fs::read(descriptor_path)?;
pbjson_build::Builder::new()
    .register_descriptors(&descriptor_set)?
    .build(&[".mypackage"])?;
pbjsonrpc_build::Builder::new()
    .register_descriptors(&descriptor_set)?
    .build(&[".mypackage"])?;

Finally within lib.rs

/// Generated by [`prost-build`]
include!(concat!(env!("OUT_DIR"), "/mypackage.rs"));
/// Generated by [`pbjson-build`]
include!(concat!(env!("OUT_DIR"), "/mypackage.serde.rs"));
/// Generated by [`pbjsonrpc-build`]
include!(concat!(env!("OUT_DIR"), "/mypackage.jsonrpc.rs"));

Dependencies

~1–1.5MB
~31K SLoC