1use proc_macro2::TokenStream;
2use quote::ToTokens;
3use syn::Ident;
4
5use crate::codegen::FromFieldImpl;
6use crate::options::{OuterFrom, ParseAttribute, ParseData};
7use crate::Result;
8
9#[derive(Debug)]
10pub struct FromFieldOptions {
11 pub base: OuterFrom,
12 pub vis: Option<Ident>,
13 pub ty: Option<Ident>,
14}
15
16impl FromFieldOptions {
17 pub fn new(di: &syn::DeriveInput) -> Result<Self> {
18 (FromFieldOptions {
19 base: OuterFrom::start(di)?,
20 vis: Default::default(),
21 ty: Default::default(),
22 })
23 .parse_attributes(&di.attrs)?
24 .parse_body(&di.data)
25 }
26}
27
28impl ParseAttribute for FromFieldOptions {
29 fn parse_nested(&mut self, mi: &syn::Meta) -> Result<()> {
30 self.base.parse_nested(mi)
31 }
32}
33
34impl ParseData for FromFieldOptions {
35 fn parse_variant(&mut self, variant: &syn::Variant) -> Result<()> {
36 self.base.parse_variant(variant)
37 }
38
39 fn parse_field(&mut self, field: &syn::Field) -> Result<()> {
40 match field.ident.as_ref().map(|v: &Ident| v.to_string()).as_deref() {
41 Some("vis") => {
42 self.vis = field.ident.clone();
43 Ok(())
44 }
45 Some("ty") => {
46 self.ty = field.ident.clone();
47 Ok(())
48 }
49 _ => self.base.parse_field(field),
50 }
51 }
52}
53
54impl<'a> From<&'a FromFieldOptions> for FromFieldImpl<'a> {
55 fn from(v: &'a FromFieldOptions) -> Self {
56 FromFieldImpl {
57 ident: v.base.ident.as_ref(),
58 vis: v.vis.as_ref(),
59 ty: v.ty.as_ref(),
60 attrs: v.base.attrs.as_ref(),
61 base: (&v.base.container).into(),
62 attr_names: &v.base.attr_names,
63 forward_attrs: v.base.forward_attrs.as_ref(),
64 from_ident: v.base.from_ident,
65 }
66 }
67}
68
69impl ToTokens for FromFieldOptions {
70 fn to_tokens(&self, tokens: &mut TokenStream) {
71 FromFieldImpl::from(self).to_tokens(tokens)
72 }
73}
74