use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use super::element::{Alternative, EnumValue, Field, Struct};
pub(crate) fn fields(shape: &syn::Fields, head: TokenStream, parts: &[TokenStream]) -> TokenStream {
match shape {
syn::Fields::Unit => head,
syn::Fields::Unnamed(_) => quote!(#head(#(#parts),*)),
syn::Fields::Named(_) => quote!(#head { #(#parts),* }),
}
}
pub(crate) trait Shaped {
fn shape(&self) -> &syn::Fields;
}
impl Shaped for Alternative {
fn shape(&self) -> &syn::Fields {
&self.origin.as_syn().fields
}
}
impl Shaped for EnumValue {
fn shape(&self) -> &syn::Fields {
&self.origin.as_syn().fields
}
}
impl Shaped for Struct {
fn shape(&self) -> &syn::Fields {
&self.origin.as_syn().fields
}
}
impl Field {
pub fn member(&self) -> syn::Member {
match &self.name {
Some(id) => syn::Member::Named(id.clone()),
None => syn::Member::Unnamed(syn::Index::from(self.index)),
}
}
pub fn bind(&self, bind: &impl ToTokens) -> TokenStream {
match &self.name {
Some(id) => quote!(#id: #bind),
None => quote!(#bind),
}
}
}