1
- use crate :: code_gen:: CodeGenBuilder ;
1
+ use crate :: { code_gen:: CodeGenBuilder , compile_settings :: CompileSettings } ;
2
2
3
3
use super :: Attributes ;
4
4
use proc_macro2:: TokenStream ;
@@ -41,6 +41,7 @@ pub fn configure() -> Builder {
41
41
disable_comments : HashSet :: default ( ) ,
42
42
use_arc_self : false ,
43
43
generate_default_stubs : false ,
44
+ compile_settings : CompileSettings :: default ( ) ,
44
45
}
45
46
}
46
47
@@ -61,61 +62,98 @@ pub fn compile_protos(proto: impl AsRef<Path>) -> io::Result<()> {
61
62
Ok ( ( ) )
62
63
}
63
64
64
- const PROST_CODEC_PATH : & str = "tonic::codec::ProstCodec" ;
65
-
66
65
/// Non-path Rust types allowed for request/response types.
67
66
const NON_PATH_TYPE_ALLOWLIST : & [ & str ] = & [ "()" ] ;
68
67
69
- impl crate :: Service for Service {
70
- type Method = Method ;
68
+ /// Newtype wrapper for prost to add tonic-specific extensions
69
+ struct TonicBuildService {
70
+ prost_service : Service ,
71
+ methods : Vec < TonicBuildMethod > ,
72
+ }
73
+
74
+ impl TonicBuildService {
75
+ fn new ( prost_service : Service , settings : CompileSettings ) -> Self {
76
+ Self {
77
+ // CompileSettings are currently only consumed method-by-method but if you need them in the Service, here's your spot.
78
+ // The tonic_build::Service trait specifies that methods are borrowed, so they have to reified up front.
79
+ methods : prost_service
80
+ . methods
81
+ . iter ( )
82
+ . map ( |prost_method| TonicBuildMethod {
83
+ prost_method : prost_method. clone ( ) ,
84
+ settings : settings. clone ( ) ,
85
+ } )
86
+ . collect ( ) ,
87
+ prost_service,
88
+ }
89
+ }
90
+ }
91
+
92
+ /// Newtype wrapper for prost to add tonic-specific extensions
93
+ struct TonicBuildMethod {
94
+ prost_method : Method ,
95
+ settings : CompileSettings ,
96
+ }
97
+
98
+ impl crate :: Service for TonicBuildService {
99
+ type Method = TonicBuildMethod ;
71
100
type Comment = String ;
72
101
73
102
fn name ( & self ) -> & str {
74
- & self . name
103
+ & self . prost_service . name
75
104
}
76
105
77
106
fn package ( & self ) -> & str {
78
- & self . package
107
+ & self . prost_service . package
79
108
}
80
109
81
110
fn identifier ( & self ) -> & str {
82
- & self . proto_name
111
+ & self . prost_service . proto_name
83
112
}
84
113
85
114
fn comment ( & self ) -> & [ Self :: Comment ] {
86
- & self . comments . leading [ ..]
115
+ & self . prost_service . comments . leading [ ..]
87
116
}
88
117
89
118
fn methods ( & self ) -> & [ Self :: Method ] {
90
- & self . methods [ .. ]
119
+ & self . methods
91
120
}
92
121
}
93
122
94
- impl crate :: Method for Method {
123
+ impl crate :: Method for TonicBuildMethod {
95
124
type Comment = String ;
96
125
97
126
fn name ( & self ) -> & str {
98
- & self . name
127
+ & self . prost_method . name
99
128
}
100
129
101
130
fn identifier ( & self ) -> & str {
102
- & self . proto_name
131
+ & self . prost_method . proto_name
103
132
}
104
133
134
+ /// For code generation, you can override the codec.
135
+ ///
136
+ /// You should set the codec path to an import path that has a free
137
+ /// function like `fn default()`. The default value is tonic::codec::ProstCodec,
138
+ /// which returns a default-configured ProstCodec. You may wish to configure
139
+ /// the codec, e.g., with a buffer configuration.
140
+ ///
141
+ /// Though ProstCodec implements Default, it is currently only required that
142
+ /// the function match the Default trait's function spec.
105
143
fn codec_path ( & self ) -> & str {
106
- PROST_CODEC_PATH
144
+ & self . settings . codec_path
107
145
}
108
146
109
147
fn client_streaming ( & self ) -> bool {
110
- self . client_streaming
148
+ self . prost_method . client_streaming
111
149
}
112
150
113
151
fn server_streaming ( & self ) -> bool {
114
- self . server_streaming
152
+ self . prost_method . server_streaming
115
153
}
116
154
117
155
fn comment ( & self ) -> & [ Self :: Comment ] {
118
- & self . comments . leading [ ..]
156
+ & self . prost_method . comments . leading [ ..]
119
157
}
120
158
121
159
fn request_response_name (
@@ -140,8 +178,14 @@ impl crate::Method for Method {
140
178
}
141
179
} ;
142
180
143
- let request = convert_type ( & self . input_proto_type , & self . input_type ) ;
144
- let response = convert_type ( & self . output_proto_type , & self . output_type ) ;
181
+ let request = convert_type (
182
+ & self . prost_method . input_proto_type ,
183
+ & self . prost_method . input_type ,
184
+ ) ;
185
+ let response = convert_type (
186
+ & self . prost_method . output_proto_type ,
187
+ & self . prost_method . output_type ,
188
+ ) ;
145
189
( request, response)
146
190
}
147
191
}
@@ -176,7 +220,10 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
176
220
. disable_comments ( self . builder . disable_comments . clone ( ) )
177
221
. use_arc_self ( self . builder . use_arc_self )
178
222
. generate_default_stubs ( self . builder . generate_default_stubs )
179
- . generate_server ( & service, & self . builder . proto_path ) ;
223
+ . generate_server (
224
+ & TonicBuildService :: new ( service. clone ( ) , self . builder . compile_settings . clone ( ) ) ,
225
+ & self . builder . proto_path ,
226
+ ) ;
180
227
181
228
self . servers . extend ( server) ;
182
229
}
@@ -188,7 +235,10 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
188
235
. attributes ( self . builder . client_attributes . clone ( ) )
189
236
. disable_comments ( self . builder . disable_comments . clone ( ) )
190
237
. build_transport ( self . builder . build_transport )
191
- . generate_client ( & service, & self . builder . proto_path ) ;
238
+ . generate_client (
239
+ & TonicBuildService :: new ( service, self . builder . compile_settings . clone ( ) ) ,
240
+ & self . builder . proto_path ,
241
+ ) ;
192
242
193
243
self . clients . extend ( client) ;
194
244
}
@@ -252,6 +302,7 @@ pub struct Builder {
252
302
pub ( crate ) disable_comments : HashSet < String > ,
253
303
pub ( crate ) use_arc_self : bool ,
254
304
pub ( crate ) generate_default_stubs : bool ,
305
+ pub ( crate ) compile_settings : CompileSettings ,
255
306
256
307
out_dir : Option < PathBuf > ,
257
308
}
@@ -524,6 +575,16 @@ impl Builder {
524
575
self
525
576
}
526
577
578
+ /// Override the default codec.
579
+ ///
580
+ /// If set, writes `{codec_path}::default()` in generated code wherever a codec is created.
581
+ ///
582
+ /// This defaults to `"tonic::codec::ProstCodec"`
583
+ pub fn codec_path ( mut self , codec_path : impl Into < String > ) -> Self {
584
+ self . compile_settings . codec_path = codec_path. into ( ) ;
585
+ self
586
+ }
587
+
527
588
/// Compile the .proto files and execute code generation.
528
589
pub fn compile (
529
590
self ,
0 commit comments