Skip to content

Commit 2e082f8

Browse files
authored
chore(build): Refactor codegen traits (#302)
Signed-off-by: Lucio Franco <luciofranco14@gmail.com>
1 parent 012fa3c commit 2e082f8

File tree

13 files changed

+113
-144
lines changed

13 files changed

+113
-144
lines changed

examples/build.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fn main() {
2-
tonic_build::prost::compile_protos("proto/helloworld/helloworld.proto").unwrap();
3-
tonic_build::prost::compile_protos("proto/routeguide/route_guide.proto").unwrap();
4-
tonic_build::prost::compile_protos("proto/echo/echo.proto").unwrap();
5-
tonic_build::prost::compile_protos("proto/google/pubsub/pubsub.proto").unwrap();
2+
tonic_build::compile_protos("proto/helloworld/helloworld.proto").unwrap();
3+
tonic_build::compile_protos("proto/routeguide/route_guide.proto").unwrap();
4+
tonic_build::compile_protos("proto/echo/echo.proto").unwrap();
5+
tonic_build::compile_protos("proto/google/pubsub/pubsub.proto").unwrap();
66
}

examples/routeguide-tutorial.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ opposed to at build time, placing the resulting modules wherever we need them.
823823

824824
```rust
825825
fn main() {
826-
tonic_build::prost::configure()
826+
tonic_build::configure()
827827
.build_client(false)
828828
.out_dir("another_crate/src/pb")
829829
.compile(&["path/my_proto.proto"], &["path"])

interop/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
fn main() {
22
let proto = "proto/grpc/testing/test.proto";
33

4-
tonic_build::prost::compile_protos(proto).unwrap();
4+
tonic_build::compile_protos(proto).unwrap();
55

66
// prevent needing to rebuild if files (or deps) haven't changed
77
println!("cargo:rerun-if-changed={}", proto);

tests/extern_path/my_application/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() -> Result<(), std::io::Error> {
2-
tonic_build::prost::configure()
2+
tonic_build::configure()
33
.build_server(false)
44
.build_client(true)
55
.extern_path(".uuid", "::uuid")

tests/included_service/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
fn main() {
2-
tonic_build::prost::compile_protos("proto/includer.proto").unwrap();
2+
tonic_build::compile_protos("proto/includer.proto").unwrap();
33
}

tests/same_name/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
fn main() {
2-
tonic_build::prost::compile_protos("proto/foo.proto").unwrap();
2+
tonic_build::compile_protos("proto/foo.proto").unwrap();
33
}

tests/wellknown/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
fn main() {
2-
tonic_build::prost::compile_protos("proto/wellknown.proto").unwrap();
2+
tonic_build::compile_protos("proto/wellknown.proto").unwrap();
33
}

tonic-build/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ tonic-build = <tonic-version>
2323

2424
```rust
2525
fn main() -> Result<(), Box<dyn std::error::Error>> {
26-
tonic_build::prost::compile_protos("proto/service.proto")?;
26+
tonic_build::compile_protos("proto/service.proto")?;
2727
Ok(())
2828
}
2929
```
@@ -32,7 +32,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3232

3333
```rust
3434
fn main() -> Result<(), Box<dyn std::error::Error>> {
35-
tonic_build::prost::configure()
35+
tonic_build::configure()
3636
.build_server(false)
3737
.compile(
3838
&["proto/helloworld/helloworld.proto"],

tonic-build/src/client.rs

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use super::schema::{Context, Method, Service};
1+
use super::schema::{Method, Service};
22
use crate::{generate_doc_comments, naive_snake_case};
33
use proc_macro2::TokenStream;
44
use quote::{format_ident, quote};
55

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

1212
let connect = generate_connect(&service_ident);
1313
let service_doc = generate_doc_comments(service.comment());
@@ -76,7 +76,7 @@ fn generate_connect(_service_ident: &syn::Ident) -> TokenStream {
7676
TokenStream::new()
7777
}
7878

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

8282
for method in service.methods() {
@@ -92,10 +92,10 @@ fn generate_methods<'a, T: Service<'a>>(service: &'a T, context: &T::Context) ->
9292
stream.extend(generate_doc_comments(method.comment()));
9393

9494
let method = match (method.client_streaming(), method.server_streaming()) {
95-
(false, false) => generate_unary(method, &context, path),
96-
(false, true) => generate_server_streaming(method, &context, path),
97-
(true, false) => generate_client_streaming(method, &context, path),
98-
(true, true) => generate_streaming(method, &context, path),
95+
(false, false) => generate_unary(method, proto_path, path),
96+
(false, true) => generate_server_streaming(method, proto_path, path),
97+
(true, false) => generate_client_streaming(method, proto_path, path),
98+
(true, true) => generate_streaming(method, proto_path, path),
9999
};
100100

101101
stream.extend(method);
@@ -104,14 +104,10 @@ fn generate_methods<'a, T: Service<'a>>(service: &'a T, context: &T::Context) ->
104104
stream
105105
}
106106

107-
fn generate_unary<'a, T: Method<'a>>(
108-
method: &T,
109-
context: &T::Context,
110-
path: String,
111-
) -> TokenStream {
112-
let codec_name = syn::parse_str::<syn::Path>(context.codec_name()).unwrap();
107+
fn generate_unary<T: Method>(method: &T, proto_path: &str, path: String) -> TokenStream {
108+
let codec_name = syn::parse_str::<syn::Path>(T::CODEC_PATH).unwrap();
113109
let ident = format_ident!("{}", method.name());
114-
let (request, response) = method.request_response_name(context);
110+
let (request, response) = method.request_response_name(proto_path);
115111

116112
quote! {
117113
pub async fn #ident(
@@ -128,15 +124,11 @@ fn generate_unary<'a, T: Method<'a>>(
128124
}
129125
}
130126

131-
fn generate_server_streaming<'a, T: Method<'a>>(
132-
method: &T,
133-
context: &T::Context,
134-
path: String,
135-
) -> TokenStream {
136-
let codec_name = syn::parse_str::<syn::Path>(context.codec_name()).unwrap();
127+
fn generate_server_streaming<T: Method>(method: &T, proto_path: &str, path: String) -> TokenStream {
128+
let codec_name = syn::parse_str::<syn::Path>(T::CODEC_PATH).unwrap();
137129
let ident = format_ident!("{}", method.name());
138130

139-
let (request, response) = method.request_response_name(context);
131+
let (request, response) = method.request_response_name(proto_path);
140132

141133
quote! {
142134
pub async fn #ident(
@@ -153,15 +145,11 @@ fn generate_server_streaming<'a, T: Method<'a>>(
153145
}
154146
}
155147

156-
fn generate_client_streaming<'a, T: Method<'a>>(
157-
method: &T,
158-
context: &T::Context,
159-
path: String,
160-
) -> TokenStream {
161-
let codec_name = syn::parse_str::<syn::Path>(context.codec_name()).unwrap();
148+
fn generate_client_streaming<T: Method>(method: &T, proto_path: &str, path: String) -> TokenStream {
149+
let codec_name = syn::parse_str::<syn::Path>(T::CODEC_PATH).unwrap();
162150
let ident = format_ident!("{}", method.name());
163151

164-
let (request, response) = method.request_response_name(context);
152+
let (request, response) = method.request_response_name(proto_path);
165153

166154
quote! {
167155
pub async fn #ident(
@@ -178,15 +166,11 @@ fn generate_client_streaming<'a, T: Method<'a>>(
178166
}
179167
}
180168

181-
fn generate_streaming<'a, T: Method<'a>>(
182-
method: &T,
183-
context: &T::Context,
184-
path: String,
185-
) -> TokenStream {
186-
let codec_name = syn::parse_str::<syn::Path>(context.codec_name()).unwrap();
169+
fn generate_streaming<T: Method>(method: &T, proto_path: &str, path: String) -> TokenStream {
170+
let codec_name = syn::parse_str::<syn::Path>(T::CODEC_PATH).unwrap();
187171
let ident = format_ident!("{}", method.name());
188172

189-
let (request, response) = method.request_response_name(context);
173+
let (request, response) = method.request_response_name(proto_path);
190174

191175
quote! {
192176
pub async fn #ident(

tonic-build/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
//!
2424
//! ```rust,no_run
2525
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
26-
//! tonic_build::prost::compile_protos("proto/service.proto")?;
26+
//! tonic_build::compile_protos("proto/service.proto")?;
2727
//! Ok(())
2828
//! }
2929
//! ```
@@ -32,7 +32,7 @@
3232
//!
3333
//! ```rust,no_run
3434
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
35-
//! tonic_build::prost::configure()
35+
//! tonic_build::configure()
3636
//! .build_server(false)
3737
//! .compile(
3838
//! &["proto/helloworld/helloworld.proto"],
@@ -61,7 +61,11 @@ use quote::TokenStreamExt;
6161

6262
/// Prost generator
6363
#[cfg(feature = "prost")]
64-
pub mod prost;
64+
mod prost;
65+
66+
#[cfg(feature = "prost")]
67+
pub use prost::{compile_protos, configure, Builder};
68+
6569
/// Traits to describe schema
6670
pub mod schema;
6771

0 commit comments

Comments
 (0)