Crates.io | include_proto_dir |
lib.rs | include_proto_dir |
version | 0.1.0 |
source | src |
created_at | 2024-09-19 18:24:27.733618 |
updated_at | 2024-09-19 18:24:27.733618 |
description | A Rust crate for embedding and extracting Protobuf directories in your binary. |
homepage | https://github.com/bliednov/include_proto_dir |
repository | https://github.com/bliednov/include_proto_dir |
max_upload_size | |
id | 1380574 |
size | 21,943 |
include_proto_dir
is a Rust crate that simplifies embedding and extracting Protocol Buffer (.proto
) files into your Rust binaries. It enhances the developer experience when working with Protobufs in Rust, especially when creating crates for your protobuf definitions.
The crate is a thin wrapper around include_dir
.
By using include_proto_dir
, you can embed your .proto
files directly into your crate, ensuring that they are always available during build time of the dependent crates. This approach eliminates the need to manage external .proto
files and simplifies the build process for your Protobuf crates.
For the proto crate:
.proto
files directly into your binary.use include_proto_dir::*;
pub const PROTO_DIR: include_proto_dir::ProtoDir = include_proto_dir!("$CARGO_MANIFEST_DIR/proto");
// that is the same as:
include_proto_dir_default!();
.proto
files during the build process in build.rs
.let proto_dir = PROTO_DIR.extract(&PathBuf::from(std::env::var("OUT_DIR")?));
.proto
files to ensure that the generated Rust code is up-to-date.println!("cargo:rerun-if-changed={}", proto_dir.to_glob());
.proto
files using prost-build
.prost_build::Config::new()
.compile_protos(proto_dir.protos(), &[proto_dir.as_path()])?;
#[cfg(test)]
mod tests {
use super::*;
#[macro_export]
macro_rules! include_proto {
($package: tt) => {
include!(concat!(env!("OUT_DIR"), concat!("/", $package, ".rs")));
};
}
include_proto!("foo.v1");
#[test]
fn test_protobuf_compilation() {
// Use the generated code in some way to ensure it compiles.
}
}
For the dependent crate:
[build-dependencies]
in your Cargo.toml
.[build-dependencies]
proto = { path = "../proto" }
.proto
files from the proto crate during the build process in build.rs
and generate Rust code from the extracted .proto
files using prost-build
the way you like.use include_proto_dir::include_proto_dir_default;
include_proto_dir_default!();
fn main() -> Result<(), Box<dyn std::error::Error>> {
let proto_dir = PROTO_DIR.extract(&PathBuf::from(std::env::var("OUT_DIR")?));
println!("cargo:rerun-if-changed={}", proto_dir.to_glob());
prost_build::Config::new()
.compile_protos(proto_dir.protos(), &[proto_dir.as_path()])?;
Ok(())
}
Add include_proto_dir
to your Cargo.toml
:
[dependencies]
include_proto_dir = "0.1.0"
Contributions are welcome! Please submit issues and pull requests on GitHub.
This project is licensed under the MIT License..