re_protos_builder 0.38.1

Code generation for Rerun's Protobuf and gRPC definitions.
Documentation
//! This crate contains logic for generating remote store gRPC API types as defined in
//! `re_protos` proto files. We are currently generating both client and server
//! definitions in the same file.
//!

#![expect(clippy::exit)]

/// The encoded `FileDescriptorSet` that `re_protos` serves through gRPC server reflection.
pub const FILE_DESCRIPTOR_SET_FILE_NAME: &str = "rerun.v1alpha1.fds.bin";

/// Generate rust from protobuf definitions. We rely on `tonic_build` to do the heavy lifting.
/// `tonic_build` relies on `prost` which itself relies on `protoc`.
///
/// Note: make sure to invoke this via `pixi run codegen-protos` in order to use the right `protoc` version.
pub fn generate_rust_code<P>(definitions_dir: P, proto_paths: &[P], output_dir: &P)
where
    P: AsRef<std::path::Path>,
{
    let mut prost_config = tonic_prost_build::Config::new();
    prost_config.enable_type_names(); // tonic doesn't expose this option
    prost_config.file_descriptor_set_path(output_dir.as_ref().join(FILE_DESCRIPTOR_SET_FILE_NAME));
    prost_config.bytes([
        ".rerun.common.v1alpha1",
        ".rerun.cloud.v1alpha1",
        ".rerun.log_msg.v1alpha1",
        ".rerun.manifest_registry.v1alpha1",
    ]);
    if let Err(err) = tonic_prost_build::configure()
        .out_dir(output_dir)
        .build_client(true)
        .build_server(true)
        .build_transport(false) // Small convenience, but doesn't work on web
        .compile_with_config(prost_config, proto_paths, &[definitions_dir])
    {
        match err.kind() {
            std::io::ErrorKind::Other => {
                eprintln!("Failed to generate protobuf types:\n{err}");
                std::process::exit(1);
            }
            _ => {
                panic!("{err}");
            }
        }
    }
}