Skip to content

Commit

Permalink
update generate as_str method, remove panic and stringify used.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxin231 committed Apr 17, 2024
1 parent a8ad6d7 commit 775a159
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 66 deletions.
4 changes: 2 additions & 2 deletions pilota-build/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ where
};
(
format!("pub const {name}: Self = Self({discr});"),
format!("Self({discr}) => stringify!({name}),"),
format!("Self({discr}) => \"{name}\","),
)
})
.collect::<Vec<_>>();
Expand All @@ -253,7 +253,7 @@ where
pub fn as_str(&self) -> &'static str {{
match self {{
{variants_as_str_fields}
_ => panic!("{{}} unknown fields val {{}}", std::any::type_name::<Self>(), self.0),
Self(val) => val.to_string().as_str(),
}}
}}
}}
Expand Down
10 changes: 3 additions & 7 deletions pilota-build/test_data/plugin/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,9 @@ pub mod serde {

pub fn as_str(&self) -> &'static str {
match self {
Self(0) => stringify!(D),
Self(1) => stringify!(E),
_ => panic!(
"{} unknown fields val {}",
std::any::type_name::<Self>(),
self.0
),
Self(0) => "D",
Self(1) => "E",
Self(val) => val.to_string().as_str(),
}
}
}
Expand Down
12 changes: 4 additions & 8 deletions pilota-build/test_data/protobuf/nested_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,10 @@ pub mod nested_message {

pub fn as_str(&self) -> &'static str {
match self {
Self(1) => stringify!(LABEL_OPTIONAL),
Self(2) => stringify!(LABEL_REQUIRED),
Self(3) => stringify!(LABEL_REPEATED),
_ => panic!(
"{} unknown fields val {}",
std::any::type_name::<Self>(),
self.0
),
Self(1) => "LABEL_OPTIONAL",
Self(2) => "LABEL_REQUIRED",
Self(3) => "LABEL_REPEATED",
Self(val) => val.to_string().as_str(),
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions pilota-build/test_data/thrift/const_val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ pub mod const_val {

pub fn as_str(&self) -> &'static str {
match self {
Self(0) => stringify!(A),
Self(1) => stringify!(B),
_ => panic!(
"{} unknown fields val {}",
std::any::type_name::<Self>(),
self.0
),
Self(0) => "A",
Self(1) => "B",
Self(val) => val.to_string().as_str(),
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions pilota-build/test_data/thrift/default_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ pub mod default_value {

pub fn as_str(&self) -> &'static str {
match self {
Self(1) => stringify!(READ),
Self(2) => stringify!(WRITE),
_ => panic!(
"{} unknown fields val {}",
std::any::type_name::<Self>(),
self.0
),
Self(1) => "READ",
Self(2) => "WRITE",
Self(val) => val.to_string().as_str(),
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions pilota-build/test_data/thrift/enum_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ pub mod enum_test {

pub fn as_str(&self) -> &'static str {
match self {
Self(1) => stringify!(A),
Self(16) => stringify!(B),
_ => panic!(
"{} unknown fields val {}",
std::any::type_name::<Self>(),
self.0
),
Self(1) => "A",
Self(16) => "B",
Self(val) => val.to_string().as_str(),
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions pilota-build/test_data/thrift/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,9 @@ pub mod multi {

pub fn as_str(&self) -> &'static str {
match self {
Self(1) => stringify!(READ),
Self(2) => stringify!(WRITE),
_ => panic!(
"{} unknown fields val {}",
std::any::type_name::<Self>(),
self.0
),
Self(1) => "READ",
Self(2) => "WRITE",
Self(val) => val.to_string().as_str(),
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions pilota-build/test_data/thrift/pilota_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,13 +937,9 @@ pub mod pilota_name {

pub fn as_str(&self) -> &'static str {
match self {
Self(0) => stringify!(AA),
Self(1) => stringify!(B),
_ => panic!(
"{} unknown fields val {}",
std::any::type_name::<Self>(),
self.0
),
Self(0) => "AA",
Self(1) => "B",
Self(val) => val.to_string().as_str(),
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions pilota-build/test_data/thrift/self_kw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ pub mod self_kw {

pub fn as_str(&self) -> &'static str {
match self {
Self(0) => stringify!(A),
Self(1) => stringify!(SELF),
_ => panic!(
"{} unknown fields val {}",
std::any::type_name::<Self>(),
self.0
),
Self(0) => "A",
Self(1) => "SELF",
Self(val) => val.to_string().as_str(),
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions pilota-build/test_data/unknown_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2587,13 +2587,9 @@ pub mod unknown_fields {

pub fn as_str(&self) -> &'static str {
match self {
Self(0) => stringify!(A),
Self(1) => stringify!(B),
_ => panic!(
"{} unknown fields val {}",
std::any::type_name::<Self>(),
self.0
),
Self(0) => "A",
Self(1) => "B",
Self(val) => val.to_string().as_str(),
}
}
}
Expand Down

0 comments on commit 775a159

Please sign in to comment.