Skip to content

Commit

Permalink
Add gen mod for more convenient to use
Browse files Browse the repository at this point in the history
while use customize ,you can use gen_mod to generate mod.rs.

Signed-off-by: jokemanfire <[email protected]>
  • Loading branch information
jokemanfire committed Sep 27, 2024
1 parent 152ac12 commit 088d8b1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ Cargo.lock
.idea
*.o
example/protocols/**/*.rs
!example/protocols/**/mod.rs
src/ttrpc.rs
31 changes: 30 additions & 1 deletion compiler/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@

#![allow(dead_code)]

use std::collections::HashMap;
use std::{
collections::{HashMap, HashSet},
fs,
io::BufRead,
};

use crate::Customize;
use protobuf::{
Expand Down Expand Up @@ -724,6 +728,31 @@ pub fn gen_and_write(
) -> io::Result<()> {
let results = gen(file_descriptors, files_to_generate, customize);

if customize.gen_mod {
let file_path = out_dir.join("mod.rs");
let mut set = HashSet::new();
//if mod file exists
if let Ok(file) = File::open(&file_path) {
let reader = io::BufReader::new(file);
reader.lines().for_each(|line| {
let _ = line.map(|r| set.insert(r));
});
}
let mut file_write = fs::OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&file_path)?;
for r in &results {
let prefix_name: Vec<&str> = r.name.split('.').collect();
set.insert(format!("pub mod {};", prefix_name[0]));
}
for item in &set {
writeln!(file_write, "{}", item)?;
}
file_write.flush()?;
}

for r in &results {
let mut file_path = out_dir.to_owned();
file_path.push(&r.name);
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ pub struct Customize {
pub async_client: bool,
/// Indicates whether to generate async code for server.
pub async_server: bool,
/// Gen mod rs in mod.rs
pub gen_mod: bool,
}
7 changes: 5 additions & 2 deletions example/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ fn main() {
"protocols/protos/google/protobuf/empty.proto",
"protocols/protos/oci.proto",
];

let protobuf_customized = ProtobufCustomize::default().gen_mod_rs(false);
///This could be add while the new ttrpc compiler support it.
///If need use now change ttrpc_codegen's cargo.toml ttrpc-compiler = "0.6.1" to ttrpc-compiler = "../compiler"
let protobuf_customized = ProtobufCustomize::default().gen_mod_rs(true);

Codegen::new()
.out_dir("protocols/sync")
.inputs(&protos)
.include("protocols/protos")
.rust_protobuf()
.customize(Customize {
gen_mod:true,//This could be add while the new ttrpc compiler support it.
..Default::default()
})
.rust_protobuf_customize(protobuf_customized.clone())
Expand All @@ -42,6 +44,7 @@ fn main() {
.include("protocols/protos")
.rust_protobuf()
.customize(Customize {
gen_mod:true, //This could be add while the new ttrpc compiler support it.
async_all: true,
..Default::default()
})
Expand Down

0 comments on commit 088d8b1

Please sign in to comment.