Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Veetaha committed Aug 31, 2024
1 parent 27fa1c5 commit 3526065
Show file tree
Hide file tree
Showing 12 changed files with 420 additions and 521 deletions.
69 changes: 30 additions & 39 deletions bon-macros/src/builder/builder_gen/input_func.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
generic_param_to_arg, AssocMethodCtx, AssocMethodReceiverCtx, BuilderGenCtx, FinishFunc,
FinishFuncBody, Generics, Member, MemberExpr, MemberOrigin, StartFunc,
FinishFuncBody, Generics, Member, MemberOrigin, RawMember, StartFunc,
};
use crate::builder::params::BuilderParams;
use crate::normalization::NormalizeSelfTy;
Expand Down Expand Up @@ -283,23 +283,37 @@ impl FuncInputCtx {
}

let builder_ident = self.builder_ident();
let builder_private_impl_ident =
quote::format_ident!("__{}PrivateImpl", builder_ident.raw_name());
let builder_state_trait_ident = quote::format_ident!("__{}State", builder_ident.raw_name());

fn typed_args(func: &syn::ItemFn) -> impl Iterator<Item = &syn::PatType> {
func.sig.inputs.iter().filter_map(syn::FnArg::as_typed)
}

let members: Vec<_> = typed_args(&self.norm_func)
let members = typed_args(&self.norm_func)
.zip(typed_args(&self.orig_func))
.map(Member::from_typed_fn_arg)
.try_collect()?;
.map(|(norm_arg, orig_arg)| {
let syn::Pat::Ident(pat) = norm_arg.pat.as_ref() else {
bail!(
&orig_arg.pat,
"use a simple `identifier: type` syntax for the function argument; \
destructuring patterns in arguments aren't supported by the `#[builder]`",
)
};

Ok(RawMember {
attrs: &norm_arg.attrs,
ident: pat.ident.clone(),
norm_ty: norm_arg.ty.clone(),
orig_ty: orig_arg.ty.clone(),
})
})
.collect::<Result<Vec<_>>>()?;

let members = Member::from_raw(MemberOrigin::FnArg, members)?;

let generics = self.generics();

let finish_func_body = FnCallBody {
func: self.adapted_func()?,
sig: self.adapted_func()?.sig,
impl_ctx: self.impl_ctx.clone(),
};

Expand Down Expand Up @@ -358,8 +372,6 @@ impl FuncInputCtx {
conditional_params: self.params.base.on,

builder_ident,
builder_private_impl_ident,
builder_state_trait_ident,

assoc_method_ctx: receiver,
generics,
Expand All @@ -374,13 +386,13 @@ impl FuncInputCtx {
}

struct FnCallBody {
func: syn::ItemFn,
sig: syn::Signature,
impl_ctx: Option<Rc<ImplCtx>>,
}

impl FinishFuncBody for FnCallBody {
fn generate(&self, member_exprs: &[MemberExpr<'_>]) -> TokenStream2 {
let asyncness = &self.func.sig.asyncness;
fn generate(&self, members: &[Member]) -> TokenStream2 {
let asyncness = &self.sig.asyncness;
let maybe_await = asyncness.is_some().then(|| quote!(.await));

// Filter out lifetime generic arguments, because they are not needed
Expand All @@ -389,7 +401,6 @@ impl FinishFuncBody for FnCallBody {
// the turbofish syntax. See the problem of late-bound lifetimes specification
// in the issue https://github.com/rust-lang/rust/issues/42868
let generic_args = self
.func
.sig
.generics
.params
Expand All @@ -398,25 +409,25 @@ impl FinishFuncBody for FnCallBody {
.map(generic_param_to_arg);

let prefix = self
.func
.sig
.receiver()
.map(|receiver| {
let self_token = &receiver.self_token;
quote!(#self_token.__private_impl.receiver.)
quote!(#self_token.__private_receiver.)
})
.or_else(|| {
let self_ty = &self.impl_ctx.as_deref()?.self_ty;
Some(quote!(<#self_ty>::))
});

let func_ident = &self.func.sig.ident;
let func_ident = &self.sig.ident;

let member_exprs = member_exprs.iter().map(|member| &member.expr);
// The variables with values of members in scope for this expression.
let member_vars = members.iter().map(Member::ident);

quote! {
#prefix #func_ident::<#(#generic_args,)*>(
#( #member_exprs ),*
#( #member_vars ),*
)
#maybe_await
}
Expand Down Expand Up @@ -458,26 +469,6 @@ fn merge_generic_params(
.collect()
}

impl Member {
fn from_typed_fn_arg((norm_arg, orig_arg): (&syn::PatType, &syn::PatType)) -> Result<Self> {
let syn::Pat::Ident(pat) = norm_arg.pat.as_ref() else {
bail!(
&orig_arg.pat,
"use a simple `identifier: type` syntax for the function argument; \
destructuring patterns in arguments aren't supported by the `#[builder]`",
)
};

Member::new(
MemberOrigin::FnArg,
&norm_arg.attrs,
pat.ident.clone(),
norm_arg.ty.clone(),
orig_arg.ty.clone(),
)
}
}

#[derive(Default)]
struct FindSelfReference {
self_span: Option<Span>,
Expand Down
58 changes: 22 additions & 36 deletions bon-macros/src/builder/builder_gen/input_struct.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
AssocMethodCtx, BuilderGenCtx, FinishFunc, FinishFuncBody, Generics, Member, MemberExpr,
MemberOrigin, StartFunc,
AssocMethodCtx, BuilderGenCtx, FinishFunc, FinishFuncBody, Generics, Member, MemberOrigin,
RawMember, StartFunc,
};
use crate::builder::params::{BuilderParams, ItemParams};
use crate::util::prelude::*;
Expand Down Expand Up @@ -74,10 +74,6 @@ impl StructInputCtx {

pub(crate) fn into_builder_gen_ctx(self) -> Result<BuilderGenCtx> {
let builder_ident = self.builder_ident();
let builder_private_impl_ident =
quote::format_ident!("__{}PrivateImpl", builder_ident.raw_name());

let builder_state_trait_ident = quote::format_ident!("__{}State", builder_ident.raw_name());

fn fields(struct_item: &syn::ItemStruct) -> Result<&syn::FieldsNamed> {
match &struct_item.fields {
Expand All @@ -91,12 +87,25 @@ impl StructInputCtx {
let norm_fields = fields(&self.norm_struct)?;
let orig_fields = fields(&self.orig_struct)?;

let members: Vec<_> = norm_fields
let members = norm_fields
.named
.iter()
.zip(&orig_fields.named)
.map(Member::from_syn_field)
.try_collect()?;
.map(|(norm_field, orig_field)| {
let ident = norm_field.ident.clone().ok_or_else(|| {
err!(norm_field, "only structs with named fields are supported")
})?;

Ok(RawMember {
attrs: &norm_field.attrs,
ident,
norm_ty: Box::new(norm_field.ty.clone()),
orig_ty: Box::new(orig_field.ty.clone()),
})
})
.collect::<Result<Vec<_>>>()?;

let members = Member::from_raw(MemberOrigin::StructField, members)?;

let generics = Generics {
params: Vec::from_iter(self.norm_struct.generics.params.iter().cloned()),
Expand Down Expand Up @@ -157,8 +166,6 @@ impl StructInputCtx {
conditional_params: self.params.base.on,

builder_ident,
builder_private_impl_ident,
builder_state_trait_ident,

assoc_method_ctx,
generics,
Expand All @@ -177,37 +184,16 @@ struct StructLiteralBody {
}

impl FinishFuncBody for StructLiteralBody {
fn generate(&self, member_exprs: &[MemberExpr<'_>]) -> TokenStream2 {
fn generate(&self, member_exprs: &[Member]) -> TokenStream2 {
let Self { struct_ident } = self;

let member_exprs = member_exprs.iter().map(|MemberExpr { member, expr }| {
let ident = member.ident();
quote! {
#ident: #expr
}
});
// The variables with values of members in scope for this expression.
let member_vars = member_exprs.iter().map(Member::ident);

quote! {
#struct_ident {
#(#member_exprs,)*
#(#member_vars,)*
}
}
}
}

impl Member {
fn from_syn_field((norm_field, orig_field): (&syn::Field, &syn::Field)) -> Result<Self> {
let ident = norm_field
.ident
.clone()
.ok_or_else(|| err!(norm_field, "only structs with named fields are supported"))?;

Member::new(
MemberOrigin::StructField,
&norm_field.attrs,
ident,
Box::new(norm_field.ty.clone()),
Box::new(orig_field.ty.clone()),
)
}
}
Loading

0 comments on commit 3526065

Please sign in to comment.