tafia / quick-protobuf

A rust implementation of protobuf parser
MIT License
452 stars 87 forks source link

Example on transpiling multiple .proto files #159

Open dlazerka opened 4 years ago

dlazerka commented 4 years ago

All examples and code I see assume there's only one .proto file. What is recommended way to compile .proto to .rs files, if we have thousands of input .proto files?

HiceS commented 1 year ago

Hey this is an old issue but here is an example on compiling multiple proto file to rs files.

CLI

Code: Here is an example of my code however there is a problem discovering types in multiple message This will look two parent directories up and then output the files to the src/protos folder.

use pb_rs::{types::FileDescriptor, ConfigBuilder};
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

fn main() {
    let out_dir = PathBuf::from(::std::env::var("CARGO_MANIFEST_DIR").unwrap()).join("src").join("protos");
    let in_dir = PathBuf::from(::std::env::var("CARGO_MANIFEST_DIR").unwrap()).parent().unwrap().parent().unwrap().to_path_buf();
    // Re-run this build.rs if the protos dir changes (i.e. a new file is added)
    // println!("cargo:rerun-if-changed={}", in_dir.to_str().unwrap());

    println!("{} -> {}", in_dir.to_str().unwrap(), out_dir.to_str().unwrap());

    // Find all *.proto files in the `in_dir` and add them to the list of files
    let mut protos = Vec::new();
    let proto_ext = Some(Path::new("proto").as_os_str());
    for entry in WalkDir::new(&in_dir) {
        let path = entry.unwrap().into_path();
        if path.extension() == proto_ext {
            println!("Found {:?}", path.to_str().unwrap());
            // Re-run this build.rs if any of the files in the protos dir change
            // println!("cargo:rerun-if-changed={}", path.to_str().unwrap());
            protos.push(path);
        }
    }

    // Delete all old generated files before re-generating new ones
    if out_dir.exists() {
        std::fs::remove_dir_all(&out_dir).unwrap();
    }

    std::fs::DirBuilder::new().create(&out_dir).unwrap();
    let config_builder = ConfigBuilder::new(&protos, None, Some(&out_dir), &[in_dir]).unwrap();
    FileDescriptor::run(&config_builder.build()).unwrap()
}