Skip to content

Commit

Permalink
Build: Moves gRPC from a feature to an option
Browse files Browse the repository at this point in the history
  • Loading branch information
Tpt committed Dec 23, 2024
1 parent 7e741ed commit 8c71c6b
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 7 deletions.
3 changes: 0 additions & 3 deletions build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ authors.workspace = true
edition.workspace = true
license.workspace = true

[features]
grpc = []

[dependencies]
regex.workspace = true
prost-build.workspace = true
Expand Down
16 changes: 14 additions & 2 deletions build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -179,6 +185,7 @@ impl TwirpBuilder {
struct TwirpServiceGenerator {
client: bool,
server: bool,
grpc: bool,
request_extractors: Vec<(String, String)>,
}

Expand All @@ -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<String>,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -353,7 +365,7 @@ impl TwirpServiceGenerator {
writeln!(buf, " .build()")?;
writeln!(buf, " }}")?;

if cfg!(feature = "grpc") {
if self.grpc {
writeln!(buf)?;
writeln!(
buf,
Expand Down
2 changes: 1 addition & 1 deletion example/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions example/server/build.rs
Original file line number Diff line number Diff line change
@@ -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"], &[".."])
}
2 changes: 1 addition & 1 deletion integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions integration/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"], &["."])?;

Expand Down

0 comments on commit 8c71c6b

Please sign in to comment.