refactor: generate x.proto file using build.rs

This commit is contained in:
appflowy
2022-02-09 09:57:03 +08:00
parent 32e0b5e96d
commit 7646f984e0
35 changed files with 1972 additions and 63 deletions

View File

@ -19,6 +19,32 @@ cmd_lib = { version = "1", optional = true }
protoc-rust = { version = "2", optional = true }
walkdir = { version = "2", optional = true }
flowy-ast = { path = "../flowy-ast", optional = true}
similar = { version = "1.2.2", optional = true }
syn = { version = "1.0.60", features = ["extra-traits", "parsing", "derive", "full"], optional = true }
fancy-regex = { version = "0.5.0", optional = true }
lazy_static = { version = "1.4.0", optional = true }
tera = { version = "1.5.0", optional = true}
itertools = {versino = "0.10", optional = true}
phf = { version = "0.8.0", features = ["macros"], optional = true }
console = {version = "0.14.0", optional = true}
serde = { version = "1.0", features = ["derive"], optional = true}
toml = {version = "0.5.8", optional = true}
[features]
pb_gen = ["cmd_lib", "protoc-rust", "walkdir"]
proto_gen = [
"flowy-ast",
"similar",
"syn",
"fancy-regex",
"lazy_static",
"tera",
"itertools",
"phf",
"walkdir",
"console",
"serde",
"toml"
]
dart = []

View File

@ -2,7 +2,10 @@ pub mod future;
pub mod retry;
#[cfg(feature = "pb_gen")]
pub mod pb_gen;
pub mod pb;
#[cfg(feature = "proto_gen")]
pub mod proto_gen;
#[allow(dead_code)]
pub fn uuid_string() -> String {

View File

@ -2,37 +2,68 @@
#![allow(unused_imports)]
#![allow(unused_attributes)]
#![allow(dead_code)]
use crate::proto_gen;
use crate::proto_gen::proto_info::ProtobufCrate;
use crate::proto_gen::util::suffix_relative_to_path;
use crate::proto_gen::ProtoGenerator;
use log::info;
use std::fs::File;
use std::io::Write;
use std::process::Command;
use walkdir::WalkDir;
pub fn gen(name: &str, root: &str) {
let mut paths = vec![];
let mut file_names = vec![];
for (path, file_name) in WalkDir::new(root).into_iter().filter_map(|e| e.ok()).map(|e| {
let path = e.path().to_str().unwrap().to_string();
let file_name = e.path().file_stem().unwrap().to_str().unwrap().to_string();
(path, file_name)
}) {
if path.ends_with(".proto") {
// https://stackoverflow.com/questions/49077147/how-can-i-force-build-rs-to-run-again-without-cleaning-my-whole-project
println!("cargo:rerun-if-changed={}", path);
paths.push(path);
file_names.push(file_name);
fn gen_protos(root: &str) -> Vec<ProtobufCrate> {
let crate_context = ProtoGenerator::gen(root);
let proto_crates = crate_context
.iter()
.map(|info| info.protobuf_crate.clone())
.collect::<Vec<_>>();
crate_context
.into_iter()
.map(|info| info.files)
.flatten()
.for_each(|file| {
println!("cargo:rerun-if-changed={}", file.file_path);
});
proto_crates
}
pub fn gen_files(name: &str, root: &str) {
let root_absolute_path = std::fs::canonicalize(".").unwrap().as_path().display().to_string();
for protobuf_crate in gen_protos(&root_absolute_path) {
let mut paths = vec![];
let mut file_names = vec![];
let proto_relative_path = suffix_relative_to_path(&protobuf_crate.proto_output_dir(), &root_absolute_path);
for (path, file_name) in WalkDir::new(proto_relative_path)
.into_iter()
.filter_map(|e| e.ok())
.map(|e| {
let path = e.path().to_str().unwrap().to_string();
let file_name = e.path().file_stem().unwrap().to_str().unwrap().to_string();
(path, file_name)
})
{
if path.ends_with(".proto") {
// https://stackoverflow.com/questions/49077147/how-can-i-force-build-rs-to-run-again-without-cleaning-my-whole-project
println!("cargo:rerun-if-changed={}", path);
paths.push(path);
file_names.push(file_name);
}
}
println!("cargo:rerun-if-changed=build.rs");
#[cfg(feature = "dart")]
gen_pb_for_dart(name, root, &paths, &file_names);
protoc_rust::Codegen::new()
.out_dir("./src/protobuf/model")
.inputs(&paths)
.include(root)
.run()
.expect("Running protoc failed.");
}
println!("cargo:rerun-if-changed=build.rs");
#[cfg(feature = "dart")]
gen_pb_for_dart(name, root, &paths, &file_names);
protoc_rust::Codegen::new()
.out_dir("./src/protobuf/model")
.inputs(&paths)
.include(root)
.run()
.expect("Running protoc failed.");
}
#[cfg(feature = "dart")]

View File

@ -0,0 +1,180 @@
use crate::proto_gen::proto_info::{parse_crate_info_from_path, ProtoFile, ProtobufCrateContext};
use crate::proto_gen::template::{EnumTemplate, StructTemplate};
use crate::proto_gen::util::*;
use fancy_regex::Regex;
use flowy_ast::*;
use lazy_static::lazy_static;
use std::{fs::File, io::Read, path::Path};
use syn::Item;
use walkdir::WalkDir;
pub fn parse_crate_protobuf(roots: Vec<String>) -> Vec<ProtobufCrateContext> {
let crate_infos = parse_crate_info_from_path(roots);
crate_infos
.into_iter()
.map(|crate_info| {
let proto_output_dir = crate_info.proto_output_dir();
let files = crate_info
.proto_paths
.iter()
.map(|proto_crate_path| parse_files_protobuf(proto_crate_path, &proto_output_dir))
.flatten()
.collect::<Vec<ProtoFile>>();
ProtobufCrateContext::from_crate_info(crate_info, files)
})
.collect::<Vec<ProtobufCrateContext>>()
}
fn parse_files_protobuf(proto_crate_path: &str, proto_output_dir: &str) -> Vec<ProtoFile> {
let mut gen_proto_vec: Vec<ProtoFile> = vec![];
// file_stem https://doc.rust-lang.org/std/path/struct.Path.html#method.file_stem
for (path, file_name) in WalkDir::new(proto_crate_path)
.into_iter()
.filter_entry(|e| !is_hidden(e))
.filter_map(|e| e.ok())
.filter(|e| !e.file_type().is_dir())
.map(|e| {
let path = e.path().to_str().unwrap().to_string();
let file_name = e.path().file_stem().unwrap().to_str().unwrap().to_string();
(path, file_name)
})
{
if file_name == "mod" {
continue;
}
// https://docs.rs/syn/1.0.54/syn/struct.File.html
let ast = syn::parse_file(read_file(&path).unwrap().as_ref())
.unwrap_or_else(|_| panic!("Unable to parse file at {}", path));
let structs = get_ast_structs(&ast);
let proto_file_path = format!("{}/{}.proto", &proto_output_dir, &file_name);
let mut proto_file_content = parse_or_init_proto_file(proto_file_path.as_ref());
structs.iter().for_each(|s| {
let mut struct_template = StructTemplate::new();
struct_template.set_message_struct_name(&s.name);
s.fields.iter().filter(|f| f.attrs.pb_index().is_some()).for_each(|f| {
struct_template.set_field(&f);
});
let s = struct_template.render().unwrap();
proto_file_content.push_str(s.as_ref());
proto_file_content.push('\n');
});
let enums = get_ast_enums(&ast);
enums.iter().for_each(|e| {
let mut enum_template = EnumTemplate::new();
enum_template.set_message_enum(&e);
let s = enum_template.render().unwrap();
proto_file_content.push_str(s.as_ref());
proto_file_content.push('\n');
});
if !enums.is_empty() || !structs.is_empty() {
let info = ProtoFile {
file_path: path.clone(),
file_name: file_name.clone(),
structs: structs.iter().map(|s| s.name.clone()).collect(),
enums: enums.iter().map(|e| e.name.clone()).collect(),
generated_content: proto_file_content.clone(),
};
gen_proto_vec.push(info);
}
}
gen_proto_vec
}
pub fn parse_or_init_proto_file(path: &str) -> String {
let mut proto_file_content = String::new();
let imported_content = find_proto_file_import(path);
proto_file_content.push_str(imported_content.as_ref());
proto_file_content.push('\n');
proto_file_content
}
pub fn get_ast_structs(ast: &syn::File) -> Vec<Struct> {
// let mut content = format!("{:#?}", &ast);
// let mut file = File::create("./foo.txt").unwrap();
// file.write_all(content.as_bytes()).unwrap();
let ctxt = Ctxt::new();
let mut proto_structs: Vec<Struct> = vec![];
ast.items.iter().for_each(|item| {
if let Item::Struct(item_struct) = item {
let (_, fields) = struct_from_ast(&ctxt, &item_struct.fields);
if fields.iter().filter(|f| f.attrs.pb_index().is_some()).count() > 0 {
proto_structs.push(Struct {
name: item_struct.ident.to_string(),
fields,
});
}
}
});
ctxt.check().unwrap();
proto_structs
}
pub fn get_ast_enums(ast: &syn::File) -> Vec<FlowyEnum> {
let mut flowy_enums: Vec<FlowyEnum> = vec![];
let ctxt = Ctxt::new();
ast.items.iter().for_each(|item| {
// https://docs.rs/syn/1.0.54/syn/enum.Item.html
if let Item::Enum(item_enum) = item {
let attrs = flowy_ast::enum_from_ast(&ctxt, &item_enum.ident, &item_enum.variants, &ast.attrs);
flowy_enums.push(FlowyEnum {
name: item_enum.ident.to_string(),
attrs,
});
}
});
ctxt.check().unwrap();
flowy_enums
}
pub struct FlowyEnum<'a> {
pub name: String,
pub attrs: Vec<ASTEnumVariant<'a>>,
}
pub struct Struct<'a> {
pub name: String,
pub fields: Vec<ASTField<'a>>,
}
lazy_static! {
static ref SYNTAX_REGEX: Regex = Regex::new("syntax.*;").unwrap();
static ref IMPORT_REGEX: Regex = Regex::new("(import\\s).*;").unwrap();
}
fn find_proto_file_import(path: &str) -> String {
let mut result = String::new();
if !Path::new(path).exists() {
// log::error!("{} not exist", path);
result = String::from("syntax = \"proto3\";");
return result;
}
let mut file = File::open(path).unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
content.lines().for_each(|line| {
////Result<Option<Match<'t>>>
if let Ok(Some(m)) = SYNTAX_REGEX.find(line) {
result.push_str(m.as_str());
result.push('\n');
}
if let Ok(Some(m)) = IMPORT_REGEX.find(line) {
result.push_str(m.as_str());
result.push('\n');
}
});
result
}

View File

@ -0,0 +1,52 @@
use std::fs;
#[derive(serde::Deserialize)]
pub struct FlowyConfig {
pub proto_crates: Vec<String>,
pub event_files: Vec<String>,
}
impl FlowyConfig {
pub fn from_toml_file(path: &str) -> Self {
let content = fs::read_to_string(path).unwrap();
let config: FlowyConfig = toml::from_str(content.as_ref()).unwrap();
config
}
}
pub struct CrateConfig {
pub(crate) crate_path: String,
pub(crate) folder_name: String,
pub(crate) flowy_config: FlowyConfig,
}
impl CrateConfig {
pub fn proto_paths(&self) -> Vec<String> {
let proto_paths = self
.flowy_config
.proto_crates
.iter()
.map(|name| format!("{}/{}", self.crate_path, name))
.collect::<Vec<String>>();
proto_paths
}
}
pub fn parse_crate_config_from(entry: &walkdir::DirEntry) -> Option<CrateConfig> {
let path = entry.path().parent().unwrap();
let crate_path = path.to_str().unwrap().to_string();
let folder_name = path.file_stem().unwrap().to_str().unwrap().to_string();
let config_path = format!("{}/Flowy.toml", crate_path);
if !std::path::Path::new(&config_path).exists() {
return None;
}
let flowy_config = FlowyConfig::from_toml_file(config_path.as_ref());
Some(CrateConfig {
crate_path,
folder_name,
flowy_config,
})
}

View File

@ -0,0 +1,8 @@
mod ast;
mod flowy_toml;
mod proto_gen;
pub mod proto_info;
mod template;
pub mod util;
pub(crate) use proto_gen::*;

View File

@ -0,0 +1,63 @@
use crate::proto_gen::ast::parse_crate_protobuf;
use crate::proto_gen::proto_info::ProtobufCrateContext;
use crate::proto_gen::template::write_derive_meta;
use crate::proto_gen::util::*;
use std::{fs::OpenOptions, io::Write};
pub(crate) struct ProtoGenerator();
impl ProtoGenerator {
pub(crate) fn gen(root: &str) -> Vec<ProtobufCrateContext> {
let crate_contexts = parse_crate_protobuf(vec![root.to_owned()]);
write_proto_files(&crate_contexts);
write_rust_crate_mod_file(&crate_contexts);
for crate_info in &crate_contexts {
let _ = crate_info.protobuf_crate.create_output_dir();
let _ = crate_info.protobuf_crate.proto_output_dir();
crate_info.create_crate_mod_file();
}
crate_contexts
}
}
fn write_proto_files(crate_contexts: &[ProtobufCrateContext]) {
for context in crate_contexts {
let dir = context.protobuf_crate.proto_output_dir();
context.files.iter().for_each(|info| {
let proto_file_path = format!("{}/{}.proto", dir, &info.file_name);
save_content_to_file_with_diff_prompt(&info.generated_content, proto_file_path.as_ref());
});
}
}
fn write_rust_crate_mod_file(crate_contexts: &[ProtobufCrateContext]) {
for context in crate_contexts {
let mod_path = context.protobuf_crate.proto_model_mod_file();
match OpenOptions::new()
.create(true)
.write(true)
.append(false)
.truncate(true)
.open(&mod_path)
{
Ok(ref mut file) => {
let mut mod_file_content = String::new();
mod_file_content.push_str("#![cfg_attr(rustfmt, rustfmt::skip)]\n");
mod_file_content.push_str("// Auto-generated, do not edit\n");
walk_dir(
context.protobuf_crate.proto_output_dir().as_ref(),
|e| !e.file_type().is_dir(),
|_, name| {
let c = format!("\nmod {};\npub use {}::*;\n", &name, &name);
mod_file_content.push_str(c.as_ref());
},
);
file.write_all(mod_file_content.as_bytes()).unwrap();
}
Err(err) => {
panic!("Failed to open file: {}", err);
}
}
}
}

View File

@ -0,0 +1,142 @@
#![allow(dead_code)]
use crate::proto_gen::flowy_toml::{parse_crate_config_from, CrateConfig};
use crate::proto_gen::util::*;
use std::fs::OpenOptions;
use std::io::Write;
use walkdir::WalkDir;
#[derive(Debug)]
pub struct ProtobufCrateContext {
pub files: Vec<ProtoFile>,
pub protobuf_crate: ProtobufCrate,
}
impl ProtobufCrateContext {
pub fn from_crate_info(inner: ProtobufCrate, files: Vec<ProtoFile>) -> Self {
Self {
files,
protobuf_crate: inner,
}
}
pub fn create_crate_mod_file(&self) {
// mod model;
// pub use model::*;
let mod_file_path = format!("{}/mod.rs", self.protobuf_crate.protobuf_crate_name());
let mut content = "#![cfg_attr(rustfmt, rustfmt::skip)]\n".to_owned();
content.push_str("// Auto-generated, do not edit\n");
content.push_str("mod model;\npub 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);
}
}
}
#[allow(dead_code)]
pub fn flutter_mod_dir(&self, root: &str) -> String {
let crate_module_dir = format!("{}/{}", root, self.protobuf_crate.folder_name);
crate_module_dir
}
#[allow(dead_code)]
pub fn flutter_mod_file(&self, root: &str) -> String {
let crate_module_dir = format!("{}/{}/protobuf.dart", root, self.protobuf_crate.folder_name);
crate_module_dir
}
}
#[derive(Clone, Debug)]
pub struct ProtobufCrate {
pub folder_name: String,
pub proto_paths: Vec<String>,
pub crate_path: String,
}
impl ProtobufCrate {
pub fn from_config(config: CrateConfig) -> Self {
let proto_paths = config.proto_paths();
ProtobufCrate {
folder_name: config.folder_name,
proto_paths,
crate_path: config.crate_path,
}
}
fn protobuf_crate_name(&self) -> String {
format!("{}/src/protobuf", self.crate_path)
}
pub fn proto_output_dir(&self) -> String {
let dir = format!("{}/proto", self.protobuf_crate_name());
create_dir_if_not_exist(dir.as_ref());
dir
}
pub fn create_output_dir(&self) -> String {
let dir = format!("{}/model", self.protobuf_crate_name());
create_dir_if_not_exist(dir.as_ref());
dir
}
pub fn proto_model_mod_file(&self) -> String {
format!("{}/mod.rs", self.create_output_dir())
}
}
#[derive(Debug)]
pub struct ProtoFile {
pub file_path: String,
pub file_name: String,
pub structs: Vec<String>,
pub enums: Vec<String>,
pub generated_content: String,
}
pub fn parse_crate_info_from_path(roots: Vec<String>) -> Vec<ProtobufCrate> {
let mut protobuf_crates: Vec<ProtobufCrate> = vec![];
roots.iter().for_each(|root| {
let crates = WalkDir::new(root)
.into_iter()
.filter_entry(|e| !is_hidden(e))
.filter_map(|e| e.ok())
.filter(|e| is_crate_dir(e))
.flat_map(|e| parse_crate_config_from(&e))
.map(ProtobufCrate::from_config)
.collect::<Vec<ProtobufCrate>>();
protobuf_crates.extend(crates);
});
protobuf_crates
}
pub struct FlutterProtobufInfo {
package_path: String,
}
impl FlutterProtobufInfo {
pub fn new(root: &str) -> Self {
FlutterProtobufInfo {
package_path: root.to_owned(),
}
}
pub fn model_dir(&self) -> String {
let model_dir = format!("{}/protobuf", self.package_path);
create_dir_if_not_exist(model_dir.as_ref());
model_dir
}
#[allow(dead_code)]
pub fn mod_file_path(&self) -> String {
let mod_file_path = format!("{}/protobuf.dart", self.package_path);
mod_file_path
}
}

View File

@ -0,0 +1,79 @@
use crate::proto_gen::proto_info::{ProtoFile, ProtobufCrateContext};
use crate::proto_gen::template::{get_tera, read_file};
use itertools::Itertools;
use std::fs::OpenOptions;
use std::io::Write;
use tera::Context;
pub struct ProtobufDeriveMeta {
context: Context,
structs: Vec<String>,
enums: Vec<String>,
}
#[allow(dead_code)]
impl ProtobufDeriveMeta {
pub fn new(structs: Vec<String>, enums: Vec<String>) -> Self {
let enums: Vec<_> = enums.into_iter().unique().collect();
ProtobufDeriveMeta {
context: Context::new(),
structs,
enums,
}
}
pub fn render(&mut self) -> Option<String> {
self.context.insert("names", &self.structs);
self.context.insert("enums", &self.enums);
let tera = get_tera("proto_gen/template/derive_meta");
match tera.render("derive_meta.tera", &self.context) {
Ok(r) => Some(r),
Err(e) => {
log::error!("{:?}", e);
None
}
}
}
}
pub fn write_derive_meta(crate_infos: &[ProtobufCrateContext], derive_meta_dir: &str) {
let file_proto_infos = crate_infos
.iter()
.map(|ref crate_info| &crate_info.files)
.flatten()
.collect::<Vec<&ProtoFile>>();
let structs: Vec<String> = file_proto_infos
.iter()
.map(|info| info.structs.clone())
.flatten()
.collect();
let enums: Vec<String> = file_proto_infos
.iter()
.map(|info| info.enums.clone())
.flatten()
.collect();
let mut derive_template = ProtobufDeriveMeta::new(structs, enums);
let new_content = derive_template.render().unwrap();
let old_content = read_file(derive_meta_dir).unwrap();
if new_content == old_content {
return;
}
// println!("{}", diff_lines(&old_content, &new_content));
match OpenOptions::new()
.create(true)
.write(true)
.append(false)
.truncate(true)
.open(derive_meta_dir)
{
Ok(ref mut file) => {
file.write_all(new_content.as_bytes()).unwrap();
}
Err(err) => {
panic!("Failed to open log file: {}", err);
}
}
}

View File

@ -0,0 +1,45 @@
#![cfg_attr(rustfmt, rustfmt::skip)]
pub enum TypeCategory {
Array,
Map,
Str,
Protobuf,
Bytes,
Enum,
Opt,
Primitive,
}
// auto generate, do not edit
pub fn category_from_str(type_str: &str) -> TypeCategory {
match type_str {
"Vec" => TypeCategory::Array,
"HashMap" => TypeCategory::Map,
"u8" => TypeCategory::Bytes,
"String" => TypeCategory::Str,
{%- for name in names -%}
{%- if loop.first %}
"{{ name }}"
{%- else %}
| "{{ name }}"
{%- endif -%}
{%- if loop.last %}
=> TypeCategory::Protobuf,
{%- endif %}
{%- endfor %}
{%- for enum in enums -%}
{%- if loop.first %}
"{{ enum }}"
{%- else %}
| "{{ enum }}"
{%- endif -%}
{%- if loop.last %}
=> TypeCategory::Enum,
{%- endif %}
{%- endfor %}
"Option" => TypeCategory::Opt,
_ => TypeCategory::Primitive,
}
}

View File

@ -0,0 +1,4 @@
#![allow(clippy::module_inception)]
mod derive_meta;
pub use derive_meta::*;

View File

@ -0,0 +1,40 @@
mod derive_meta;
mod proto_file;
pub use derive_meta::*;
pub use proto_file::*;
use std::fs::File;
use std::io::Read;
use tera::Tera;
pub fn get_tera(directory: &str) -> Tera {
let mut root = format!("{}/../shared-lib/lib-infra/src/", env!("CARGO_MAKE_WORKING_DIRECTORY"));
root.push_str(directory);
let root_absolute_path = std::fs::canonicalize(root).unwrap().as_path().display().to_string();
let mut template_path = format!("{}/**/*.tera", root_absolute_path);
if cfg!(windows) {
// remove "\\?\" prefix on windows
template_path = format!("{}/**/*.tera", &root_absolute_path[4..]);
}
match Tera::new(template_path.as_ref()) {
Ok(t) => t,
Err(e) => {
log::error!("Parsing error(s): {}", e);
::std::process::exit(1);
}
}
}
pub fn read_file(path: &str) -> Option<String> {
let mut file = File::open(path).unwrap_or_else(|_| panic!("Unable to open file at {}", path));
let mut content = String::new();
match file.read_to_string(&mut content) {
Ok(_) => Some(content),
Err(e) => {
log::error!("{}, with error: {:?}", path, e);
Some("".to_string())
}
}
}

View File

@ -0,0 +1,5 @@
enum {{ enum_name }} {
{%- for item in items %}
{{ item }}
{%- endfor %}
}

View File

@ -0,0 +1,38 @@
use crate::proto_gen::ast::FlowyEnum;
use crate::proto_gen::template::get_tera;
use tera::Context;
pub struct EnumTemplate {
context: Context,
items: Vec<String>,
}
#[allow(dead_code)]
impl EnumTemplate {
pub fn new() -> Self {
EnumTemplate {
context: Context::new(),
items: vec![],
}
}
pub fn set_message_enum(&mut self, flowy_enum: &FlowyEnum) {
self.context.insert("enum_name", &flowy_enum.name);
flowy_enum.attrs.iter().for_each(|item| {
self.items
.push(format!("{} = {};", item.attrs.enum_item_name, item.attrs.value))
})
}
pub fn render(&mut self) -> Option<String> {
self.context.insert("items", &self.items);
let tera = get_tera("proto_gen/template/proto_file");
match tera.render("enum.tera", &self.context) {
Ok(r) => Some(r),
Err(e) => {
log::error!("{:?}", e);
None
}
}
}
}

View File

@ -0,0 +1,5 @@
mod enum_template;
mod struct_template;
pub use enum_template::*;
pub use struct_template::*;

View File

@ -0,0 +1,5 @@
message {{ struct_name }} {
{%- for field in fields %}
{{ field }}
{%- endfor %}
}

View File

@ -0,0 +1,107 @@
use flowy_ast::*;
use phf::phf_map;
use crate::proto_gen::template::get_tera;
use tera::Context;
// Protobuf data type : https://developers.google.com/protocol-buffers/docs/proto3
static RUST_TYPE_MAP: phf::Map<&'static str, &'static str> = phf_map! {
"String" => "string",
"i64" => "int64",
"i32" => "int32",
"u64" => "uint64",
"u32" => "uint32",
"Vec" => "repeated",
"f64" => "double",
"HashMap" => "map",
};
pub struct StructTemplate {
context: Context,
fields: Vec<String>,
}
#[allow(dead_code)]
impl StructTemplate {
pub fn new() -> Self {
StructTemplate {
context: Context::new(),
fields: vec![],
}
}
pub fn set_message_struct_name(&mut self, name: &str) {
self.context.insert("struct_name", name);
}
pub fn set_field(&mut self, field: &ASTField) {
// {{ field_type }} {{ field_name }} = {{index}};
let name = field.name().unwrap().to_string();
let index = field.attrs.pb_index().unwrap();
let ty: &str = &field.ty_as_str();
let mut mapped_ty: &str = ty;
if RUST_TYPE_MAP.contains_key(ty) {
mapped_ty = RUST_TYPE_MAP[ty];
}
if let Some(ref category) = field.bracket_category {
match category {
BracketCategory::Opt => match &field.bracket_inner_ty {
None => {}
Some(inner_ty) => match inner_ty.to_string().as_str() {
//TODO: support hashmap or something else wrapped by Option
"Vec" => {
self.fields
.push(format!("oneof one_of_{} {{ bytes {} = {}; }};", name, name, index));
}
_ => {
self.fields.push(format!(
"oneof one_of_{} {{ {} {} = {}; }};",
name, mapped_ty, name, index
));
}
},
},
BracketCategory::Map((k, v)) => {
let key: &str = k;
let value: &str = v;
self.fields.push(format!(
// map<string, string> attrs = 1;
"map<{}, {}> {} = {};",
RUST_TYPE_MAP.get(key).unwrap_or(&key),
RUST_TYPE_MAP.get(value).unwrap_or(&value),
name,
index
));
}
BracketCategory::Vec => {
let bracket_ty: &str = &field.bracket_ty.as_ref().unwrap().to_string();
// Vec<u8>
if mapped_ty == "u8" && bracket_ty == "Vec" {
self.fields.push(format!("bytes {} = {};", name, index))
} else {
self.fields.push(format!(
"{} {} {} = {};",
RUST_TYPE_MAP[bracket_ty], mapped_ty, name, index
))
}
}
BracketCategory::Other => self.fields.push(format!("{} {} = {};", mapped_ty, name, index)),
}
}
}
pub fn render(&mut self) -> Option<String> {
self.context.insert("fields", &self.fields);
let tera = get_tera("proto_gen/template/proto_file");
match tera.render("struct.tera", &self.context) {
Ok(r) => Some(r),
Err(e) => {
log::error!("{:?}", e);
None
}
}
}
}

View File

@ -0,0 +1,128 @@
use console::Style;
use similar::{ChangeTag, TextDiff};
use std::{
fs::{File, OpenOptions},
io::{Read, Write},
path::Path,
};
use walkdir::WalkDir;
pub fn read_file(path: &str) -> Option<String> {
let mut file = File::open(path).unwrap_or_else(|_| panic!("Unable to open file at {}", path));
let mut content = String::new();
match file.read_to_string(&mut content) {
Ok(_) => Some(content),
Err(e) => {
log::error!("{}, with error: {:?}", path, e);
Some("".to_string())
}
}
}
pub fn save_content_to_file_with_diff_prompt(content: &str, output_file: &str) {
if Path::new(output_file).exists() {
let old_content = read_file(output_file).unwrap();
let new_content = content.to_owned();
let write_to_file = || match OpenOptions::new()
.create(true)
.write(true)
.append(false)
.truncate(true)
.open(output_file)
{
Ok(ref mut file) => {
file.write_all(new_content.as_bytes()).unwrap();
}
Err(err) => {
panic!("Failed to open log file: {}", err);
}
};
if new_content != old_content {
print_diff(old_content, new_content.clone());
write_to_file()
}
} else {
match OpenOptions::new().create(true).write(true).open(output_file) {
Ok(ref mut file) => file.write_all(content.as_bytes()).unwrap(),
Err(err) => panic!("Open or create to {} fail: {}", output_file, err),
}
}
}
pub fn print_diff(old_content: String, new_content: String) {
let diff = TextDiff::from_lines(&old_content, &new_content);
for op in diff.ops() {
for change in diff.iter_changes(op) {
let (sign, style) = match change.tag() {
ChangeTag::Delete => ("-", Style::new().red()),
ChangeTag::Insert => ("+", Style::new().green()),
ChangeTag::Equal => (" ", Style::new()),
};
match change.tag() {
ChangeTag::Delete => {
print!("{}{}", style.apply_to(sign).bold(), style.apply_to(change));
}
ChangeTag::Insert => {
print!("{}{}", style.apply_to(sign).bold(), style.apply_to(change));
}
ChangeTag::Equal => {}
};
}
println!("---------------------------------------------------");
}
}
#[allow(dead_code)]
pub fn is_crate_dir(e: &walkdir::DirEntry) -> bool {
let cargo = e.path().file_stem().unwrap().to_str().unwrap().to_string();
cargo == *"Cargo"
}
#[allow(dead_code)]
pub fn is_proto_file(e: &walkdir::DirEntry) -> bool {
if e.path().extension().is_none() {
return false;
}
let ext = e.path().extension().unwrap().to_str().unwrap().to_string();
ext == *"proto"
}
pub fn is_hidden(entry: &walkdir::DirEntry) -> bool {
entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false)
}
pub fn create_dir_if_not_exist(dir: &str) {
if !std::path::Path::new(&dir).exists() {
std::fs::create_dir_all(&dir).unwrap();
}
}
#[allow(dead_code)]
pub(crate) fn walk_dir<F1, F2>(dir: &str, filter: F2, mut path_and_name: F1)
where
F1: FnMut(String, String),
F2: Fn(&walkdir::DirEntry) -> bool,
{
for (path, name) in WalkDir::new(dir)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| filter(e))
.map(|e| {
(
e.path().to_str().unwrap().to_string(),
e.path().file_stem().unwrap().to_str().unwrap().to_string(),
)
})
{
path_and_name(path, name);
}
}
#[allow(dead_code)]
pub fn suffix_relative_to_path(path: &str, base: &str) -> String {
let base = Path::new(base);
let path = Path::new(path);
path.strip_prefix(base).unwrap().to_str().unwrap().to_owned()
}