23
23
//! .expect("Gen async code failed.");
24
24
//! }
25
25
26
- pub use protobuf_codegen:: Customize as ProtobufCustomize ;
26
+ pub use protobuf_codegen:: {
27
+ Customize as ProtobufCustomize , CustomizeCallback as ProtobufCustomizeCallback ,
28
+ } ;
27
29
use std:: collections:: HashMap ;
28
30
use std:: error:: Error ;
29
31
use std:: fmt;
@@ -50,8 +52,8 @@ pub struct Codegen {
50
52
inputs : Vec < PathBuf > ,
51
53
/// Generate rust-protobuf files along with rust-gprc
52
54
rust_protobuf : bool ,
53
- /// Customize rust- protobuf codegen
54
- pub rust_protobuf_customize : ProtobufCustomize ,
55
+ /// rust protobuf codegen
56
+ rust_protobuf_codegen : protobuf_codegen :: Codegen ,
55
57
/// Customize code generation
56
58
customize : Customize ,
57
59
}
@@ -102,9 +104,18 @@ impl Codegen {
102
104
self
103
105
}
104
106
105
- /// Customize code generated by rust-protobuf.
107
+ /// Customize code generated by rust-protobuf-codegen .
106
108
pub fn rust_protobuf_customize ( & mut self , customize : ProtobufCustomize ) -> & mut Self {
107
- self . rust_protobuf_customize = customize;
109
+ self . rust_protobuf_codegen . customize ( customize) ;
110
+ self
111
+ }
112
+
113
+ /// Callback for dynamic per-element customization of rust-protobuf-codegen.
114
+ pub fn rust_protobuf_customize_callback (
115
+ & mut self ,
116
+ customize : impl ProtobufCustomizeCallback ,
117
+ ) -> & mut Self {
118
+ self . rust_protobuf_codegen . customize_callback ( customize) ;
108
119
self
109
120
}
110
121
@@ -116,18 +127,17 @@ impl Codegen {
116
127
117
128
/// Like `protoc --rust_out=...` but without requiring `protoc` or `protoc-gen-rust`
118
129
/// commands in `$PATH`.
119
- pub fn run ( & self ) -> io:: Result < ( ) > {
130
+ pub fn run ( & mut self ) -> io:: Result < ( ) > {
120
131
let includes: Vec < & Path > = self . includes . iter ( ) . map ( |p| p. as_path ( ) ) . collect ( ) ;
121
132
let inputs: Vec < & Path > = self . inputs . iter ( ) . map ( |p| p. as_path ( ) ) . collect ( ) ;
122
133
let p = parse_and_typecheck ( & includes, & inputs) ?;
123
134
124
135
if self . rust_protobuf {
125
- protobuf_codegen :: Codegen :: new ( )
136
+ self . rust_protobuf_codegen
126
137
. pure ( )
127
138
. out_dir ( & self . out_dir )
128
139
. inputs ( & self . inputs )
129
140
. includes ( & self . includes )
130
- . customize ( self . rust_protobuf_customize . clone ( ) )
131
141
. run ( )
132
142
. expect ( "Gen rust protobuf failed." ) ;
133
143
}
0 commit comments