use quote::{ToTokens, TokenStreamExt};
use syn::{Ident, Type};
use crate::opts::ModelFieldOpts;
use crate::utils::{is_optional, unwrap_type};
#[derive(Clone, Debug)]
pub struct Field {
pub ident: Ident,
pub ty: Type,
pub skip: bool,
pub create_skip: bool,
pub update_skip: bool,
}
impl Into<Field> for ModelFieldOpts {
fn into(self) -> Field {
Field {
ident: self.ident.expect("derives-model-parse-error"),
ty: self.ty,
skip: self.skip,
create_skip: self.create_skip,
update_skip: self.update_skip,
}
}
}
impl Field {
#[allow(dead_code)]
pub fn name(&self) -> String {
format!("{}", self.ident)
}
}
impl ToTokens for Field {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let ident = &self.ident;
let ty = unwrap_type(&self.ty);
if is_optional(&self.ty) {
tokens.append_all(quote!(pub #ident: Option<#ty>,));
} else {
tokens.append_all(quote!(pub #ident: #ty,));
}
}
}