Skip to content

Use format_ident! macro to build formatted identifiers #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions typed-builder-macro/src/field_info.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote_spanned;
use quote::{format_ident, quote_spanned};
use syn::{parse::Error, spanned::Spanned};

use crate::mutator::Mutator;
use crate::util::{expr_to_lit_string, ident_to_type, path_to_single_string, strip_raw_ident_prefix, ApplyMeta, AttrArg};
use crate::util::{expr_to_lit_string, ident_to_type, path_to_single_string, ApplyMeta, AttrArg};

#[derive(Debug)]
pub struct FieldInfo<'a> {
Expand All @@ -20,7 +20,7 @@ impl<'a> FieldInfo<'a> {
FieldInfo {
ordinal,
name,
generic_ident: syn::Ident::new(&format!("__{}", strip_raw_ident_prefix(name.to_string())), Span::call_site()),
generic_ident: format_ident!("__{}", name),
ty: &field.ty,
builder_attr: field_defaults.with(name, &field.attrs)?,
}
Expand Down Expand Up @@ -75,14 +75,12 @@ impl<'a> FieldInfo<'a> {
}

pub fn setter_method_name(&self) -> Ident {
let name = strip_raw_ident_prefix(self.name.to_string());

if let (Some(prefix), Some(suffix)) = (&self.builder_attr.setter.prefix, &self.builder_attr.setter.suffix) {
Ident::new(&format!("{}{}{}", prefix, name, suffix), Span::call_site())
format_ident!("{}{}{}", prefix, self.name, suffix)
} else if let Some(prefix) = &self.builder_attr.setter.prefix {
Ident::new(&format!("{}{}", prefix, name), Span::call_site())
format_ident!("{}{}", prefix, self.name)
} else if let Some(suffix) = &self.builder_attr.setter.suffix {
Ident::new(&format!("{}{}", name, suffix), Span::call_site())
format_ident!("{}{}", self.name, suffix)
} else {
self.name.clone()
}
Expand Down
29 changes: 6 additions & 23 deletions typed-builder-macro/src/struct_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use syn::{parse::Error, parse_quote, punctuated::Punctuated, GenericArgument, It
use crate::builder_attr::{IntoSetting, TypeBuilderAttr};
use crate::field_info::FieldInfo;
use crate::mutator::Mutator;
use crate::util::{
empty_type, empty_type_tuple, first_visibility, modify_types_generics_hack, public_visibility, strip_raw_ident_prefix,
type_tuple,
};
use crate::util::{empty_type, empty_type_tuple, first_visibility, modify_types_generics_hack, public_visibility, type_tuple};

#[derive(Debug)]
pub struct StructInfo<'a> {
Expand Down Expand Up @@ -52,8 +49,8 @@ impl<'a> StructInfo<'a> {
let builder_name = builder_attr
.builder_type
.get_name()
.map(|name| strip_raw_ident_prefix(name.to_string()))
.unwrap_or_else(|| strip_raw_ident_prefix(format!("{}Builder", ast.ident)));
.map(|name| format_ident!("{}", name.to_string()))
.unwrap_or_else(|| format_ident!("{}Builder", ast.ident));
Ok(StructInfo {
vis: &ast.vis,
name: &ast.ident,
Expand All @@ -63,7 +60,7 @@ impl<'a> StructInfo<'a> {
.map(|(i, f)| FieldInfo::new(i, f, builder_attr.field_defaults.clone()))
.collect::<Result<_, _>>()?,
builder_attr,
builder_name: syn::Ident::new(&builder_name, proc_macro2::Span::call_site()),
builder_name,
})
}

Expand Down Expand Up @@ -281,14 +278,7 @@ impl<'a> StructInfo<'a> {
(quote!(#field_name: #arg_type), arg_expr)
};

let repeated_fields_error_type_name = syn::Ident::new(
&format!(
"{}_Error_Repeated_field_{}",
builder_name,
strip_raw_ident_prefix(field_name.to_string())
),
proc_macro2::Span::call_site(),
);
let repeated_fields_error_type_name = format_ident!("{}_Error_Repeated_field_{}", builder_name, field_name);
let repeated_fields_error_message = format!("Repeated field {}", field_name);

let method_name = field.setter_method_name();
Expand Down Expand Up @@ -385,14 +375,7 @@ impl<'a> StructInfo<'a> {
builder_generics.push(syn::GenericArgument::Type(builder_generics_tuple.into()));
let (impl_generics, _, where_clause) = generics.split_for_impl();

let early_build_error_type_name = syn::Ident::new(
&format!(
"{}_Error_Missing_required_field_{}",
builder_name,
strip_raw_ident_prefix(field_name.to_string())
),
proc_macro2::Span::call_site(),
);
let early_build_error_type_name = format_ident!("{}_Error_Missing_required_field_{}", builder_name, field_name);
let early_build_error_message = format!("Missing required field {}", field_name);

let build_method_name = self.build_method_name();
Expand Down
7 changes: 0 additions & 7 deletions typed-builder-macro/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,6 @@ where
abga
}

pub fn strip_raw_ident_prefix(mut name: String) -> String {
if name.starts_with("r#") {
name.replace_range(0..2, "");
}
name
}

pub fn first_visibility(visibilities: &[Option<&syn::Visibility>]) -> proc_macro2::TokenStream {
let vis = visibilities
.iter()
Expand Down