如何在不知道名称的情况下包含任意一组由 Protobuf 构建的文件?

How can I include an arbitrary set of Protobuf-built files without knowing their names?

我打算使用 rust-protobuf library。我编写了一个 bash 脚本来构建所有内容(包括我的代码)并将我拥有的 .proto 文件构建到 .rs 文件中。文档告诉我继续的方式是专门做:

mod foo;

对于生成的每个 .rs 文件。我希望我的用户可以将新的 .proto 文件放到一个目录中,运行 构建脚本,我的代码将负责包含已编译的 .proto 文件的所有 Rust 实现。

我知道 Rust 并不真正支持反射,所以有什么方法可以让我基本上"determine all the .rs generated files in a directory and use them in my code"(在 TLDR 语句中)。

您可以编写一个 Cargo build script 来扫描您的目录并生成一个如下所示的 Rust 文件:

mod file1;
mod file2;
//etc

然后您可以使用 include! macro.

将此文件包含在您的库中