Skip to content

Commit baa5278

Browse files
committed
hacky debug info
1 parent 8b3c6e1 commit baa5278

File tree

1 file changed

+44
-12
lines changed

1 file changed

+44
-12
lines changed

crates/twirp-build/src/lib.rs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,41 @@ pub fn service_generator() -> Box<ServiceGenerator> {
1212
Box::new(ServiceGenerator {})
1313
}
1414

15-
pub struct ServiceGenerator;
15+
struct MethodTypes {
16+
input_type: TokenStream,
17+
output_type: TokenStream,
18+
}
19+
20+
impl MethodTypes {
21+
fn from_prost(m: &prost_build::Method) -> Self {
22+
let as_type = |s| -> TokenStream {
23+
let Ok(typ) = syn::parse_str::<syn::Type>(s) else {
24+
panic!(
25+
"twirp-build generated invalid Rust. this is a bug in twirp-build, please file an issue:\n
26+
method={name}
27+
input_type={input_type}
28+
output_type={output_type}
29+
",
30+
name = m.name,
31+
input_type = m.input_type,
32+
output_type = m.output_type,
33+
);
34+
};
35+
typ.to_token_stream()
36+
};
37+
38+
let input_type = as_type(&m.input_type);
39+
let output_type = as_type(&m.output_type);
1640

17-
fn as_path(s: &str) -> TokenStream {
18-
syn::parse_str::<syn::Path>(s)
19-
.expect("twirp-build generated invalid Rust. this is a bug in twirp-build, please file an issue")
20-
.to_token_stream()
41+
Self {
42+
input_type,
43+
output_type,
44+
}
45+
}
2146
}
2247

48+
pub struct ServiceGenerator;
49+
2350
impl prost_build::ServiceGenerator for ServiceGenerator {
2451
fn generate(&mut self, service: prost_build::Service, buf: &mut String) {
2552
let service_name = format_ident!("{}", &service.name);
@@ -30,8 +57,10 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
3057
let mut proxy_methods = Vec::with_capacity(service.methods.len());
3158
for m in &service.methods {
3259
let name = format_ident!("{}", &m.name);
33-
let input_type = as_path(&m.input_type);
34-
let output_type = as_path(&m.output_type);
60+
let MethodTypes {
61+
input_type,
62+
output_type,
63+
} = MethodTypes::from_prost(m);
3564

3665
trait_methods.push(quote! {
3766
async fn #name(&self, ctx: twirp::Context, req: #input_type) -> Result<#output_type, Self::Error>;
@@ -68,9 +97,9 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
6897
for m in &service.methods {
6998
let name = format_ident!("{}", &m.name);
7099
let uri = format!("/{}", &m.proto_name);
71-
let req_type = as_path(&m.input_type);
100+
let MethodTypes { input_type, .. } = MethodTypes::from_prost(&m);
72101
route_calls.push(quote! {
73-
.route(#uri, |api: T, ctx: twirp::Context, req: #req_type| async move {
102+
.route(#uri, |api: T, ctx: twirp::Context, req: #input_type| async move {
74103
api.#name(ctx, req).await
75104
})
76105
});
@@ -96,8 +125,10 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
96125
let mut client_methods = Vec::with_capacity(service.methods.len());
97126
for m in &service.methods {
98127
let name = format_ident!("{}", &m.name);
99-
let input_type = as_path(&m.input_type);
100-
let output_type = as_path(&m.output_type);
128+
let MethodTypes {
129+
input_type,
130+
output_type,
131+
} = MethodTypes::from_prost(&m);
101132

102133
client_trait_methods.push(quote! {
103134
async fn #name(&self, req: #input_type) -> Result<#output_type, twirp::ClientError>;
@@ -137,7 +168,8 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
137168
#client_trait
138169
};
139170

140-
let ast: syn::File = syn::parse2(generated).expect("generated an invalid token stream");
171+
let ast: syn::File = syn::parse2(generated)
172+
.expect("twirp-build generated invalid Rust. this is a bug in twirp-build, please file an issue");
141173
let code = prettyplease::unparse(&ast);
142174
buf.push_str(&code);
143175
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy