21
21
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
22
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
23
24
- use std:: collections:: HashMap ;
25
- use std:: io:: Write ;
26
-
24
+ #[ cfg( feature = "protobuf-codec" ) ]
27
25
use protobuf:: compiler_plugin;
26
+ #[ cfg( feature = "protobuf-codec" ) ]
28
27
use protobuf:: descriptor:: * ;
28
+ #[ cfg( feature = "protobuf-codec" ) ]
29
29
use protobuf:: descriptorx:: * ;
30
+ #[ cfg( feature = "protobuf-codec" ) ]
31
+ use std:: collections:: HashMap ;
32
+ #[ cfg( feature = "protobuf-codec" ) ]
33
+ use std:: io:: Write ;
30
34
31
35
#[ cfg( feature = "protobufv3-codec" ) ]
32
36
use protobuf_codegen;
33
37
38
+ #[ cfg( feature = "protobuf-codec" ) ]
34
39
struct CodeWriter < ' a > {
35
40
writer : & ' a mut ( dyn Write + ' a ) ,
36
41
indent : String ,
37
42
}
38
43
44
+ #[ cfg( feature = "protobuf-codec" ) ]
39
45
impl < ' a > CodeWriter < ' a > {
40
46
pub fn new ( writer : & ' a mut dyn Write ) -> CodeWriter < ' a > {
41
47
CodeWriter {
@@ -173,15 +179,18 @@ impl<'a> CodeWriter<'a> {
173
179
}
174
180
}
175
181
182
+ #[ cfg( feature = "protobuf-codec" ) ]
176
183
use super :: util:: { self , fq_grpc, to_snake_case, MethodType } ;
177
184
185
+ #[ cfg( feature = "protobuf-codec" ) ]
178
186
struct MethodGen < ' a > {
179
187
proto : & ' a MethodDescriptorProto ,
180
188
service_name : String ,
181
189
service_path : String ,
182
190
root_scope : & ' a RootScope < ' a > ,
183
191
}
184
192
193
+ #[ cfg( feature = "protobuf-codec" ) ]
185
194
impl < ' a > MethodGen < ' a > {
186
195
fn new (
187
196
proto : & ' a MethodDescriptorProto ,
@@ -241,16 +250,10 @@ impl<'a> MethodGen<'a> {
241
250
to_snake_case ( self . proto . get_name ( ) )
242
251
}
243
252
244
- #[ cfg( feature = "protobuf-codec" ) ]
245
253
fn fq_name ( & self ) -> String {
246
254
format ! ( "\" {}/{}\" " , self . service_path, & self . proto. get_name( ) )
247
255
}
248
256
249
- #[ cfg( feature = "protobufv3-codec" ) ]
250
- fn fq_name ( & self ) -> String {
251
- format ! ( "\" {}/{}\" " , self . service_path, & self . proto. name)
252
- }
253
-
254
257
fn const_method_name ( & self ) -> String {
255
258
format ! (
256
259
"METHOD_{}_{}" ,
@@ -541,6 +544,7 @@ impl<'a> MethodGen<'a> {
541
544
}
542
545
}
543
546
547
+ #[ cfg( feature = "protobuf-codec" ) ]
544
548
struct ServiceGen < ' a > {
545
549
proto : & ' a ServiceDescriptorProto ,
546
550
methods : Vec < MethodGen < ' a > > ,
@@ -555,15 +559,7 @@ fn service_path(proto: &ServiceDescriptorProto, file: &FileDescriptorProto) -> S
555
559
}
556
560
}
557
561
558
- #[ cfg( feature = "protobufv3-codec" ) ]
559
- fn service_path ( proto : & ' a ServiceDescriptorProto , file : & FileDescriptorProto ) -> String {
560
- if file. package . is_empty ( ) {
561
- format ! ( "/{}" , proto. name)
562
- } else {
563
- format ! ( "/{}.{}" , file. package, proto. name)
564
- } ;
565
- }
566
-
562
+ #[ cfg( feature = "protobuf-codec" ) ]
567
563
impl < ' a > ServiceGen < ' a > {
568
564
fn new (
569
565
proto : & ' a ServiceDescriptorProto ,
@@ -587,16 +583,10 @@ impl<'a> ServiceGen<'a> {
587
583
ServiceGen { proto, methods }
588
584
}
589
585
590
- #[ cfg( feature = "protobuf-codec" ) ]
591
586
fn service_name ( & self ) -> String {
592
587
util:: to_camel_case ( self . proto . get_name ( ) )
593
588
}
594
589
595
- #[ cfg( feature = "protobufv3-codec" ) ]
596
- fn service_name ( & self ) -> String {
597
- util:: to_camel_case ( self . proto . name )
598
- }
599
-
600
590
fn client_name ( & self ) -> String {
601
591
format ! ( "{}Client" , self . service_name( ) )
602
592
}
@@ -685,11 +675,7 @@ fn get_service(file: &FileDescriptorProto) -> &[ServiceDescriptorProto] {
685
675
file. get_service ( )
686
676
}
687
677
688
- #[ cfg( feature = "protobufv3-codec" ) ]
689
- fn get_service ( file : & FileDescriptorProto ) -> & [ ServiceDescriptorProto ] {
690
- file. service
691
- }
692
-
678
+ #[ cfg( feature = "protobuf-codec" ) ]
693
679
fn gen_file (
694
680
file : & FileDescriptorProto ,
695
681
root_scope : & RootScope ,
@@ -743,40 +729,21 @@ pub fn gen(
743
729
}
744
730
745
731
#[ cfg( feature = "protobufv3-codec" ) ]
746
- pub fn gen (
747
- file_descriptors : & [ FileDescriptorProto ] ,
748
- files_to_generate : & [ String ] ,
749
- ) -> Vec < compiler_plugin:: GenResult > {
750
- let files_map: HashMap < & str , & FileDescriptorProto > =
751
- file_descriptors. iter ( ) . map ( |f| ( f. get_name ( ) , f) ) . collect ( ) ;
752
-
732
+ pub fn gen ( input_files : & [ String ] ) -> anyhow:: Result < ( ) > {
753
733
let res = protobuf_codegen:: Codegen :: new ( )
754
734
. protoc ( )
755
735
// All inputs and imports from the inputs must reside in `includes` directories.
756
- . includes ( & [ "src/protos" ] )
736
+ . includes ( [ "src/protos" ] )
757
737
// Inputs must reside in some of include paths.
758
- . input ( file_descriptors )
738
+ . inputs ( input_files )
759
739
// Specify output directory relative to Cargo soutput directory.
760
740
. cargo_out_dir ( "protos" )
761
741
. run ( ) ;
762
742
763
- let root_scope = RootScope { file_descriptors } ;
764
-
765
- let mut results = Vec :: new ( ) ;
766
-
767
- for file_name in files_to_generate {
768
- let file = files_map[ & file_name[ ..] ] ;
769
-
770
- if file. get_service ( ) . is_empty ( ) {
771
- continue ;
772
- }
773
-
774
- results. extend ( gen_file ( file, & root_scope) . into_iter ( ) ) ;
775
- }
776
-
777
- results
743
+ res
778
744
}
779
745
746
+ #[ cfg( feature = "protobuf-codec" ) ]
780
747
pub fn protoc_gen_grpc_rust_main ( ) {
781
748
compiler_plugin:: plugin_main ( gen) ;
782
749
}
0 commit comments