andrewhickman / protox

A pure-rust protobuf compiler, designed for use with prost-build
Apache License 2.0
73 stars 6 forks source link

Better build instructions for tonic #17

Closed 0xAlcibiades closed 1 year ago

0xAlcibiades commented 1 year ago

The example of using this with tonic is wrong/incomplete at present. Here is a bit more of a complete version:

use std::{env, fs};
use std::path::PathBuf;
use prost::Message;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Compile the proto files
    let file_descriptors = protox::compile(["root.proto",], [".",]).unwrap();

    // Get the path to the output directory for the file descriptor set
    let file_descriptor_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("file_descriptor_set.bin");

    // Write the file descriptor set to the output directory
    fs::write(&file_descriptor_path, file_descriptors.encode_to_vec()).unwrap();

    // Run tonic code generation
    tonic_build::configure()
        .build_server(true)
        .file_descriptor_set_path(file_descriptor_path)
        .skip_protoc_run()
        .compile(
            &[
                "proto/proto/valorem/trade/v1/root.proto",
            ],
            &["."], // specify the root location to search proto dependencies
        )
        .unwrap();
    Ok(())
}

Additionally, a build dependency for prost = "0.12.1" must be added to the crate building. I do wonder if this should be added to the crates build time dependecies/if that would abstract from the caller.

andrewhickman commented 1 year ago

Thanks for the example - I've fixed the docs in 961527f62889b2c4eb9440a2ffc1d35c72459e2d.

I've also added a re-export of prost so it doesn't need to be directly added as a build dependency, but can be imported as use protox::prost::Message