From 447b5aa128266c881f07e6b05b7e92aa674d8f83 Mon Sep 17 00:00:00 2001 From: appflowy Date: Mon, 5 Jul 2021 15:49:03 +0800 Subject: [PATCH] create crate protobuf mod file automatically --- .../flowy-derive/src/proto_buf/deserialize.rs | 6 ++-- rust-lib/flowy-user/Cargo.toml | 1 - rust-lib/flowy-user/src/lib.rs | 1 + rust-lib/flowy-user/src/protobuf/mod.rs | 4 +++ scripts/flowy-tool/src/proto/helper.rs | 28 ++++++++++++++++++- scripts/flowy-tool/src/proto/proto_gen.rs | 20 +++++++++---- 6 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 rust-lib/flowy-user/src/protobuf/mod.rs diff --git a/rust-lib/flowy-derive/src/proto_buf/deserialize.rs b/rust-lib/flowy-derive/src/proto_buf/deserialize.rs index cbd43de95d..389d0299be 100644 --- a/rust-lib/flowy-derive/src/proto_buf/deserialize.rs +++ b/rust-lib/flowy-derive/src/proto_buf/deserialize.rs @@ -25,7 +25,7 @@ pub fn make_de_token_steam(ctxt: &Ctxt, ast: &ASTContainer) -> Option> for #struct_ident { type Error = String; fn try_from(bytes: &Vec) -> Result { - let result: ::protobuf::ProtobufResult = ::protobuf::Message::parse_from_bytes(bytes); + let result: ::protobuf::ProtobufResult = ::protobuf::Message::parse_from_bytes(bytes); match result { Ok(mut pb) => { #struct_ident::try_from(&mut pb) @@ -35,9 +35,9 @@ pub fn make_de_token_steam(ctxt: &Ctxt, ast: &ASTContainer) -> Option for #struct_ident { + impl std::convert::TryFrom<&mut crate::protobuf::#pb_ty> for #struct_ident { type Error = String; - fn try_from(pb: &mut flowy_protobuf::#pb_ty) -> Result { + fn try_from(pb: &mut crate::protobuf::#pb_ty) -> Result { let mut o = Self::default(); #(#build_take_fields)* Ok(o) diff --git a/rust-lib/flowy-user/Cargo.toml b/rust-lib/flowy-user/Cargo.toml index 1ad8814744..04e23fdb60 100644 --- a/rust-lib/flowy-user/Cargo.toml +++ b/rust-lib/flowy-user/Cargo.toml @@ -10,7 +10,6 @@ derive_more = {version = "0.99", features = ["display"]} flowy-sys = { path = "../flowy-sys" } flowy-log = { path = "../flowy-log" } flowy-derive = { path = "../flowy-derive" } -flowy-protobuf = { path = "../flowy-protobuf" } tracing = { version = "0.1", features = ["log"] } bytes = "1.0" serde = { version = "1.0", features = ["derive"] } diff --git a/rust-lib/flowy-user/src/lib.rs b/rust-lib/flowy-user/src/lib.rs index d3af4e5e8b..498e7e3ebd 100644 --- a/rust-lib/flowy-user/src/lib.rs +++ b/rust-lib/flowy-user/src/lib.rs @@ -3,6 +3,7 @@ mod error; pub mod event; mod handlers; pub mod module; +mod protobuf; pub mod prelude { pub use crate::{ diff --git a/rust-lib/flowy-user/src/protobuf/mod.rs b/rust-lib/flowy-user/src/protobuf/mod.rs new file mode 100644 index 0000000000..2480f62fbe --- /dev/null +++ b/rust-lib/flowy-user/src/protobuf/mod.rs @@ -0,0 +1,4 @@ + +mod model; +pub use model::*; + \ No newline at end of file diff --git a/scripts/flowy-tool/src/proto/helper.rs b/scripts/flowy-tool/src/proto/helper.rs index f32593c356..c679dbf2b0 100644 --- a/scripts/flowy-tool/src/proto/helper.rs +++ b/scripts/flowy-tool/src/proto/helper.rs @@ -1,3 +1,5 @@ +use std::fs::OpenOptions; +use std::io::Write; use walkdir::WalkDir; #[derive(Clone)] @@ -29,7 +31,7 @@ impl CrateInfo { dir } - pub fn crate_mod_file(&self) -> String { + pub fn proto_model_mod_file(&self) -> String { format!("{}/mod.rs", self.proto_struct_output_dir()) } @@ -44,6 +46,30 @@ impl CrateProtoInfo { pub fn from_crate_info(inner: CrateInfo, files: Vec) -> Self { Self { files, inner } } + + pub fn create_crate_mod_file(&self) { + // mod model; + // pub use model::*; + let mod_file_path = format!("{}/mod.rs", self.inner.protobuf_crate_name()); + let content = r#" +mod model; +pub use model::*; + "#; + match OpenOptions::new() + .create(true) + .write(true) + .append(false) + .truncate(true) + .open(&mod_file_path) + { + Ok(ref mut file) => { + file.write_all(content.as_bytes()).unwrap(); + } + Err(err) => { + panic!("Failed to open protobuf mod file: {}", err); + } + } + } } #[derive(Debug)] diff --git a/scripts/flowy-tool/src/proto/proto_gen.rs b/scripts/flowy-tool/src/proto/proto_gen.rs index ad5bea15f9..05d7819d3b 100644 --- a/scripts/flowy-tool/src/proto/proto_gen.rs +++ b/scripts/flowy-tool/src/proto/proto_gen.rs @@ -23,6 +23,8 @@ impl ProtoGen { run_protoc(&crate_proto_infos); + write_protobuf_crate_mod_file(&crate_proto_infos); + write_derive_meta(&crate_proto_infos, self.derive_meta_dir.as_ref()); write_rust_crate_protobuf(&crate_proto_infos); @@ -45,7 +47,7 @@ fn write_proto_files(crate_infos: &Vec) { fn write_rust_crate_protobuf(crate_infos: &Vec) { for crate_info in crate_infos { - let mod_path = crate_info.inner.crate_mod_file(); + let mod_path = crate_info.inner.proto_model_mod_file(); match OpenOptions::new() .create(true) .write(true) @@ -79,10 +81,6 @@ fn write_rust_crate_protobuf(crate_infos: &Vec) { } fn run_protoc(crate_infos: &Vec) { - // protoc --rust_out=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/flowy-protobuf/src/model \ - // --proto_path=${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/flowy-protobuf/define \ - // ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/flowy-protobuf/define/*.proto - for crate_info in crate_infos { let rust_out = crate_info.inner.proto_struct_output_dir(); let proto_path = crate_info.inner.proto_file_output_dir(); @@ -93,9 +91,19 @@ fn run_protoc(crate_infos: &Vec) { .filter(|e| is_proto_file(e)) .map(|e| e.path().to_str().unwrap().to_string()) { - cmd_lib::run_cmd! { + if cmd_lib::run_cmd! { protoc --rust_out=${rust_out} --proto_path=${proto_path} ${proto_file} + } + .is_err() + { + panic!("Create protobuf rust struct fail") }; } } } + +fn write_protobuf_crate_mod_file(crate_infos: &Vec) { + for crate_info in crate_infos { + crate_info.create_crate_mod_file(); + } +}