Skip to content

Commit

Permalink
fix: use rust_name (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
Millione authored Nov 9, 2022
1 parent c280540 commit e862d10
Show file tree
Hide file tree
Showing 3 changed files with 355 additions and 9 deletions.
11 changes: 5 additions & 6 deletions pilota-build/src/middle/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,18 @@ impl Context {
}

pub fn item_path(&self, def_id: DefId) -> Arc<[smol_str::SmolStr]> {
fn calc_item_path(db: &Context, def_id: DefId, segs: &mut Vec<smol_str::SmolStr>) {
let node = db.node(def_id).unwrap();
fn calc_item_path(cx: &Context, def_id: DefId, segs: &mut Vec<smol_str::SmolStr>) {
let node = cx.node(def_id).unwrap();

match node.kind {
NodeKind::Item(_) => {}
_ => calc_item_path(db, node.parent.unwrap(), segs),
_ => calc_item_path(cx, node.parent.unwrap(), segs),
}

let name = match node.kind {
NodeKind::Item(item) => match &*item {
crate::rir::Item::Const(_) => (&*item.symbol_name()).const_ident(),
crate::rir::Item::Mod(_) => return,
_ => (&*item.symbol_name()).upper_camel_ident(),
_ => cx.rust_name(def_id),
},
NodeKind::Variant(v) => (&**v.name).variant_ident(),
_ => panic!(),
Expand All @@ -164,7 +163,7 @@ impl Context {

let mut segs = Vec::from(&*self.mod_path(def_id));

calc_item_path(&self, def_id, &mut segs);
calc_item_path(self, def_id, &mut segs);

Arc::from(segs)
}
Expand Down
347 changes: 345 additions & 2 deletions pilota-build/test_data/thrift/pilota_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ pub mod pilota_name {
}
pub const LANG_ID: &'static str = "id";
#[derive(PartialOrd, Hash, Eq, Ord, Debug, Default, Clone, PartialEq)]
pub struct Test {
pub struct Test1 {
pub id: ::std::string::String,
pub hello: ::std::string::String,
}
#[::async_trait::async_trait]
impl ::pilota::thrift::Message for Test {
impl ::pilota::thrift::Message for Test1 {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&self,
protocol: &mut T,
Expand Down Expand Up @@ -332,5 +332,348 @@ pub mod pilota_name {
protocol.write_i32_len(*self as i32)
}
}
#[derive(PartialOrd, Hash, Eq, Ord, Debug, :: pilota :: derivative :: Derivative)]
#[derivative(Default)]
#[derive(Clone, PartialEq)]
pub enum TestServiceTestResult {
#[derivative(Default)]
Ok(Test1),
}
#[::async_trait::async_trait]
impl ::pilota::thrift::Message for TestServiceTestResult {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&self,
protocol: &mut T,
) -> ::std::result::Result<(), ::pilota::thrift::Error> {
protocol.write_struct_begin(&::pilota::thrift::TStructIdentifier {
name: "TestServiceTestResult",
})?;
match self {
TestServiceTestResult::Ok(ref value) => {
protocol.write_field_begin(::pilota::thrift::TType::Struct, 0i16)?;
::pilota::thrift::Message::encode(value, protocol)?;
protocol.write_field_end()?;
}
}
protocol.write_field_stop()?;
protocol.write_struct_end()?;
Ok(())
}
fn decode<T: ::pilota::thrift::TInputProtocol>(
protocol: &mut T,
) -> ::std::result::Result<Self, ::pilota::thrift::Error> {
let mut ret = None;
protocol.read_struct_begin()?;
loop {
let field_ident = protocol.read_field_begin()?;
if field_ident.field_type == ::pilota::thrift::TType::Stop {
break;
}
let field_id = field_ident.id;
match field_id {
Some(0i16) => {
if ret.is_none() {
ret = Some(TestServiceTestResult::Ok(
::pilota::thrift::Message::decode(protocol)?,
));
} else {
return Err(::pilota::thrift::new_protocol_error(
::pilota::thrift::ProtocolErrorKind::InvalidData,
"received multiple fields for union from remote Message",
));
}
}
_ => {
protocol.skip(field_ident.field_type)?;
}
}
}
protocol.read_field_end()?;
protocol.read_struct_end()?;
if let Some(ret) = ret {
Ok(ret)
} else {
Err(::pilota::thrift::new_protocol_error(
::pilota::thrift::ProtocolErrorKind::InvalidData,
"received empty union from remote Message",
))
}
}
async fn decode_async<C: ::tokio::io::AsyncRead + Unpin + Send>(
protocol: &mut ::pilota::thrift::TAsyncBinaryProtocol<C>,
) -> ::std::result::Result<Self, ::pilota::thrift::Error> {
let mut ret = None;
protocol.read_struct_begin().await?;
loop {
let field_ident = protocol.read_field_begin().await?;
if field_ident.field_type == ::pilota::thrift::TType::Stop {
break;
}
let field_id = field_ident.id;
match field_id {
Some(0i16) => {
if ret.is_none() {
ret = Some(TestServiceTestResult::Ok(
::pilota::thrift::Message::decode_async(protocol).await?,
));
} else {
return Err(::pilota::thrift::new_protocol_error(
::pilota::thrift::ProtocolErrorKind::InvalidData,
"received multiple fields for union from remote Message",
));
}
}
_ => {
protocol.skip(field_ident.field_type).await?;
}
}
}
protocol.read_field_end().await?;
protocol.read_struct_end().await?;
if let Some(ret) = ret {
Ok(ret)
} else {
Err(::pilota::thrift::new_protocol_error(
::pilota::thrift::ProtocolErrorKind::InvalidData,
"received empty union from remote Message",
))
}
}
fn size<T: ::pilota::thrift::TLengthProtocol>(&self, protocol: &T) -> usize {
protocol.write_struct_begin_len(&::pilota::thrift::TStructIdentifier {
name: "TestServiceTestResult",
}) + match self {
TestServiceTestResult::Ok(ref value) => {
protocol.write_field_begin_len(&::pilota::thrift::TFieldIdentifier {
name: Some("Ok"),
field_type: ::pilota::thrift::TType::Struct,
id: Some(0i16),
}) + ::pilota::thrift::Message::size(value, protocol)
+ protocol.write_field_end_len()
}
} + protocol.write_field_stop_len()
+ protocol.write_struct_end_len()
}
}
#[derive(PartialOrd, Hash, Eq, Ord, Debug, Default, Clone, PartialEq)]
pub struct TestServiceTestArgsSend {
pub req: Test2,
}
#[::async_trait::async_trait]
impl ::pilota::thrift::Message for TestServiceTestArgsSend {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&self,
protocol: &mut T,
) -> ::std::result::Result<(), ::pilota::thrift::Error> {
let struct_ident = ::pilota::thrift::TStructIdentifier {
name: "TestServiceTestArgsSend",
};
protocol.write_struct_begin(&struct_ident)?;
{
let value = &self.req;
protocol.write_field_begin(::pilota::thrift::TType::Struct, 1i16)?;
::pilota::thrift::Message::encode(value, protocol)?;
protocol.write_field_end()?;
}
protocol.write_field_stop()?;
protocol.write_struct_end()?;
Ok(())
}
fn decode<T: ::pilota::thrift::TInputProtocol>(
protocol: &mut T,
) -> ::std::result::Result<Self, ::pilota::thrift::Error> {
let mut req = None;
protocol.read_struct_begin()?;
loop {
let field_ident = protocol.read_field_begin()?;
if field_ident.field_type == ::pilota::thrift::TType::Stop {
break;
}
let field_id = field_ident.id;
match field_id {
Some(1i16) if field_ident.field_type == ::pilota::thrift::TType::Struct => {
req = Some(::pilota::thrift::Message::decode(protocol)?);
}
_ => {
protocol.skip(field_ident.field_type)?;
}
}
protocol.read_field_end()?;
}
protocol.read_struct_end()?;
let req = if let Some(req) = req {
req
} else {
return Err(::pilota::thrift::Error::Protocol(
::pilota::thrift::ProtocolError::new(
::pilota::thrift::ProtocolErrorKind::InvalidData,
"field req is required".to_string(),
),
));
};
let data = Self { req };
Ok(data)
}
async fn decode_async<C: ::tokio::io::AsyncRead + Unpin + Send>(
protocol: &mut ::pilota::thrift::TAsyncBinaryProtocol<C>,
) -> ::std::result::Result<Self, ::pilota::thrift::Error> {
let mut req = None;
protocol.read_struct_begin().await?;
loop {
let field_ident = protocol.read_field_begin().await?;
if field_ident.field_type == ::pilota::thrift::TType::Stop {
break;
}
let field_id = field_ident.id;
match field_id {
Some(1i16) if field_ident.field_type == ::pilota::thrift::TType::Struct => {
req = Some(::pilota::thrift::Message::decode_async(protocol).await?);
}
_ => {
protocol.skip(field_ident.field_type).await?;
}
}
protocol.read_field_end().await?;
}
protocol.read_struct_end().await?;
let req = if let Some(req) = req {
req
} else {
return Err(::pilota::thrift::Error::Protocol(
::pilota::thrift::ProtocolError::new(
::pilota::thrift::ProtocolErrorKind::InvalidData,
"field req is required".to_string(),
),
));
};
let data = Self { req };
Ok(data)
}
fn size<T: ::pilota::thrift::TLengthProtocol>(&self, protocol: &T) -> usize {
protocol.write_struct_begin_len(&::pilota::thrift::TStructIdentifier {
name: "TestServiceTestArgsSend",
}) + {
let value = &self.req;
protocol.write_field_begin_len(&::pilota::thrift::TFieldIdentifier {
name: Some("req"),
field_type: ::pilota::thrift::TType::Struct,
id: Some(1i16),
}) + ::pilota::thrift::Message::size(value, protocol)
+ protocol.write_field_end_len()
} + protocol.write_field_stop_len()
+ protocol.write_struct_end_len()
}
}
#[derive(PartialOrd, Hash, Eq, Ord, Debug, Default, Clone, PartialEq)]
pub struct TestServiceTestArgsRecv {
pub req: Test2,
}
#[::async_trait::async_trait]
impl ::pilota::thrift::Message for TestServiceTestArgsRecv {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&self,
protocol: &mut T,
) -> ::std::result::Result<(), ::pilota::thrift::Error> {
let struct_ident = ::pilota::thrift::TStructIdentifier {
name: "TestServiceTestArgsRecv",
};
protocol.write_struct_begin(&struct_ident)?;
{
let value = &self.req;
protocol.write_field_begin(::pilota::thrift::TType::Struct, 1i16)?;
::pilota::thrift::Message::encode(value, protocol)?;
protocol.write_field_end()?;
}
protocol.write_field_stop()?;
protocol.write_struct_end()?;
Ok(())
}
fn decode<T: ::pilota::thrift::TInputProtocol>(
protocol: &mut T,
) -> ::std::result::Result<Self, ::pilota::thrift::Error> {
let mut req = None;
protocol.read_struct_begin()?;
loop {
let field_ident = protocol.read_field_begin()?;
if field_ident.field_type == ::pilota::thrift::TType::Stop {
break;
}
let field_id = field_ident.id;
match field_id {
Some(1i16) if field_ident.field_type == ::pilota::thrift::TType::Struct => {
req = Some(::pilota::thrift::Message::decode(protocol)?);
}
_ => {
protocol.skip(field_ident.field_type)?;
}
}
protocol.read_field_end()?;
}
protocol.read_struct_end()?;
let req = if let Some(req) = req {
req
} else {
return Err(::pilota::thrift::Error::Protocol(
::pilota::thrift::ProtocolError::new(
::pilota::thrift::ProtocolErrorKind::InvalidData,
"field req is required".to_string(),
),
));
};
let data = Self { req };
Ok(data)
}
async fn decode_async<C: ::tokio::io::AsyncRead + Unpin + Send>(
protocol: &mut ::pilota::thrift::TAsyncBinaryProtocol<C>,
) -> ::std::result::Result<Self, ::pilota::thrift::Error> {
let mut req = None;
protocol.read_struct_begin().await?;
loop {
let field_ident = protocol.read_field_begin().await?;
if field_ident.field_type == ::pilota::thrift::TType::Stop {
break;
}
let field_id = field_ident.id;
match field_id {
Some(1i16) if field_ident.field_type == ::pilota::thrift::TType::Struct => {
req = Some(::pilota::thrift::Message::decode_async(protocol).await?);
}
_ => {
protocol.skip(field_ident.field_type).await?;
}
}
protocol.read_field_end().await?;
}
protocol.read_struct_end().await?;
let req = if let Some(req) = req {
req
} else {
return Err(::pilota::thrift::Error::Protocol(
::pilota::thrift::ProtocolError::new(
::pilota::thrift::ProtocolErrorKind::InvalidData,
"field req is required".to_string(),
),
));
};
let data = Self { req };
Ok(data)
}
fn size<T: ::pilota::thrift::TLengthProtocol>(&self, protocol: &T) -> usize {
protocol.write_struct_begin_len(&::pilota::thrift::TStructIdentifier {
name: "TestServiceTestArgsRecv",
}) + {
let value = &self.req;
protocol.write_field_begin_len(&::pilota::thrift::TFieldIdentifier {
name: Some("req"),
field_type: ::pilota::thrift::TType::Struct,
id: Some(1i16),
}) + ::pilota::thrift::Message::size(value, protocol)
+ protocol.write_field_end_len()
} + protocol.write_field_stop_len()
+ protocol.write_struct_end_len()
}
}
#[::async_trait::async_trait]
pub trait TestService {}
}
}
Loading

0 comments on commit e862d10

Please sign in to comment.