Skip to content

Commit

Permalink
feat(build): Decouple codgen from prost (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
Perlmint committed Mar 1, 2020
1 parent 1b3d107 commit f65cda1
Show file tree
Hide file tree
Showing 20 changed files with 550 additions and 356 deletions.
4 changes: 2 additions & 2 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ name = "hyper-warp-server"
path = "src/hyper_warp/server.rs"

[dependencies]
tonic = { path = "../tonic", features = ["tls"] }
tonic = { path = "../tonic", features = ["tls", "data-prost"] }
prost = "0.6"
tokio = { version = "0.2", features = ["rt-threaded", "time", "stream", "fs", "macros", "uds"] }
futures = { version = "0.3", default-features = false, features = ["alloc"] }
Expand All @@ -128,4 +128,4 @@ http-body = "0.3"
pin-project = "0.4"

[build-dependencies]
tonic-build = { path = "../tonic-build" }
tonic-build = { path = "../tonic-build", features = ["prost"] }
8 changes: 4 additions & 4 deletions examples/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main() {
tonic_build::compile_protos("proto/helloworld/helloworld.proto").unwrap();
tonic_build::compile_protos("proto/routeguide/route_guide.proto").unwrap();
tonic_build::compile_protos("proto/echo/echo.proto").unwrap();
tonic_build::compile_protos("proto/google/pubsub/pubsub.proto").unwrap();
tonic_build::prost::compile_protos("proto/helloworld/helloworld.proto").unwrap();
tonic_build::prost::compile_protos("proto/routeguide/route_guide.proto").unwrap();
tonic_build::prost::compile_protos("proto/echo/echo.proto").unwrap();
tonic_build::prost::compile_protos("proto/google/pubsub/pubsub.proto").unwrap();
}
2 changes: 1 addition & 1 deletion examples/helloworld-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ At the root of your crate, create a `build.rs` file and add the following code:

```rust
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::compile_protos("proto/helloworld.proto")?;
tonic_build::prost::compile_protos("proto/helloworld.proto")?;
Ok(())
}
```
Expand Down
4 changes: 2 additions & 2 deletions examples/routeguide-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ Create a `build.rs` file at the root of your crate:

```rust
fn main() {
tonic_build::compile_protos("proto/route_guide.proto")
tonic_build::prost::compile_protos("proto/route_guide.proto")
.unwrap_or_else(|e| panic!("Failed to compile protos {:?}", e));
}
```
Expand Down Expand Up @@ -823,7 +823,7 @@ opposed to at build time, placing the resulting modules wherever we need them.

```rust
fn main() {
tonic_build::configure()
tonic_build::prost::configure()
.build_client(false)
.out_dir("another_crate/src/pb")
.compile(&["path/my_proto.proto"], &["path"])
Expand Down
2 changes: 1 addition & 1 deletion interop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ tracing-subscriber = "0.2.0-alpha"
tracing-log = "0.1.0"

[build-dependencies]
tonic-build = { path = "../tonic-build" }
tonic-build = { path = "../tonic-build", features=["prost"] }
2 changes: 1 addition & 1 deletion interop/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fn main() {
let proto = "proto/grpc/testing/test.proto";

tonic_build::compile_protos(proto).unwrap();
tonic_build::prost::compile_protos(proto).unwrap();

// prevent needing to rebuild if files (or deps) haven't changed
println!("cargo:rerun-if-changed={}", proto);
Expand Down
2 changes: 1 addition & 1 deletion tests/extern_path/my_application/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() -> Result<(), std::io::Error> {
tonic_build::configure()
tonic_build::prost::configure()
.build_server(false)
.build_client(true)
.extern_path(".uuid", "::uuid")
Expand Down
2 changes: 1 addition & 1 deletion tests/included_service/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() {
tonic_build::compile_protos("proto/includer.proto").unwrap();
tonic_build::prost::compile_protos("proto/includer.proto").unwrap();
}
2 changes: 1 addition & 1 deletion tests/same_name/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() {
tonic_build::compile_protos("proto/foo.proto").unwrap();
tonic_build::prost::compile_protos("proto/foo.proto").unwrap();
}
2 changes: 1 addition & 1 deletion tests/wellknown/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() {
tonic_build::compile_protos("proto/wellknown.proto").unwrap();
tonic_build::prost::compile_protos("proto/wellknown.proto").unwrap();
}
5 changes: 3 additions & 2 deletions tonic-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ keywords = ["rpc", "grpc", "async", "codegen", "protobuf"]


[dependencies]
prost-build = "0.6"
prost-build = { version = "0.6", optional = true }
syn = "1.0"
quote = "1.0"
proc-macro2 = "1.0"

[features]
default = ["transport", "rustfmt"]
default = ["transport", "rustfmt", "prost"]
rustfmt = []
transport = []
prost = ["prost-build"]

[package.metadata.docs.rs]
all-features = true
4 changes: 2 additions & 2 deletions tonic-build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ tonic-build = <tonic-version>

```rust
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::compile_protos("proto/service.proto")?;
tonic_build::prost::compile_protos("proto/service.proto")?;
Ok(())
}
```
Expand All @@ -32,7 +32,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

```rust
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::configure()
tonic_build::prost::configure()
.build_server(false)
.compile(
&["proto/helloworld/helloworld.proto"],
Expand Down
87 changes: 56 additions & 31 deletions tonic-build/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use super::schema::{Context, Method, Service};
use crate::{generate_doc_comments, naive_snake_case};
use proc_macro2::TokenStream;
use prost_build::{Method, Service};
use quote::{format_ident, quote};

pub(crate) fn generate(service: &Service, proto: &str) -> TokenStream {
let service_ident = quote::format_ident!("{}Client", service.name);
let client_mod = quote::format_ident!("{}_client", naive_snake_case(&service.name));
let methods = generate_methods(service, proto);
/// Generate service for client
pub fn generate<'a, T: Service<'a>>(service: &'a T, context: &T::Context) -> TokenStream {
let service_ident = quote::format_ident!("{}Client", service.name());
let client_mod = quote::format_ident!("{}_client", naive_snake_case(&service.name()));
let methods = generate_methods(service, context);

let connect = generate_connect(&service_ident);
let service_doc = generate_doc_comments(&service.comments.leading);
let service_doc = generate_doc_comments(service.comment());

quote! {
/// Generated client implementations.
Expand Down Expand Up @@ -75,22 +76,26 @@ fn generate_connect(_service_ident: &syn::Ident) -> TokenStream {
TokenStream::new()
}

fn generate_methods(service: &Service, proto: &str) -> TokenStream {
fn generate_methods<'a, T: Service<'a>>(service: &'a T, context: &T::Context) -> TokenStream {
let mut stream = TokenStream::new();

for method in &service.methods {
for method in service.methods() {
use super::schema::Commentable;

let path = format!(
"/{}.{}/{}",
service.package, service.proto_name, method.proto_name
service.package(),
service.identifier(),
method.identifier()
);

stream.extend(generate_doc_comments(&method.comments.leading));
stream.extend(generate_doc_comments(method.comment()));

let method = match (method.client_streaming, method.server_streaming) {
(false, false) => generate_unary(method, &proto, path),
(false, true) => generate_server_streaming(method, &proto, path),
(true, false) => generate_client_streaming(method, &proto, path),
(true, true) => generate_streaming(method, &proto, path),
let method = match (method.client_streaming(), method.server_streaming()) {
(false, false) => generate_unary(method, &context, path),
(false, true) => generate_server_streaming(method, &context, path),
(true, false) => generate_client_streaming(method, &context, path),
(true, true) => generate_streaming(method, &context, path),
};

stream.extend(method);
Expand All @@ -99,9 +104,14 @@ fn generate_methods(service: &Service, proto: &str) -> TokenStream {
stream
}

fn generate_unary(method: &Method, proto: &str, path: String) -> TokenStream {
let ident = format_ident!("{}", method.name);
let (request, response) = crate::replace_wellknown(proto, &method);
fn generate_unary<'a, T: Method<'a>>(
method: &T,
context: &T::Context,
path: String,
) -> TokenStream {
let codec_name = syn::parse_str::<syn::Path>(context.codec_name()).unwrap();
let ident = format_ident!("{}", method.name());
let (request, response) = method.request_response_name(context);

quote! {
pub async fn #ident(
Expand All @@ -111,17 +121,22 @@ fn generate_unary(method: &Method, proto: &str, path: String) -> TokenStream {
self.inner.ready().await.map_err(|e| {
tonic::Status::new(tonic::Code::Unknown, format!("Service was not ready: {}", e.into()))
})?;
let codec = tonic::codec::ProstCodec::default();
let codec = #codec_name::default();
let path = http::uri::PathAndQuery::from_static(#path);
self.inner.unary(request.into_request(), path, codec).await
}
}
}

fn generate_server_streaming(method: &Method, proto: &str, path: String) -> TokenStream {
let ident = format_ident!("{}", method.name);
fn generate_server_streaming<'a, T: Method<'a>>(
method: &T,
context: &T::Context,
path: String,
) -> TokenStream {
let codec_name = syn::parse_str::<syn::Path>(context.codec_name()).unwrap();
let ident = format_ident!("{}", method.name());

let (request, response) = crate::replace_wellknown(proto, &method);
let (request, response) = method.request_response_name(context);

quote! {
pub async fn #ident(
Expand All @@ -131,17 +146,22 @@ fn generate_server_streaming(method: &Method, proto: &str, path: String) -> Toke
self.inner.ready().await.map_err(|e| {
tonic::Status::new(tonic::Code::Unknown, format!("Service was not ready: {}", e.into()))
})?;
let codec = tonic::codec::ProstCodec::default();
let codec = #codec_name::default();
let path = http::uri::PathAndQuery::from_static(#path);
self.inner.server_streaming(request.into_request(), path, codec).await
}
}
}

fn generate_client_streaming(method: &Method, proto: &str, path: String) -> TokenStream {
let ident = format_ident!("{}", method.name);
fn generate_client_streaming<'a, T: Method<'a>>(
method: &T,
context: &T::Context,
path: String,
) -> TokenStream {
let codec_name = syn::parse_str::<syn::Path>(context.codec_name()).unwrap();
let ident = format_ident!("{}", method.name());

let (request, response) = crate::replace_wellknown(proto, &method);
let (request, response) = method.request_response_name(context);

quote! {
pub async fn #ident(
Expand All @@ -151,17 +171,22 @@ fn generate_client_streaming(method: &Method, proto: &str, path: String) -> Toke
self.inner.ready().await.map_err(|e| {
tonic::Status::new(tonic::Code::Unknown, format!("Service was not ready: {}", e.into()))
})?;
let codec = tonic::codec::ProstCodec::default();
let codec = #codec_name::default();
let path = http::uri::PathAndQuery::from_static(#path);
self.inner.client_streaming(request.into_streaming_request(), path, codec).await
}
}
}

fn generate_streaming(method: &Method, proto: &str, path: String) -> TokenStream {
let ident = format_ident!("{}", method.name);
fn generate_streaming<'a, T: Method<'a>>(
method: &T,
context: &T::Context,
path: String,
) -> TokenStream {
let codec_name = syn::parse_str::<syn::Path>(context.codec_name()).unwrap();
let ident = format_ident!("{}", method.name());

let (request, response) = crate::replace_wellknown(proto, &method);
let (request, response) = method.request_response_name(context);

quote! {
pub async fn #ident(
Expand All @@ -171,7 +196,7 @@ fn generate_streaming(method: &Method, proto: &str, path: String) -> TokenStream
self.inner.ready().await.map_err(|e| {
tonic::Status::new(tonic::Code::Unknown, format!("Service was not ready: {}", e.into()))
})?;
let codec = tonic::codec::ProstCodec::default();
let codec = #codec_name::default();
let path = http::uri::PathAndQuery::from_static(#path);
self.inner.streaming(request.into_streaming_request(), path, codec).await
}
Expand Down
Loading

0 comments on commit f65cda1

Please sign in to comment.