From 93991ea06d5f366efd7021492e2e508026e71e2a Mon Sep 17 00:00:00 2001 From: Adam Jacob Date: Thu, 9 Jan 2020 16:10:31 -0800 Subject: [PATCH 1/2] Add prost extern_path support 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. --- tonic-build/src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tonic-build/src/lib.rs b/tonic-build/src/lib.rs index 17bfd15ee..454d4002c 100644 --- a/tonic-build/src/lib.rs +++ b/tonic-build/src/lib.rs @@ -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, @@ -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: T, rust_path: T) -> 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`. @@ -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); } @@ -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")] From 606b43459238387bcc1a677f34143087f652e70f Mon Sep 17 00:00:00 2001 From: Adam Jacob Date: Fri, 10 Jan 2020 12:23:27 -0800 Subject: [PATCH 2/2] switch to impl AsRef from generics --- tonic-build/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tonic-build/src/lib.rs b/tonic-build/src/lib.rs index 454d4002c..cf594d7da 100644 --- a/tonic-build/src/lib.rs +++ b/tonic-build/src/lib.rs @@ -115,7 +115,7 @@ impl Builder { /// Declare externally provided Protobuf package or type. /// /// Passed directly to `prost_build::Config.extern_path`. - pub fn extern_path>(mut self, proto_path: T, rust_path: T) -> Self { + pub fn extern_path(mut self, proto_path: impl AsRef, rust_path: impl AsRef) -> Self { self.extern_path.push(( proto_path.as_ref().to_string(), rust_path.as_ref().to_string(),