From 73351f6c826d0ec4f596d2e470000c54f0c5d799 Mon Sep 17 00:00:00 2001 From: Ggiggle <47661277+Ggiggle@users.noreply.github.com> Date: Mon, 13 Nov 2023 18:51:57 +0800 Subject: [PATCH] fix: escape keywords when generate code (#213) * fix: escape keywords when generate code --- Cargo.lock | 2 +- pilota-build/Cargo.toml | 2 +- pilota-build/src/codegen/mod.rs | 12 ++++++++++-- pilota-build/src/codegen/thrift/mod.rs | 2 +- pilota-build/src/codegen/workspace.rs | 5 ++++- pilota-build/src/middle/context.rs | 24 ++++++++++++------------ pilota-build/src/middle/resolver.rs | 2 -- pilota-build/src/middle/ty.rs | 7 ++++++- 8 files changed, 35 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c426c537..c45a3438 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -793,7 +793,7 @@ dependencies = [ [[package]] name = "pilota-build" -version = "0.9.7" +version = "0.9.8" dependencies = [ "anyhow", "criterion", diff --git a/pilota-build/Cargo.toml b/pilota-build/Cargo.toml index ecdd6978..a7a7864d 100644 --- a/pilota-build/Cargo.toml +++ b/pilota-build/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pilota-build" -version = "0.9.7" +version = "0.9.8" edition = "2021" description = "Compile thrift and protobuf idl into rust code at compile-time." documentation = "https://docs.rs/pilota-build" diff --git a/pilota-build/src/codegen/mod.rs b/pilota-build/src/codegen/mod.rs index 2fc7b7a9..9d1f4c79 100644 --- a/pilota-build/src/codegen/mod.rs +++ b/pilota-build/src/codegen/mod.rs @@ -170,7 +170,11 @@ where }; } CodegenKind::RePub => { - let path = self.item_path(item.def_id).join("::"); + let path = self + .item_path(item.def_id) + .iter() + .map(|item| item.to_string()) + .join("::"); stream.push_str(format!("pub use ::{};", path).as_str()); } }) @@ -336,7 +340,11 @@ where pub fn get_init_service(&self, def_id: DefId) -> (String, String) { let service_name = self.rust_name(def_id); let mod_prefix = self.mod_path(def_id); - let service_path = format!("{}::{}", mod_prefix.join("::"), service_name); + let service_path = format!( + "{}::{}", + mod_prefix.iter().map(|item| item.to_string()).join("::"), + service_name + ); tracing::debug!("service_path: {}", service_path); let methods = self.service_methods(def_id); diff --git a/pilota-build/src/codegen/thrift/mod.rs b/pilota-build/src/codegen/thrift/mod.rs index 715b7716..42340d1f 100644 --- a/pilota-build/src/codegen/thrift/mod.rs +++ b/pilota-build/src/codegen/thrift/mod.rs @@ -93,7 +93,7 @@ impl ThriftBackend { fn codegen_impl_message( &self, - def_id: DefId, + _def_id: DefId, name: Symbol, encode: String, size: String, diff --git a/pilota-build/src/codegen/workspace.rs b/pilota-build/src/codegen/workspace.rs index 4d941eb9..8752c982 100644 --- a/pilota-build/src/codegen/workspace.rs +++ b/pilota-build/src/codegen/workspace.rs @@ -303,7 +303,10 @@ where })), ); if let Some(main_mod_path) = info.main_mod_path { - gen_rs_stream.push_str(&format!("pub use {}::*;", main_mod_path.join("::"))); + gen_rs_stream.push_str(&format!( + "pub use {}::*;", + main_mod_path.iter().map(|item| item.to_string()).join("::") + )); } gen_rs_stream = format! {r#"pub mod gen {{ #![allow(warnings, clippy::all)] diff --git a/pilota-build/src/middle/context.rs b/pilota-build/src/middle/context.rs index 07edcbe8..9a9ff00d 100644 --- a/pilota-build/src/middle/context.rs +++ b/pilota-build/src/middle/context.rs @@ -871,18 +871,18 @@ impl Context { info } - pub fn def_id_info(&self, def_id: DefId) -> FastStr { - let file_path = self - .file(self.node(def_id).unwrap().file_id) - .unwrap() - .package - .clone(); - file_path - .iter() - .chain(&[self.node(def_id).unwrap().name()]) - .join("::") - .into() - } + // pub fn def_id_info(&self, def_id: DefId) -> FastStr { + // let file_path = self + // .file(self.node(def_id).unwrap().file_id) + // .unwrap() + // .package + // .clone(); + // file_path + // .iter() + // .chain(&[self.node(def_id).unwrap().name()]) + // .join("::") + // .into() + // } pub fn config(&self, crate_id: &CrateId) -> &serde_yaml::Value { let main_file = crate_id.main_file; diff --git a/pilota-build/src/middle/resolver.rs b/pilota-build/src/middle/resolver.rs index fb8956f7..d9064a03 100644 --- a/pilota-build/src/middle/resolver.rs +++ b/pilota-build/src/middle/resolver.rs @@ -24,8 +24,6 @@ pub trait PathResolver: Sync + Send { _ => panic!(), }; - // keyword escape - let name = name.to_string().into(); segs.push(name); } diff --git a/pilota-build/src/middle/ty.rs b/pilota-build/src/middle/ty.rs index b776d4c5..f5cb740d 100644 --- a/pilota-build/src/middle/ty.rs +++ b/pilota-build/src/middle/ty.rs @@ -1,6 +1,7 @@ use std::fmt::Display; pub use std::sync::Arc; +use itertools::Itertools; pub use TyKind::*; use super::context::tls::with_cx; @@ -141,7 +142,11 @@ impl CodegenTy { .into() } CodegenTy::Adt(def) => with_cx(|cx| { - let path = cx.item_path(def.did).join("::"); + let path = cx + .item_path(def.did) + .iter() + .map(|item| item.to_string()) + .join("::"); format!("::{path}").into() }),