File tree Expand file tree Collapse file tree 6 files changed +39
-33
lines changed Expand file tree Collapse file tree 6 files changed +39
-33
lines changed Original file line number Diff line number Diff line change @@ -6,5 +6,4 @@ Cargo.lock
66.idea
77* .o
88example /protocols /** /* .rs
9- ! example /protocols /** /mod.rs
109src /ttrpc.rs
Original file line number Diff line number Diff line change 3636
3737#![ allow( dead_code) ]
3838
39- use std:: collections:: HashMap ;
39+ use std:: {
40+ collections:: { HashMap , HashSet } ,
41+ fs,
42+ io:: BufRead ,
43+ } ;
4044
4145use crate :: Customize ;
4246use protobuf:: {
@@ -723,6 +727,31 @@ pub fn gen_and_write(
723727) -> io:: Result < ( ) > {
724728 let results = gen ( file_descriptors, files_to_generate, customize) ;
725729
730+ if customize. gen_mod {
731+ let file_path = out_dir. join ( "mod.rs" ) ;
732+ let mut set = HashSet :: new ( ) ;
733+ //if mod file exists
734+ if let Ok ( file) = File :: open ( & file_path) {
735+ let reader = io:: BufReader :: new ( file) ;
736+ reader. lines ( ) . for_each ( |line| {
737+ let _ = line. map ( |r| set. insert ( r) ) ;
738+ } ) ;
739+ }
740+ let mut file_write = fs:: OpenOptions :: new ( )
741+ . create ( true )
742+ . write ( true )
743+ . truncate ( true )
744+ . open ( & file_path) ?;
745+ for r in & results {
746+ let prefix_name: Vec < & str > = r. name . split ( '.' ) . collect ( ) ;
747+ set. insert ( format ! ( "pub mod {};" , prefix_name[ 0 ] ) ) ;
748+ }
749+ for item in & set {
750+ writeln ! ( file_write, "{}" , item) ?;
751+ }
752+ file_write. flush ( ) ?;
753+ }
754+
726755 for r in & results {
727756 let mut file_path = out_dir. to_owned ( ) ;
728757 file_path. push ( & r. name ) ;
Original file line number Diff line number Diff line change @@ -36,4 +36,6 @@ pub struct Customize {
3636 pub async_client : bool ,
3737 /// Indicates whether to generate async code for server.
3838 pub async_server : bool ,
39+ /// Gen mod rs in mod.rs
40+ pub gen_mod : bool ,
3941}
Original file line number Diff line number Diff line change 44//
55
66use std:: {
7- fs:: File ,
7+ fs:: { self , File } ,
88 io:: { Read , Write } ,
99} ;
1010use ttrpc_codegen:: { Codegen , Customize , ProtobufCustomize } ;
1111
1212fn main ( ) {
13+ fs:: create_dir_all ( "protocols/sync" ) . unwrap ( ) ;
14+ fs:: create_dir_all ( "protocols/asynchronous" ) . unwrap ( ) ;
15+
1316 let mut protos = vec ! [
1417 "protocols/protos/github.com/gogo/protobuf/gogoproto/gogo.proto" ,
1518 "protocols/protos/github.com/kata-containers/agent/pkg/types/types.proto" ,
@@ -18,15 +21,15 @@ fn main() {
1821 "protocols/protos/google/protobuf/empty.proto" ,
1922 "protocols/protos/oci.proto" ,
2023 ] ;
21-
22- let protobuf_customized = ProtobufCustomize :: default ( ) . gen_mod_rs ( false ) ;
24+ let protobuf_customized = ProtobufCustomize :: default ( ) . gen_mod_rs ( true ) ;
2325
2426 Codegen :: new ( )
2527 . out_dir ( "protocols/sync" )
2628 . inputs ( & protos)
2729 . include ( "protocols/protos" )
2830 . rust_protobuf ( )
2931 . customize ( Customize {
32+ gen_mod : true , //This could be add while the new ttrpc compiler support it.
3033 ..Default :: default ( )
3134 } )
3235 . rust_protobuf_customize ( protobuf_customized. clone ( ) )
@@ -42,6 +45,7 @@ fn main() {
4245 . include ( "protocols/protos" )
4346 . rust_protobuf ( )
4447 . customize ( Customize {
48+ gen_mod : true , //This could be add while the new ttrpc compiler support it.
4549 async_all : true ,
4650 ..Default :: default ( )
4751 } )
Load Diff This file was deleted.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments