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
6
6
.idea
7
7
* .o
8
8
example /protocols /** /* .rs
9
- ! example /protocols /** /mod.rs
10
9
src /ttrpc.rs
Original file line number Diff line number Diff line change 36
36
37
37
#![ allow( dead_code) ]
38
38
39
- use std:: collections:: HashMap ;
39
+ use std:: {
40
+ collections:: { HashMap , HashSet } ,
41
+ fs,
42
+ io:: BufRead ,
43
+ } ;
40
44
41
45
use crate :: Customize ;
42
46
use protobuf:: {
@@ -723,6 +727,31 @@ pub fn gen_and_write(
723
727
) -> io:: Result < ( ) > {
724
728
let results = gen ( file_descriptors, files_to_generate, customize) ;
725
729
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
+
726
755
for r in & results {
727
756
let mut file_path = out_dir. to_owned ( ) ;
728
757
file_path. push ( & r. name ) ;
Original file line number Diff line number Diff line change @@ -36,4 +36,6 @@ pub struct Customize {
36
36
pub async_client : bool ,
37
37
/// Indicates whether to generate async code for server.
38
38
pub async_server : bool ,
39
+ /// Gen mod rs in mod.rs
40
+ pub gen_mod : bool ,
39
41
}
Original file line number Diff line number Diff line change 4
4
//
5
5
6
6
use std:: {
7
- fs:: File ,
7
+ fs:: { self , File } ,
8
8
io:: { Read , Write } ,
9
9
} ;
10
10
use ttrpc_codegen:: { Codegen , Customize , ProtobufCustomize } ;
11
11
12
12
fn main ( ) {
13
+ fs:: create_dir_all ( "protocols/sync" ) . unwrap ( ) ;
14
+ fs:: create_dir_all ( "protocols/asynchronous" ) . unwrap ( ) ;
15
+
13
16
let mut protos = vec ! [
14
17
"protocols/protos/github.com/gogo/protobuf/gogoproto/gogo.proto" ,
15
18
"protocols/protos/github.com/kata-containers/agent/pkg/types/types.proto" ,
@@ -18,15 +21,15 @@ fn main() {
18
21
"protocols/protos/google/protobuf/empty.proto" ,
19
22
"protocols/protos/oci.proto" ,
20
23
] ;
21
-
22
- let protobuf_customized = ProtobufCustomize :: default ( ) . gen_mod_rs ( false ) ;
24
+ let protobuf_customized = ProtobufCustomize :: default ( ) . gen_mod_rs ( true ) ;
23
25
24
26
Codegen :: new ( )
25
27
. out_dir ( "protocols/sync" )
26
28
. inputs ( & protos)
27
29
. include ( "protocols/protos" )
28
30
. rust_protobuf ( )
29
31
. customize ( Customize {
32
+ gen_mod : true , //This could be add while the new ttrpc compiler support it.
30
33
..Default :: default ( )
31
34
} )
32
35
. rust_protobuf_customize ( protobuf_customized. clone ( ) )
@@ -42,6 +45,7 @@ fn main() {
42
45
. include ( "protocols/protos" )
43
46
. rust_protobuf ( )
44
47
. customize ( Customize {
48
+ gen_mod : true , //This could be add while the new ttrpc compiler support it.
45
49
async_all : true ,
46
50
..Default :: default ( )
47
51
} )
Load Diff This file was deleted.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments