use proc_macro2::Span;
use syn::Ident;
use crate::parse::FieldMapAttr;
#[derive(Debug)]
pub enum FieldMapping {
Direct { source_field: Ident },
Renamed { source_field: Ident },
Skipped,
IntoConvert { source_field: Ident },
WithFunc {
source_field: Ident,
func_path: String,
},
TryIntoConvert { source_field: Ident },
TryWithFunc {
source_field: Ident,
func_path: String,
},
}
pub fn resolve_field_mapping(
target_field_name: &Ident,
attr: &FieldMapAttr,
) -> syn::Result<FieldMapping> {
if attr.skip {
return Ok(FieldMapping::Skipped);
}
let source_field = if let Some(ref renamed) = attr.from {
Ident::new(renamed, Span::call_site())
} else {
target_field_name.clone()
};
if let Some(ref func) = attr.with {
return Ok(FieldMapping::WithFunc {
source_field,
func_path: func.clone(),
});
}
if let Some(ref func) = attr.try_with {
return Ok(FieldMapping::TryWithFunc {
source_field,
func_path: func.clone(),
});
}
if attr.try_into {
return Ok(FieldMapping::TryIntoConvert { source_field });
}
if attr.into {
return Ok(FieldMapping::IntoConvert { source_field });
}
if attr.from.is_some() {
Ok(FieldMapping::Renamed { source_field })
} else {
Ok(FieldMapping::Direct { source_field })
}
}