Skip to content

Commit 3243468

Browse files
jokemanfireTim-Zhang
authored andcommitted
Add gen mod for more convenient to use
while use customize ,you can use gen_mod to generate mod.rs. Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn>
1 parent b9e9dd8 commit 3243468

File tree

6 files changed

+39
-33
lines changed

6 files changed

+39
-33
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ Cargo.lock
66
.idea
77
*.o
88
example/protocols/**/*.rs
9-
!example/protocols/**/mod.rs
109
src/ttrpc.rs

compiler/src/codegen.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@
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

4145
use crate::Customize;
4246
use 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);

compiler/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

example/build.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
//
55

66
use std::{
7-
fs::File,
7+
fs::{self, File},
88
io::{Read, Write},
99
};
1010
use ttrpc_codegen::{Codegen, Customize, ProtobufCustomize};
1111

1212
fn 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
})

example/protocols/asynchronous/mod.rs

Lines changed: 0 additions & 15 deletions
This file was deleted.

example/protocols/sync/mod.rs

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)