diff --git a/build/Cargo.toml b/build/Cargo.toml index a23be06..f7fb8d9 100644 --- a/build/Cargo.toml +++ b/build/Cargo.toml @@ -8,9 +8,6 @@ authors.workspace = true edition.workspace = true license.workspace = true -[features] -grpc = [] - [dependencies] regex.workspace = true prost-build.workspace = true diff --git a/build/src/lib.rs b/build/src/lib.rs index d1a6597..3d9d2b2 100644 --- a/build/src/lib.rs +++ b/build/src/lib.rs @@ -48,6 +48,12 @@ impl TwirpBuilder { self } + /// Generates code for gRPC alongside Twirp. + pub fn with_grpc(mut self) -> Self { + self.generator = self.generator.with_grpc(); + self + } + /// Adds an extra parameter to generated server methods that implements [`axum::FromRequestParts`](https://docs.rs/axum/latest/axum/extract/trait.FromRequestParts.html). /// /// For example @@ -179,6 +185,7 @@ impl TwirpBuilder { struct TwirpServiceGenerator { client: bool, server: bool, + grpc: bool, request_extractors: Vec<(String, String)>, } @@ -197,6 +204,11 @@ impl TwirpServiceGenerator { self } + pub fn with_grpc(mut self) -> Self { + self.grpc = true; + self + } + pub fn with_axum_request_extractor( mut self, name: impl Into, @@ -278,7 +290,7 @@ impl TwirpServiceGenerator { writeln!(buf, "#[::twurst_server::codegen::trait_variant_make(Send)]")?; writeln!(buf, "pub trait {} {{", service.name)?; for method in &service.methods { - if !cfg!(feature = "grpc") && (method.client_streaming || method.server_streaming) { + if !self.grpc && (method.client_streaming || method.server_streaming) { continue; // No streaming } for comment in &method.comments.leading { @@ -353,7 +365,7 @@ impl TwirpServiceGenerator { writeln!(buf, " .build()")?; writeln!(buf, " }}")?; - if cfg!(feature = "grpc") { + if self.grpc { writeln!(buf)?; writeln!( buf, diff --git a/example/server/Cargo.toml b/example/server/Cargo.toml index 1c15f38..9128faf 100644 --- a/example/server/Cargo.toml +++ b/example/server/Cargo.toml @@ -16,4 +16,4 @@ tokio = { workspace = true, features = ["rt-multi-thread"] } tower-http = { workspace = true, features = ["cors"] } [build-dependencies] -twurst-build = { path = "../../build", features = ["grpc"] } +twurst-build.path = "../../build" diff --git a/example/server/build.rs b/example/server/build.rs index 3e6cf72..e336b66 100644 --- a/example/server/build.rs +++ b/example/server/build.rs @@ -1,6 +1,7 @@ fn main() -> std::io::Result<()> { twurst_build::TwirpBuilder::new() .with_server() + .with_grpc() .with_axum_request_extractor("headers", "::axum::http::HeaderMap") .compile_protos(&["../example_service.proto"], &[".."]) } diff --git a/integration/Cargo.toml b/integration/Cargo.toml index baa15e9..596eb05 100644 --- a/integration/Cargo.toml +++ b/integration/Cargo.toml @@ -21,5 +21,5 @@ tower.workspace = true tower-http = { workspace = true, features = ["auth", "cors"] } [build-dependencies] -twurst-build = { path = "../build", features = ["grpc"] } +twurst-build.path = "../build" tonic-build.workspace = true diff --git a/integration/build.rs b/integration/build.rs index ecce77d..b4bdecf 100644 --- a/integration/build.rs +++ b/integration/build.rs @@ -5,6 +5,7 @@ fn main() -> std::io::Result<()> { twurst_build::TwirpBuilder::new() .with_client() .with_server() + .with_grpc() .with_axum_request_extractor("bearer_token", "crate::server::ExtractBearerToken") .compile_protos(&["integration.proto"], &["."])?;