【问题标题】:Rust tonic tonic::include_proto file path from one module to another module in tonic buildRust tonic tonic::include_proto 在 tonic 构建中从一个模块到另一个模块的文件路径
【发布时间】:2022-05-09 03:48:19
【问题描述】:

我正在使用Tonic 实现 gRPC 客户端和服务器。我有两个模块,每个模块取决于另一个模块原型文件。当我尝试在 tonic 构建中提供 proto 文件的路径时,我遇到了一个问题。下面是我的 tonic 构建的文件夹结构和代码。

   -organization
     -src
       -client
         -mod.rs
       -service
       -cargo.toml
   -Employee
     -src
       -service
       -proto
         -proto_file
       -cargo.toml

pub mod Employee_info {
    tonic::include_proto!("{path}/employee_info.proto"); //this is organisation `mod file`. i want to pass the proto file path of employee folder->proto->proto file.
}

【问题讨论】:

  • 这看起来不像docstutorial 中描述的用法。 build.rs 文件应该正在编译 ".proto" 文件,然后 include_proto! 应该通过 proto 包名称 引用它们。

标签: rust tonic rust-tonic


【解决方案1】:

你的employee_info.proto应该被编译成rust文件employee_info.rs

你的build.rs 文件应该是这样的:

fn main() {
    let proto_file = "./src/proto/employee_info.proto";
    tonic_build::configure()
        .build_server(true)
        .out_dir("./src")
        .compile(&[proto_file], &["."])
        .unwrap_or_else(|e| panic!("protobuf compile error: {}", e));
    println!("cargo:rerun-if-changed={}", proto_file);
}

cargo build 之后,预计会看到以下文件正在生成:src/employee_info.rs

那么你只需要像往常一样在你的代码中包含这个:

mod employee_info {
    include!("employee_info.rs");
}

https://docs.rs/tonic/latest/tonic/macro.include_proto.html 中所述,您只能在未修改输出目录的情况下通过tonic::include_proto 包含包。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    相关资源
    最近更新 更多