Skip to content

Commit

Permalink
fix: generate keywords that are not supported in r# with suffix _ (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
Millione authored Mar 26, 2024
1 parent a76d848 commit 7153348
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
11 changes: 9 additions & 2 deletions pilota-build/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ where

impl std::fmt::Display for Symbol {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if &**self == "Self" {
return write!(f, "Self_");
if self.is_path_segment_keyword() {
return write!(f, "{}_", &**self);
}
if KEYWORDS_SET.contains(self) {
write!(f, "r#{}", &**self)
Expand All @@ -112,6 +112,13 @@ impl std::fmt::Display for Symbol {
}
}

impl Symbol {
// https://github.com/rust-lang/rust/blob/master/compiler/rustc_span/src/symbol.rs#L2395-L2398
fn is_path_segment_keyword(&self) -> bool {
["super", "self", "Self", "crate"].contains(&&**self)
}
}

#[derive(Hash, PartialEq, Eq, Clone, Debug, Copy)]
pub enum EnumRepr {
I32,
Expand Down
33 changes: 31 additions & 2 deletions pilota-build/test_data/thrift/self_kw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ pub mod self_kw {
#[derive(PartialOrd, Hash, Eq, Ord, Debug, Default, Clone, PartialEq)]
pub struct A {
pub r#type: ::pilota::FastStr,

pub self_: ::pilota::FastStr,
}
impl ::pilota::thrift::Message for A {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
Expand All @@ -105,6 +107,7 @@ pub mod self_kw {

protocol.write_struct_begin(&struct_ident)?;
protocol.write_faststr_field(1, (&self.r#type).clone())?;
protocol.write_faststr_field(2, (&self.self_).clone())?;
protocol.write_field_stop()?;
protocol.write_struct_end()?;
Ok(())
Expand All @@ -117,6 +120,7 @@ pub mod self_kw {
use ::pilota::{thrift::TLengthProtocolExt, Buf};

let mut r#type = None;
let mut self_ = None;

let mut __pilota_decoding_field_id = None;

Expand All @@ -137,6 +141,11 @@ pub mod self_kw {
{
r#type = Some(protocol.read_faststr()?);
}
Some(2)
if field_ident.field_type == ::pilota::thrift::TType::Binary =>
{
self_ = Some(protocol.read_faststr()?);
}
_ => {
protocol.skip(field_ident.field_type)?;
}
Expand All @@ -163,8 +172,14 @@ pub mod self_kw {
"field r#type is required".to_string(),
));
};
let Some(self_) = self_ else {
return Err(::pilota::thrift::new_protocol_exception(
::pilota::thrift::ProtocolExceptionKind::InvalidData,
"field self_ is required".to_string(),
));
};

let data = Self { r#type };
let data = Self { r#type, self_ };
Ok(data)
}

Expand All @@ -180,6 +195,7 @@ pub mod self_kw {
> {
::std::boxed::Box::pin(async move {
let mut r#type = None;
let mut self_ = None;

let mut __pilota_decoding_field_id = None;

Expand All @@ -199,6 +215,12 @@ pub mod self_kw {
{
r#type = Some(protocol.read_faststr().await?);
}
Some(2)
if field_ident.field_type
== ::pilota::thrift::TType::Binary =>
{
self_ = Some(protocol.read_faststr().await?);
}
_ => {
protocol.skip(field_ident.field_type).await?;
}
Expand Down Expand Up @@ -226,8 +248,14 @@ pub mod self_kw {
"field r#type is required".to_string(),
));
};
let Some(self_) = self_ else {
return Err(::pilota::thrift::new_protocol_exception(
::pilota::thrift::ProtocolExceptionKind::InvalidData,
"field self_ is required".to_string(),
));
};

let data = Self { r#type };
let data = Self { r#type, self_ };
Ok(data)
})
}
Expand All @@ -237,6 +265,7 @@ pub mod self_kw {
use ::pilota::thrift::TLengthProtocolExt;
protocol.struct_begin_len(&::pilota::thrift::TStructIdentifier { name: "A" })
+ protocol.faststr_field_len(Some(1), &self.r#type)
+ protocol.faststr_field_len(Some(2), &self.self_)
+ protocol.field_stop_len()
+ protocol.struct_end_len()
}
Expand Down
1 change: 1 addition & 0 deletions pilota-build/test_data/thrift/self_kw.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ enum Index {

struct A {
1: required string type,
2: required string self,
}

0 comments on commit 7153348

Please sign in to comment.