use super::{ArgHelper, Entity};
use quote::{__private::TokenStream, quote};
pub fn impl_new_and_getters(entity: &Entity) -> TokenStream {
let entity_ident = entity.ident();
let mut idents = Vec::new();
let mut types = Vec::new();
let mut comments = Vec::new();
let mut stamp_idents = Vec::new();
let mut stamp_types = Vec::new();
let mut stamp_comments = Vec::new();
for field in entity.fields().inner() {
if field.typ().is_option() && field.stamp_type().is_some() {
stamp_idents.push(field.ident());
stamp_types.push(field.typ().as_type());
stamp_comments.push(field.comments());
} else {
idents.push(field.ident());
types.push(field.typ().as_type());
comments.push(field.comments());
}
}
let (
arg_type,
arg_into_field_suffix,
getter_return,
field_into_getter_prefix_amp,
field_into_getter_suffix,
) = ArgHelper::unzip(types.iter().map(|t| ArgHelper::new(t)).collect());
let (
_stamp_arg_type,
_stamp_arg_into_field_suffix,
stamp_getter_return,
stamp_field_into_getter_prefix_amp,
stamp_field_into_getter_suffix,
) = ArgHelper::unzip(stamp_types.iter().map(|t| ArgHelper::new(t)).collect());
quote! {
impl #entity_ident {
pub fn new(#( #idents: #arg_type, )*) -> Self {
Self {
#( #idents: #idents #arg_into_field_suffix, )*
#( #stamp_idents: None, )*
}
}
#(
#( #comments )*
pub fn #idents(&self) -> #getter_return {
#field_into_getter_prefix_amp self.#idents #field_into_getter_suffix
}
)*
#(
#( #stamp_comments )*
pub fn #stamp_idents(&self) -> #stamp_getter_return {
#stamp_field_into_getter_prefix_amp self.#stamp_idents #stamp_field_into_getter_suffix
}
)*
}
}
}