Skip to content

Commit

Permalink
chore: Provide to_case functionality to syn::Ident
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Apr 22, 2024
1 parent 495b1d9 commit 8c63021
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
10 changes: 3 additions & 7 deletions sylvia-derive/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);

Expand Down
14 changes: 5 additions & 9 deletions sylvia-derive/src/multitest.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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! {
Expand Down Expand Up @@ -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! {
Expand Down
8 changes: 4 additions & 4 deletions sylvia-derive/src/querier.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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> {
Expand Down Expand Up @@ -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> {
Expand All @@ -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>;
Expand Down
16 changes: 14 additions & 2 deletions sylvia-derive/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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())
}
}

0 comments on commit 8c63021

Please sign in to comment.