Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
use proc_macro2::TokenStream;
use quote::quote;
use syn::{DeriveInput, Fields};
/// Generate an `impl From<EntityNew> for Entity` based on struct fields.
///
/// Assumes the convention:
/// - The target `New` type is `{StructName}New`.
/// - Both `Entity` and `EntityNew` share the same field names and compatible types.
pub fn from_new_derive_inner(input: DeriveInput) -> syn::Result<TokenStream> {
let struct_name = &input.ident;
let new_type_name = syn::Ident::new(&format!("{}New", struct_name), struct_name.span());
let fields = match &input.data {
syn::Data::Struct(data) => match &data.fields {
Fields::Named(fields_named) => &fields_named.named,
_ => {
return Err(syn::Error::new_spanned(
struct_name,
"FromNew only supports structs with named fields",
))
}
},
_ => {
return Err(syn::Error::new_spanned(
struct_name,
"FromNew only supports structs",
))
}
};
let field_assignments = fields
.iter()
.map(|field| {
let field_name = field.ident.as_ref().ok_or_else(|| {
syn::Error::new_spanned(field, "FromNew only supports named fields")
})?;
Ok(quote! {
#field_name: new.#field_name
})
})
.collect::<syn::Result<Vec<_>>>()?;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let expanded = quote! {
#[automatically_derived]
impl #impl_generics From<#new_type_name> for #struct_name #ty_generics #where_clause {
fn from(new: #new_type_name) -> Self {
Self {
#(#field_assignments),*
}
}
}
};
Ok(expanded)
}