@@ -12,14 +12,41 @@ pub fn service_generator() -> Box<ServiceGenerator> {
12
12
Box :: new ( ServiceGenerator { } )
13
13
}
14
14
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 ) ;
16
40
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
+ }
21
46
}
22
47
48
+ pub struct ServiceGenerator ;
49
+
23
50
impl prost_build:: ServiceGenerator for ServiceGenerator {
24
51
fn generate ( & mut self , service : prost_build:: Service , buf : & mut String ) {
25
52
let service_name = format_ident ! ( "{}" , & service. name) ;
@@ -30,8 +57,10 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
30
57
let mut proxy_methods = Vec :: with_capacity ( service. methods . len ( ) ) ;
31
58
for m in & service. methods {
32
59
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) ;
35
64
36
65
trait_methods. push ( quote ! {
37
66
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 {
68
97
for m in & service. methods {
69
98
let name = format_ident ! ( "{}" , & m. name) ;
70
99
let uri = format ! ( "/{}" , & m. proto_name) ;
71
- let req_type = as_path ( & m. input_type ) ;
100
+ let MethodTypes { input_type , .. } = MethodTypes :: from_prost ( & m) ;
72
101
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 {
74
103
api. #name( ctx, req) . await
75
104
} )
76
105
} ) ;
@@ -96,8 +125,10 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
96
125
let mut client_methods = Vec :: with_capacity ( service. methods . len ( ) ) ;
97
126
for m in & service. methods {
98
127
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) ;
101
132
102
133
client_trait_methods. push ( quote ! {
103
134
async fn #name( & self , req: #input_type) -> Result <#output_type, twirp:: ClientError >;
@@ -137,7 +168,8 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
137
168
#client_trait
138
169
} ;
139
170
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" ) ;
141
173
let code = prettyplease:: unparse ( & ast) ;
142
174
buf. push_str ( & code) ;
143
175
}
0 commit comments