Repository: CosmWasm/sylvia
Pull request #359 (2024-04-19 09:51:19)

chore: Provide `to_case` functionality to `syn::Ident`
Small QoL improvement

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/CosmWasm/sylvia/pull/359?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CosmWasm) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 84.60%. Comparing base [(`b90af55`)](https://app.codecov.io/gh/CosmWasm/sylvia/commit/b90af55ca71bdf20cf69e68626d8bb251e8f47bd?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CosmWasm) to head [(`e13e9d3`)](https://app.codecov.io/gh/CosmWasm/sylvia/pull/359?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CosmWasm).


<details><summary>Additional details and impacted files</summary>


```diff
@@                                     Coverage Diff                                      @@
##           jawoznia/refactor/remove_stripped_return_from_msgvariant     #359      +/-   ##
============================================================================================
- Coverage                                                     84.61%   84.60%   -0.02%     
============================================================================================
  Files                                                            37       37              
  Lines                                                          2580     2578       -2     
============================================================================================
- Hits                                                           2183     2181       -2     
  Misses                                                          397      397              
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/CosmWasm/sylvia/pull/359?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CosmWasm).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CosmWasm).


Patch:
diff --git a/sylvia-derive/src/message.rs b/sylvia-derive/src/message.rs
index b5353e8..64bb5f7 100644
--- a/sylvia-derive/src/message.rs
+++ b/sylvia-derive/src/message.rs
@@ -10,7 +10,7 @@ use crate::strip_generics::StripGenerics;
 use crate::strip_self_path::StripSelfPath;
 use crate::utils::{
     as_where_clause, emit_bracketed_generics, extract_return_type, filter_generics, filter_wheres,
-    process_fields,
+    process_fields, SvCasing,
 };
 use crate::variant_descs::{AsVariantDescs, VariantDescs};
 use convert_case::{Case, Casing};
@@ -424,10 +424,7 @@ impl<'a> MsgVariant<'a> {
     {
         let function_name = &sig.ident;
 
-        let name = Ident::new(
-            &function_name.to_string().to_case(Case::UpperCamel),
-            function_name.span(),
-        );
+        let name = function_name.to_case(Case::UpperCamel);
         let fields = process_fields(sig, generics_checker);
         let msg_type = msg_attr.msg_type();
 
@@ -515,8 +512,7 @@ impl<'a> MsgVariant<'a> {
     pub fn emit_variants_constructors(&self) -> TokenStream {
         let Self { name, fields, .. } = self;
 
-        let method_name = name.to_string().to_case(Case::Snake);
-        let method_name = Ident::new(&method_name, name.span());
+        let method_name = name.to_case(Case::Snake);
         let parameters = fields.iter().map(MsgField::emit_method_field);
         let arguments = fields.iter().map(MsgField::name);
 
diff --git a/sylvia-derive/src/querier.rs b/sylvia-derive/src/querier.rs
index 53d6e41..0aae9f2 100644
--- a/sylvia-derive/src/querier.rs
+++ b/sylvia-derive/src/querier.rs
@@ -1,4 +1,4 @@
-use convert_case::{Case, Casing};
+use convert_case::Case;
 use proc_macro2::TokenStream;
 use quote::{quote, ToTokens};
 use syn::{GenericParam, Ident, ItemImpl};
@@ -8,7 +8,7 @@ use crate::check_generics::GetPath;
 use crate::crate_module;
 use crate::message::{MsgField, MsgVariant, MsgVariants};
 use crate::parser::attributes::msg::MsgType;
-use crate::utils::emit_bracketed_generics;
+use crate::utils::{emit_bracketed_generics, SvCasing};
 use crate::variant_descs::AsVariantDescs;
 
 pub struct TraitQuerier<'a, Generic> {
@@ -168,7 +168,7 @@ impl EmitMethod for MsgVariant<'_> {
 
         let parameters = fields.iter().map(MsgField::emit_method_field_folded);
         let fields_names = fields.iter().map(MsgField::name);
-        let variant_name = Ident::new(&name.to_string().to_case(Case::Snake), name.span());
+        let variant_name = name.to_case(Case::Snake);
 
         quote! {
             fn #variant_name(&self, #(#parameters),*) -> Result< #return_type, #sylvia:: cw_std::StdError> {
@@ -187,7 +187,7 @@ impl EmitMethod for MsgVariant<'_> {
             .fields()
             .iter()
             .map(|field| field.emit_method_field_folded());
-        let variant_name = Ident::new(&name.to_string().to_case(Case::Snake), name.span());
+        let variant_name = name.to_case(Case::Snake);
 
         quote! {
             fn #variant_name(&self, #(#parameters),*) -> Result< #return_type, #sylvia:: cw_std::StdError>;
diff --git a/sylvia-derive/src/utils.rs b/sylvia-derive/src/utils.rs
index 02b82a4..9be6631 100644
--- a/sylvia-derive/src/utils.rs
+++ b/sylvia-derive/src/utils.rs
@@ -1,11 +1,12 @@
+use convert_case::Casing;
 use proc_macro2::TokenStream;
 use proc_macro_error::emit_error;
 use quote::{quote, ToTokens};
 use syn::spanned::Spanned;
 use syn::visit::Visit;
 use syn::{
-    parse_quote, FnArg, GenericArgument, GenericParam, Path, PathArguments, ReturnType, Signature,
-    Type, WhereClause, WherePredicate,
+    parse_quote, FnArg, GenericArgument, GenericParam, Ident, Path, PathArguments, ReturnType,
+    Signature, Type, WhereClause, WherePredicate,
 };
 
 use crate::check_generics::{CheckGenerics, GetPath};
@@ -125,3 +126,14 @@ pub fn emit_bracketed_generics<GenericT: ToTokens>(unbonded_generics: &[GenericT
         false => quote! { < #(#unbonded_generics,)* > },
     }
 }
+
+pub trait SvCasing {
+    fn to_case(&self, case: convert_case::Case) -> Self;
+}
+
+impl SvCasing for Ident {
+    fn to_case(&self, case: convert_case::Case) -> Ident {
+        let new_name = &self.to_string().to_case(case);
+        Ident::new(new_name, self.span())
+    }
+}


Test patch:
diff --git a/sylvia-derive/src/multitest.rs b/sylvia-derive/src/multitest.rs
index 67b5abf..5389e3a 100644
--- a/sylvia-derive/src/multitest.rs
+++ b/sylvia-derive/src/multitest.rs
@@ -1,4 +1,4 @@
-use convert_case::{Case, Casing};
+use convert_case::Case;
 use proc_macro2::{Ident, TokenStream};
 use quote::quote;
 use syn::{parse_quote, GenericParam, ItemImpl, ItemTrait, TraitItem, Type};
@@ -10,7 +10,7 @@ use crate::parser::attributes::msg::MsgType;
 use crate::parser::{
     Custom, FilteredOverrideEntryPoints, OverrideEntryPoint, ParsedSylviaAttributes,
 };
-use crate::utils::emit_bracketed_generics;
+use crate::utils::{emit_bracketed_generics, SvCasing};
 use crate::variant_descs::AsVariantDescs;
 
 fn get_ident_from_type(contract_name: &Type) -> &Ident {
@@ -486,11 +486,7 @@ impl<'a> ContractMtHelpers<'a> {
                 .get_only_variant()
                 .as_ref()
                 .map(|reply| {
-                    let reply_name = reply.name();
-                    let reply_name = Ident::new(
-                        &reply_name.to_string().to_case(Case::Snake),
-                        reply_name.span(),
-                    );
+                    let reply_name = reply.name().to_case(Case::Snake);
                     quote! {
                         self. #reply_name ((deps, env).into(), msg).map_err(Into::into)
                     }
@@ -777,7 +773,7 @@ impl EmitMethods for MsgVariant<'_> {
             .collect();
         let arguments = self.as_fields_names();
         let type_name = self.msg_type().as_accessor_name();
-        let name = Ident::new(&name.to_string().to_case(Case::Snake), name.span());
+        let name = name.to_case(Case::Snake);
 
         match self.msg_type() {
             MsgType::Exec => quote! {
@@ -839,7 +835,7 @@ impl EmitMethods for MsgVariant<'_> {
             .map(|field| field.emit_method_field_folded())
             .collect();
         let type_name = self.msg_type().as_accessor_name();
-        let name = Ident::new(&name.to_string().to_case(Case::Snake), name.span());
+        let name = name.to_case(Case::Snake);
 
         match self.msg_type() {
             MsgType::Exec => quote! {