style: fix warnings

This commit is contained in:
appflowy 2022-02-09 18:35:50 +08:00
parent 4b63170e56
commit 7cceafa432
10 changed files with 35 additions and 64 deletions

View File

@ -15,7 +15,7 @@ impl DartEventCodeGen {
let event_crates = parse_dart_event_files(self.rust_sources.clone());
let event_ast = event_crates
.iter()
.map(|event_crate| parse_event_crate(event_crate))
.map(parse_event_crate)
.flatten()
.collect::<Vec<_>>();
@ -62,7 +62,7 @@ pub fn parse_dart_event_files(roots: Vec<String>) -> Vec<DartEventCrate> {
.into_iter()
.filter_entry(|e| !is_hidden(e))
.filter_map(|e| e.ok())
.filter(|e| is_crate_dir(e))
.filter(is_crate_dir)
.flat_map(|e| parse_crate_config_from(&e))
.map(|crate_config| DartEventCrate::from_config(&crate_config))
.collect::<Vec<DartEventCrate>>();

View File

@ -10,7 +10,7 @@ fn main() {
let matches = app().get_matches();
if let Some(ref matches) = matches.subcommand_matches("pb-gen") {
if let Some(matches) = matches.subcommand_matches("pb-gen") {
let rust_sources: Vec<String> = matches
.values_of("rust_sources")
.unwrap()
@ -27,7 +27,7 @@ fn main() {
.gen();
}
if let Some(ref matches) = matches.subcommand_matches("dart-event") {
if let Some(matches) = matches.subcommand_matches("dart-event") {
let rust_sources: Vec<String> = matches
.values_of("rust_sources")
.unwrap()

View File

@ -59,7 +59,7 @@ fn parse_files_protobuf(proto_crate_path: &str, proto_output_dir: &str) -> Vec<P
.iter()
.filter(|f| f.attrs.pb_index().is_some())
.for_each(|f| {
struct_template.set_field(&f);
struct_template.set_field(f);
});
let s = struct_template.render().unwrap();
@ -70,7 +70,7 @@ fn parse_files_protobuf(proto_crate_path: &str, proto_output_dir: &str) -> Vec<P
let enums = get_ast_enums(&ast);
enums.iter().for_each(|e| {
let mut enum_template = EnumTemplate::new();
enum_template.set_message_enum(&e);
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');

View File

@ -43,12 +43,19 @@ pub fn gen_files(crate_name: &str, root: &str) {
#[cfg(feature = "dart")]
fn gen_pb_for_dart(name: &str, root: &str, paths: &Vec<String>, file_names: &Vec<String>) {
let output = format!(
"{}/{}/{}",
env!("CARGO_MAKE_WORKING_DIRECTORY"),
env!("FLUTTER_FLOWY_SDK_PATH"),
name
);
if std::env::var("CARGO_MAKE_WORKING_DIRECTORY").is_err() {
log::warn!("CARGO_MAKE_WORKING_DIRECTORY was not set, skip generate dart pb");
return;
}
if std::env::var("FLUTTER_FLOWY_SDK_PATH").is_err() {
log::warn!("FLUTTER_FLOWY_SDK_PATH was not set, skip generate dart pb");
return;
}
let workspace_dir = std::env::var("CARGO_MAKE_WORKING_DIRECTORY").unwrap();
let flutter_sdk_path = std::env::var("FLUTTER_FLOWY_SDK_PATH").unwrap();
let output = format!("{}/{}/{}", workspace_dir, flutter_sdk_path, name);
if !std::path::Path::new(&output).exists() {
std::fs::create_dir_all(&output).unwrap();
}

View File

@ -56,7 +56,7 @@ fn parse_files_protobuf(proto_crate_path: &str, proto_output_dir: &str) -> Vec<P
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);
struct_template.set_field(f);
});
let s = struct_template.render().unwrap();
@ -67,7 +67,7 @@ fn parse_files_protobuf(proto_crate_path: &str, proto_output_dir: &str) -> Vec<P
let enums = get_ast_enums(&ast);
enums.iter().for_each(|e| {
let mut enum_template = EnumTemplate::new();
enum_template.set_message_enum(&e);
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');

View File

@ -1,3 +1,4 @@
#![allow(clippy::module_inception)]
mod ast;
mod flowy_toml;
mod proto_gen;

View File

@ -1,6 +1,6 @@
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 crate::proto_gen::ProtoFile;
use std::fs::File;
@ -38,7 +38,7 @@ impl ProtoGenerator {
file.write_all(cache_str.as_bytes()).unwrap();
File::flush(file).unwrap();
}
Err(err) => {
Err(_err) => {
panic!("Failed to open file: {}", protobuf_cache_path);
}
}
@ -99,7 +99,7 @@ impl ProtoCache {
fn from_crate_contexts(crate_contexts: &[ProtobufCrateContext]) -> Self {
let proto_files = crate_contexts
.iter()
.map(|ref crate_info| &crate_info.files)
.map(|crate_info| &crate_info.files)
.flatten()
.collect::<Vec<&ProtoFile>>();

View File

@ -109,7 +109,7 @@ pub fn parse_crate_info_from_path(roots: Vec<String>) -> Vec<ProtobufCrate> {
.into_iter()
.filter_entry(|e| !is_hidden(e))
.filter_map(|e| e.ok())
.filter(|e| is_crate_dir(e))
.filter(is_crate_dir)
.flat_map(|e| parse_crate_config_from(&e))
.map(ProtobufCrate::from_config)
.collect::<Vec<ProtobufCrate>>();

View File

@ -1,8 +1,5 @@
use crate::proto_gen::proto_info::{ProtoFile, ProtobufCrateContext};
use crate::proto_gen::template::{get_tera, read_file};
use crate::proto_gen::template::get_tera;
use itertools::Itertools;
use std::fs::OpenOptions;
use std::io::Write;
use tera::Context;
pub struct ProtobufDeriveMeta {
@ -36,44 +33,3 @@ impl ProtobufDeriveMeta {
}
}
}
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

@ -11,7 +11,13 @@ pub fn get_tera(directory: &str) -> Tera {
let mut root = format!("{}/../", file!());
root.push_str(directory);
let root_absolute_path = std::fs::canonicalize(root).unwrap().as_path().display().to_string();
let root_absolute_path = match std::fs::canonicalize(root) {
Ok(p) => p.as_path().display().to_string(),
Err(e) => {
panic!("canonicalize {} failed {:?}", root, e);
}
};
let mut template_path = format!("{}/**/*.tera", root_absolute_path);
if cfg!(windows) {
// remove "\\?\" prefix on windows
@ -27,6 +33,7 @@ pub fn get_tera(directory: &str) -> Tera {
}
}
#[allow(dead_code)]
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();