Skip to content

Commit

Permalink
feat(build): Add extern_path config support (#223)
Browse files Browse the repository at this point in the history
Adds support for prost-build's "extern_path" feature. This allows you to
reference another prost-generated protobuf, including any traits that
may have been defined for it, from another crate or location.
  • Loading branch information
adamhjk authored and LucioFranco committed Jan 10, 2020
1 parent a66595b commit e034288
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tonic-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ mod server;
pub struct Builder {
build_client: bool,
build_server: bool,
extern_path: Vec<(String, String)>,
field_attributes: Vec<(String, String)>,
type_attributes: Vec<(String, String)>,
out_dir: Option<PathBuf>,
Expand Down Expand Up @@ -111,6 +112,17 @@ impl Builder {
self
}

/// Declare externally provided Protobuf package or type.
///
/// Passed directly to `prost_build::Config.extern_path`.
pub fn extern_path(mut self, proto_path: impl AsRef<str>, rust_path: impl AsRef<str>) -> Self {
self.extern_path.push((
proto_path.as_ref().to_string(),
rust_path.as_ref().to_string(),
));
self
}

/// Add additional attribute to matched messages, enums, and one-offs.
///
/// Passed directly to `prost_build::Config.field_attribute`.
Expand Down Expand Up @@ -142,6 +154,9 @@ impl Builder {
.unwrap_or_else(|| PathBuf::from(std::env::var("OUT_DIR").unwrap()));

config.out_dir(out_dir.clone());
for (proto_path, rust_path) in self.extern_path.iter() {
config.extern_path(proto_path, rust_path);
}
for (path, attr) in self.field_attributes.iter() {
config.field_attribute(path, attr);
}
Expand Down Expand Up @@ -171,6 +186,7 @@ pub fn configure() -> Builder {
build_client: true,
build_server: true,
out_dir: None,
extern_path: Vec::new(),
field_attributes: Vec::new(),
type_attributes: Vec::new(),
#[cfg(feature = "rustfmt")]
Expand Down

0 comments on commit e034288

Please sign in to comment.