Crate include_proto_dir

Source
Expand description

§include_proto_dir

include_proto_dir is a Rust crate that simplifies embedding and extracting Protocol Buffer (.proto) files into your Rust binaries.

This crate is designed to improve the developer experience when working with Protocol Buffers in Rust, especially when creating crates for your protobuf definitions.

The crate is a thin wrapper around include_dir.

§Examples

Use the include_proto_dir! macro to include your protobuf directory into your binary:

use include_proto_dir::include_proto_dir;

const PROTO_DIR: include_proto_dir::ProtoDir = include_proto_dir!("$CARGO_MANIFEST_DIR/proto");

Alternatively, if your protobuf files are located in the default proto directory at the root of your crate and you want, you can use the include_proto_dir_default! macro:

use include_proto_dir::*;

include_proto_dir_default!();
// It is the same as the following line, note that PROTO_DIR is public and can be used by dependent crates:
// pub const PROTO_DIR: include_proto_dir::ProtoDir = include_proto_dir!("$CARGO_MANIFEST_DIR/proto");

In your build script build.rs, you can extract the embedded .proto files and generate Rust code using e.g. prost-build:

mod some_proto_crate {
    use include_proto_dir::include_proto_dir_default;
    include_proto_dir_default!();
}

use some_proto_crate::PROTO_DIR;
use std::path::PathBuf;
extern crate build_deps;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let out_dir = PathBuf::from(std::env::var("OUT_DIR")?);
    let proto_dir = PROTO_DIR.extract(&out_dir)?;

    build_deps::rerun_if_changed_paths(&proto_dir.to_glob()).map_err(|e| format!("{:?}", e))?;
    let mut builder = prost_build::Config::new();
    builder.compile_protos(proto_dir.protos(), &[proto_dir.as_path()])?;

    Ok(())
}

Macros§

include_proto_dir
Macro to include the Protobuf directory using include_dir.
include_proto_dir_default
Macro to generate the default PROTO_DIR.

Structs§

ExtractedProtoDir
A struct that represents the extracted Protobuf directory.
ProtoDir
A struct that represents a directory of embedded Protobuf files.